*
The moose likes I/O and Streams and the fly likes Buffering streams and writers... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Buffering streams and writers..." Watch "Buffering streams and writers..." New topic
Author

Buffering streams and writers...

Spencer Lee
Greenhorn

Joined: Jan 25, 2005
Posts: 13
Hello,

I wondering if there is an advantage to wrapping streams with BufferedOutputStream before passing it to OutputStreamWriter over simply wrapping the OutputStreamWriter with a BufferedWriter? Or is it better to do both?

For example, whats the best practice...

1)


2)


3)



Thanks,
Spencer
[ December 06, 2005: Message edited by: Spencer Lee ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

Originally posted by Spencer Lee:
Hello,
I wondering if there is an advantage to wrapping streams with BufferedOutputStream before passing it to OutputStreamWriter over simply wrapping the OutputStreamWriter with a BufferedWriter?


There is probably no difference because the buffer sizes for BufferedWriter and BufferedOutputStream are the same (8k in JDK 1.5.0). Wrapping a buffered stream with a buffered writer would not get you any positive benefit for the same reason.
Since tweaking IO throughput is very application-dependent, you should look at Java Platform Performance - IO for the details on IO performance and how to measure and test it.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Spencer Lee
Greenhorn

Joined: Jan 25, 2005
Posts: 13
Thank you Joe. I'll check out your link.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There is probably no difference because the buffer sizes for BufferedWriter and BufferedOutputStream are the same...
However if you are using a charset where more than one byte is frequently converted to a single char, then the boundaries of the two buffers aren't going to match up so nicely any more. Whether this matters I have no idea, though.

Personally I would expect that a buffer between disk and memory would produce more benefits than a buffer between memory and memory. But then I distrust that kind of "explanation", so I would produce the "It depends, try it and see" sort of answer too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Buffering streams and writers...
 
Similar Threads
using java to run a perl script
Stream and writer
Invalid byte 1 of 1-byte UTF-8 sequence
Process object
BufferedInputStream: read() vs read(byte[])