• Post Reply Bookmark Topic Watch Topic
  • New Topic

What exactly is BufferedInputStream or BufferedOutputStream  RSS feed

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,
What is buffered stream? How is that useful. Speaking about a fileinputstream it can be said that it is a useful thing but how can the buffered stream help us. is it stored somewhere in the primary memory so that the access to this stream will be fast unlike the file which is stored on the hard disk and fileinputstream is used to retreive the data from the file.
Thanks in advance.
shekar.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A FileInputStream will only read as much data from the disk as you ask for. If you read data one character as a time, and read 1000 characters, that means a thousand separate disk accesses. That could be very slow!
A BufferedInputStream wrapped around a FileInputStream, though, will request data from the FileInputStream in big chunks (512 bytes or so by default, I think.) Thus if you read 1000 characters one at a time, the FileInputStream will only have to go to the disk twice. This will be much faster!
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
firstly thanks for the reply.
it means even the bufferedstream object is also stored on the disk but the retreival in long chunks will enhance the performance. can you explain that taking a file and buffered array with example.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buffered output offers the same advantage of fewer physical disk writes. Are there any issues around incomplete output? I once thought I had a logger that lost the last few critical entries right before the program ended because they were still in the buffer. Does the JVM promise to flush buffers under any particular conditions?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Does the JVM promise to flush buffers under any particular conditions?

Generally, no, not automatically. Missing, unflushed output to System.out is a common newbie problem. You can use a shutdown hook (see Runtime.addShutdownHook()) to ensure data will be flushed; an industrial-strength logging package should definitely do so.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can you explain that taking a file and buffered array with example.

Just a usage example? You just say

You use the BufferedInputStream exactly as if it were a FileInputStream -- it's just much more efficient to read small amounts of data from it than it would be from a FileInputStream.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have taken a buffered input stream for a file and read the contents of the file. now i shall explain the whole procedure. the file is opened for file input stream but the buffered stream reads the data from the file in chuncks(let us say 512 bytes) and therefore the data retreival from the file is fast. where as had we read the data from the file directly the data would have been read one byte after one. is that true?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, right, for this program, which reads one byte at a time from a stream.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!