RFR [6961766]: PrintStream.write() should flush at most once

David Holmes david.holmes at oracle.com
Thu Aug 8 02:18:33 UTC 2013

Hi Ivan,

On 8/08/2013 3:53 AM, Ivan Gerasimov wrote:
> Hello David!
> Thanks for review.
>> Yes - this is NOT A BUG this is the spec for this class:
>> "Optionally, a PrintStream can be created so as to flush
>> automatically; this means that the flush method is automatically
>> invoked after a byte array is written, one of the println methods is
>> invoked, or a newline character or byte ('\n') is written. "
> Sorry, I don't see how the proposed change would contradict the spec .
> The code first writes a char[] buffer to an OutputStream, and then
> invokes flush() if the written array contained at least one '\n' char.
> In addition to that, the documentation for OutputStream#flush() says:
> "Flushes this output stream and forces any buffered output bytes to be
> written out." Thus, there should be no point to have several subsequent
> calls to flush() with no data writes between them.

Sorry I misread the original code - it actually violates the spec as 
well in my opinion. As I read it there should be a flush as soon as the 
\n is written, not simply a flush at some later point in time if a \n 
happened to have been written. As written those additional flushes will 
be no-ops as you rightly point out. So your change is a more efficient 
version of the existing "incorrect" implementation.

That said the layering of the streams in this class is quite confusing 
to me and it seems odd that if both textOut and charOut are 
unconditionally flushed in this method, then why is 'out' only flushed 
based on autoflush and the presence of the \n ??

But I withdraw my objections as what you propose does not change the 


> I don't insist on pushing this change, but I think it's harmless and may
> be useful.
> Sincerely yours,
> Ivan
>> This bug report should be closed as "not an issue".
>> David
>> -----
>>> -Alan

More information about the core-libs-dev mailing list