RFR: 8251942: PrintStream specification is not clear which flush method is automatically invoked

Brian Burkhalter brian.burkhalter at oracle.com
Thu Mar 11 16:27:08 UTC 2021


> On Mar 11, 2021, at 2:45 AM, Daniel Fuchs <dfuchs at openjdk.java.net> wrote:
> 
> On Wed, 10 Mar 2021 23:03:03 GMT, Brian Burkhalter <bpb at openjdk.org <mailto:bpb at openjdk.org>> wrote:
> 
>> Please review this minor change to the specification of `java.io.PrintStream`. The longstanding behavior for flushing is to invoke the `flush()` method of the underlying `OutputStream` rather than its override but this was not made explicit in the specification.
> 
> src/java.base/share/classes/java/io/PrintStream.java line 45:
> 
>> 43:  * output stream is automatically invoked after a byte array is written, one
>> 44:  * of the {@code append}, {@code print}, or {@code println} methods is invoked,
>> 45:  * or a newline character or byte ({@code '\n'}) is written.
> 
> Though not wrong, this was a bit surprising. If I'm not mistaken in the case of `print` and `append` the `flush` method will be called by `write` only if the CharSequence (or String) contains a `\n` character. However, the complex layering where different output stream wrap themselves like Russian dolls (I'm talking about the use of `textOut` and `charOut` here) means that calling `print` or `append` eventually ends up in a call to `write(byte[], int, int)` on the PrintStream which causes a flush() to occur on the underlying stream. So in the case that the String contains a `\n` then flush will be called twice :-) 
> 
> I wonder how much of this is exposing arcane implementation details…


Yes, I noticed that as well. I didn’t think it was worth complicating things for the purpose of this issue to address it.



More information about the core-libs-dev mailing list