I'd like to use a PrintWriter or PrintStream to write formatted strings out to an outputstream (for which I have no control over the creation). However flushing the underlying OutputStream has a big performance hit.
Does a PrintWriter / PrintStream need to be flushed.
If I need to flush the PrintStream / PrintWriter; can I do so without flushing the underlying OutputStream, or will I need to create a "flush protecter" OutputStream to wrap the underlying stream?
To try to be a little clearer on this I want to implement
public void writeSomeString(OutputStream foo);
But this method may be called many times for the same OutputStream (foo). Each call will have to construct its own PrintWriter. I know it's ugly to do so but I have no control over the interface or the creation of foo
.
I'm trying to avoid each method having to flush foo
just to flush its own PrintWriter / PrintStream.
So I want to:
public void writeSomeString(OutputStream foo) {
PrintStream s = new PrintStream(foo);
s.println("bar");
// other code
}
I want to completely avoid this method flushing foo