• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling flush()

 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious to know if there are any effects to calling flush() in different parts of the code. For example the code below calls flush outside the while loop. Would it make any difference to call flush inside the while loop immediately after the os.write method?

[ February 21, 2007: Message edited by: Joe Ess ]
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your question is not merely "yes" or "no".
First, calling flush() on a BufferedOutputStream is unnecessary because it invokes flush() when one invokes close().
Furthermore, the purpose of a buffered stream is to prevent lots of low-level reads and writes since accessing physical storage, networks, databases and so on because each are slow operations. You aren't writing small bits of data and incurring that cost (as often), so using a Buffered*putStream is unnecessary (the default buffer of a Buffered*putStream is 8k, so at best it's cutting your disk access by half). Now if you were reading the input file byte by byte, using buffered streams would have a significant benefit AND flushing after each write would have a significant penalty.
Have a look at the IO Chapter of Java Platform Performance. It discusses these issues and tells how to measure performance and evaluate alternatives.
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer and the article was very interesting. When you say that I'm not writing small bits of data, is that because I'm using the write(buffer, 0, bytesRead) method? So when using os.write(buffer, 0, bytesRead) it is not necessary to use a BufferedOutputStream? This depends on the size of the buffer, would it be better to increase the size to something bigger than 4096 bytes? Hopefully I can learn more about the different types of IO classes.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aaron John:
When you say that I'm not writing small bits of data, is that because I'm using the write(buffer, 0, bytesRead) method?

Correct

Originally posted by Aaron John:
So when using os.write(buffer, 0, bytesRead) it is not necessary to use a BufferedOutputStream?

Under ordinary circumstances, yes (see next answer for qualification).

Originally posted by Aaron John:
This depends on the size of the buffer, would it be better to increase the size to something bigger than 4096 bytes?

The link I gave you shows several different IO approaches and how to evaluate the throughput of each. Your requirements and circumstances will determine exactly which approach you should use. There is no substitute for benchmarking and testing in your specific environment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!