Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

BufferedInputStream

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am just trying to understand how the following code works:

BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[6 * 1024];
int byteRead;
while ((byteRead = is.read(buf)) != -1) {
output.write(buf, 0, byteRead);
}

how exactly does the BufferedInputStream write to an array of bytes ?
a byte only has 8 bits but a character has 16 bits, so how can a text file be read into a an array of bytes ?

thanks in advance,

J.C
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All files are comprised of an ordered sequence of bytes. There is no such thing as a "text file", unless you provide a definition of one beforehand, but that can be said for anything. Some other APIs implicitly provide that definition; for example, java.io.Reader, which defines how bytes are converted to character data - maybe this fits your specific definition of "text file"? java.io.InputStream knows only of ordered bytes.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to IO and Streams...
 
James Clarke
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

I would like to know exactly how the ordered sequence of bytes are read from a file. does anyone know of any resources that provide this information ?

thanks,

J.C
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most OSs these days use the eight-bit byte as the basic unit of file structure. Ultimately, files contain bytes, and it's up to software to interpret what those bytes mean.

A significant portion of the world's computing is done using ASCII, a 7-bit code in which one byte corresponds to one character. Java is more sophisticated in that it uses a 16-bit character, which can represent most of the world's alphabets. But the OS still delivers bytes. It's up to Java to decide how to convert those bytes into characters. It does so using a character encoding. There are many possible encodings, and different ones are used in different parts of the world. The "UTF-8" encoding is a common one in the US -- it's basically the same as ASCII. Each byte is promoted to a character -- except for some special ones, which serve as signals to "shift" into a 3-byte encoding for special characters.

Anyway: the JDK Javadocs themselves contain a lot of information about character encodings. Read the pages for java.lang.Character, java.io.Reader, java.io.Writer, and the other pages those refer to.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!