RFR: 8191441: (Process) add Readers and Writer access to java.lang.Process streams [v4]
Alan Bateman
alanb at openjdk.java.net
Tue Jun 1 13:43:24 UTC 2021
On Tue, 25 May 2021 16:53:28 GMT, Roger Riggs <rriggs at openjdk.org> wrote:
>> Methods are added to java.lang.Process to read and write characters and lines from and to a spawned Process.
>> The Charset used to encode and decode characters to bytes can be specified or use the
>> operating system native encoding as is available from the "native.encoding" system property.
>
> Roger Riggs has updated the pull request incrementally with one additional commit since the last revision:
>
> Described charset mapping of malformed chars in outputWriter
> Repeated calls to inputReader, errorReader, and outputWriter now return the same instance
> and check for inconsistent charset argument
> Added warnings about concurrently use of input/output streams and readers/writers.
src/java.base/share/classes/java/lang/Process.java line 105:
> 103: // Readers and Writers created for this process; so repeated calls return the same object
> 104: // All updates must be done while synchronized on this Process.
> 105: private BufferedWriter outputWriter = null;
No need to explicitly initialise all these fields to null.
src/java.base/share/classes/java/lang/Process.java line 131:
> 129: * Use {@link #getOutputStream} and {@link #outputWriter} with extreme care.
> 130: * Output to the {@code BufferedWriter} may be held in the buffer until
> 131: * {@linkplain BufferedWriter#flush flush} is called.
I think this will need a bit of wordsmithing to make it clear that its the usage of both streams for the same Process requires great care (as it stands, it reads like the usage of either method is dangerous).
src/java.base/share/classes/java/lang/Process.java line 207:
> 205: * {@link Charset} named by the {@code native.encoding}
> 206: * system property or the {@link Charset#defaultCharset()} if the
> 207: * {@code native.encoding} is not supported.
"if the native.encoding is not supported". I think this needs an adjustment to make it clear that the value of the property is not a valid charset.
src/java.base/share/classes/java/lang/Process.java line 425:
> 423: } else {
> 424: if (!outputCharset.equals(charset))
> 425: throw new IllegalArgumentException("BufferedWriter was created with charset: " + outputCharset);
I'm not sure that IAE is the right exception here, I think it's closer to IllegalStateException because the first usage of outputWriter has the side effect of setting the charset for the Process's writer. Another option here is to just put it into the "unpredictable" bucket that is using getOutputStream and outputWriter at the same time. In that case, it could just return a new BufferedWriter when it doesn't match the charset of the first usage.
-------------
PR: https://git.openjdk.java.net/jdk/pull/4134
More information about the core-libs-dev
mailing list