aspose file tools*
The moose likes I/O and Streams and the fly likes BufferedInputStream: read() vs read(byte[]) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "BufferedInputStream: read() vs read(byte[])" Watch "BufferedInputStream: read() vs read(byte[])" New topic
Author

BufferedInputStream: read() vs read(byte[])

Janus Hasorius
Greenhorn

Joined: Nov 12, 2010
Posts: 1
First of all, hello everybody!
My question also applies to output streams and writes.
Granted I have something like that

is there any advantage in creating a byte buffer and reading a file with read(byte[]) method, as opposed to reading it byte by byte?
My guess is no, since we are simply transferring bytes from one point in memory to another and the compiler can optimize it.

Also, did i put anything redundant into that code?:

Cheers
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I'm no expert on this, so wait for confirmation from one before you take this as gospel, but I thought that the ability to read in data as chunks rather than byte by byte was the whole reason for the existence of the BufferedInputStream class. So if I understand things correctly, you are better off using the read(byte[] b, int off, int len) overload.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

It wouldn't be difficult to put together a quick benchmark to see what difference it makes in reading a large file, let's say. Guessing shouldn't be necessary. Or done.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BufferedInputStream: read() vs read(byte[])
 
Similar Threads
BufferedInputStream.read method
BufferedInputStream
bufferedinputstream help
Speeding up BufferedInputStream and FileOutputStream
Reading file into byte array