File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BufferedInputStream

 
James Clarke
Ranch Hand
Posts: 148
  • 0
  • 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
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to IO and Streams...
 
James Clarke
Ranch Hand
Posts: 148
  • 0
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic