Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Calling flush()

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic