RFR: 8251942: PrintStream specification is not clear which flush method is automatically invoked
Daniel Fuchs
dfuchs at openjdk.java.net
Thu Mar 11 10:45:11 UTC 2021
On Wed, 10 Mar 2021 23:03:03 GMT, Brian Burkhalter <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...
-------------
PR: https://git.openjdk.java.net/jdk/pull/2926
More information about the core-libs-dev
mailing list