Skip to content

Commit 5ce522d

Browse files
author
duke
committedJun 7, 2021
Automatic merge of jdk:master into master
2 parents cfc5cc9 + 81600dc commit 5ce522d

File tree

2 files changed

+676
-8
lines changed

2 files changed

+676
-8
lines changed
 

‎src/java.base/share/classes/java/lang/Process.java

+282-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,13 @@
2525

2626
package java.lang;
2727

28+
import jdk.internal.util.StaticProperty;
29+
2830
import java.io.*;
2931
import java.lang.ProcessBuilder.Redirect;
32+
import java.nio.charset.Charset;
33+
import java.nio.charset.UnsupportedCharsetException;
34+
import java.util.Objects;
3035
import java.util.concurrent.CompletableFuture;
3136
import java.util.concurrent.ForkJoinPool;
3237
import java.util.concurrent.TimeUnit;
@@ -57,6 +62,10 @@
5762
* {@link #getOutputStream()},
5863
* {@link #getInputStream()}, and
5964
* {@link #getErrorStream()}.
65+
* The I/O streams of characters and lines can be written and read using the methods
66+
* {@link #outputWriter()}, {@link #outputWriter(Charset)}},
67+
* {@link #inputReader()}, {@link #inputReader(Charset)},
68+
* {@link #errorReader()}, and {@link #errorReader(Charset)}.
6069
* The parent process uses these streams to feed input to and get output
6170
* from the process. Because some native platforms only provide
6271
* limited buffer size for standard input and output streams, failure
@@ -90,6 +99,16 @@
9099
* @since 1.0
91100
*/
92101
public abstract class Process {
102+
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;
106+
private Charset outputCharset;
107+
private BufferedReader inputReader;
108+
private Charset inputCharset;
109+
private BufferedReader errorReader;
110+
private Charset errorCharset;
111+
93112
/**
94113
* Default constructor for Process.
95114
*/
@@ -106,7 +125,13 @@ public Process() {}
106125
* then this method will return a
107126
* <a href="ProcessBuilder.html#redirect-input">null output stream</a>.
108127
*
109-
* <p>Implementation note: It is a good idea for the returned
128+
* @apiNote
129+
* When writing to both {@link #getOutputStream()} and either {@link #outputWriter()}
130+
* or {@link #outputWriter(Charset)}, {@link BufferedWriter#flush BufferedWriter.flush}
131+
* should be called before writes to the {@code OutputStream}.
132+
*
133+
* @implNote
134+
* Implementation note: It is a good idea for the returned
110135
* output stream to be buffered.
111136
*
112137
* @return the output stream connected to the normal input of the
@@ -132,7 +157,12 @@ public Process() {}
132157
* then the input stream returned by this method will receive the
133158
* merged standard output and the standard error of the process.
134159
*
135-
* <p>Implementation note: It is a good idea for the returned
160+
* @apiNote
161+
* Use {@link #getInputStream} and {@link #inputReader} with extreme care.
162+
* The {@code BufferedReader} may have buffered input from the input stream.
163+
*
164+
* @implNote
165+
* Implementation note: It is a good idea for the returned
136166
* input stream to be buffered.
137167
*
138168
* @return the input stream connected to the normal output of the
@@ -153,14 +183,235 @@ public Process() {}
153183
* then this method will return a
154184
* <a href="ProcessBuilder.html#redirect-output">null input stream</a>.
155185
*
156-
* <p>Implementation note: It is a good idea for the returned
186+
* @apiNote
187+
* Use {@link #getInputStream} and {@link #inputReader} with extreme care.
188+
* The {@code BufferedReader} may have buffered input from the input stream.
189+
*
190+
* @implNote
191+
* Implementation note: It is a good idea for the returned
157192
* input stream to be buffered.
158193
*
159194
* @return the input stream connected to the error output of
160195
* the process
161196
*/
162197
public abstract InputStream getErrorStream();
163198

199+
/**
200+
* Returns a {@link BufferedReader BufferedReader} connected to the standard
201+
* output of the process. The {@link Charset} for the native encoding is used
202+
* to read characters, lines, or stream lines from standard output.
203+
*
204+
* <p>This method delegates to {@link #inputReader(Charset)} using the
205+
* {@link Charset} named by the {@code native.encoding} system property.
206+
* If the {@code native.encoding} is not a valid charset name or not supported
207+
* the {@link Charset#defaultCharset()} is used.
208+
*
209+
* @return a {@link BufferedReader BufferedReader} using the
210+
* {@code native.encoding} if supported, otherwise, the
211+
* {@link Charset#defaultCharset()}
212+
* @since 17
213+
*/
214+
public final BufferedReader inputReader() {
215+
return inputReader(CharsetHolder.nativeCharset());
216+
}
217+
218+
/**
219+
* Returns a {@link BufferedReader BufferedReader} connected to the
220+
* standard output of this process using a Charset.
221+
* The {@code BufferedReader} can be used to read characters, lines,
222+
* or stream lines of the standard output.
223+
*
224+
* <p>Characters are read by an InputStreamReader that reads and decodes bytes
225+
* from this process {@link #getInputStream()}. Bytes are decoded to characters
226+
* using the {@code charset}; malformed-input and unmappable-character
227+
* sequences are replaced with the charset's default replacement.
228+
* The {@code BufferedReader} reads and buffers characters from the InputStreamReader.
229+
*
230+
* <p>The first call to this method creates the {@link BufferedReader BufferedReader},
231+
* if called again with the same {@code charset} the same {@code BufferedReader} is returned.
232+
* It is an error to call this method again with a different {@code charset}.
233+
*
234+
* <p>If the standard output of the process has been redirected using
235+
* {@link ProcessBuilder#redirectOutput(Redirect) ProcessBuilder.redirectOutput}
236+
* then the {@code InputStreamReader} will be reading from a
237+
* <a href="ProcessBuilder.html#redirect-output">null input stream</a>.
238+
*
239+
* <p>Otherwise, if the standard error of the process has been redirected using
240+
* {@link ProcessBuilder#redirectErrorStream(boolean)
241+
* ProcessBuilder.redirectErrorStream} then the input reader returned by
242+
* this method will receive the merged standard output and the standard error
243+
* of the process.
244+
*
245+
* @apiNote
246+
* Using both {@link #getInputStream} and {@link #inputReader(Charset)} has
247+
* unpredictable behavior since the buffered reader reads ahead from the
248+
* input stream.
249+
*
250+
* <p>When the process has terminated, and the standard input has not been redirected,
251+
* reading of the bytes available from the underlying stream is on a best effort basis and
252+
* may be unpredictable.
253+
*
254+
* @param charset the {@code Charset} used to decode bytes to characters
255+
* @return a {@code BufferedReader} for the standard output of the process using the {@code charset}
256+
* @throws NullPointerException if the {@code charset} is {@code null}
257+
* @throws IllegalStateException if called more than once with different charset arguments
258+
* @since 17
259+
*/
260+
public final BufferedReader inputReader(Charset charset) {
261+
Objects.requireNonNull(charset, "charset");
262+
synchronized (this) {
263+
if (inputReader == null) {
264+
inputCharset = charset;
265+
inputReader = new BufferedReader(new InputStreamReader(getInputStream(), charset));
266+
} else {
267+
if (!inputCharset.equals(charset))
268+
throw new IllegalStateException("BufferedReader was created with charset: " + inputCharset);
269+
}
270+
return inputReader;
271+
}
272+
}
273+
274+
/**
275+
* Returns a {@link BufferedReader BufferedReader} connected to the standard
276+
* error of the process. The {@link Charset} for the native encoding is used
277+
* to read characters, lines, or stream lines from standard error.
278+
*
279+
* <p>This method delegates to {@link #errorReader(Charset)} using the
280+
* {@link Charset} named by the {@code native.encoding} system property.
281+
* If the {@code native.encoding} is not a valid charset name or not supported
282+
* the {@link Charset#defaultCharset()} is used.
283+
*
284+
* @return a {@link BufferedReader BufferedReader} using the
285+
* {@code native.encoding} if supported, otherwise, the
286+
* {@link Charset#defaultCharset()}
287+
* @since 17
288+
*/
289+
public final BufferedReader errorReader() {
290+
return errorReader(CharsetHolder.nativeCharset());
291+
}
292+
293+
/**
294+
* Returns a {@link BufferedReader BufferedReader} connected to the
295+
* standard error of this process using a Charset.
296+
* The {@code BufferedReader} can be used to read characters, lines,
297+
* or stream lines of the standard error.
298+
*
299+
* <p>Characters are read by an InputStreamReader that reads and decodes bytes
300+
* from this process {@link #getErrorStream()}. Bytes are decoded to characters
301+
* using the {@code charset}; malformed-input and unmappable-character
302+
* sequences are replaced with the charset's default replacement.
303+
* The {@code BufferedReader} reads and buffers characters from the InputStreamReader.
304+
*
305+
* <p>The first call to this method creates the {@link BufferedReader BufferedReader},
306+
* if called again with the same {@code charset} the same {@code BufferedReader} is returned.
307+
* It is an error to call this method again with a different {@code charset}.
308+
*
309+
* <p>If the standard error of the process has been redirected using
310+
* {@link ProcessBuilder#redirectError(Redirect) ProcessBuilder.redirectError} or
311+
* {@link ProcessBuilder#redirectErrorStream(boolean) ProcessBuilder.redirectErrorStream}
312+
* then the {@code InputStreamReader} will be reading from a
313+
* <a href="ProcessBuilder.html#redirect-output">null input stream</a>.
314+
*
315+
* @apiNote
316+
* Using both {@link #getErrorStream} and {@link #errorReader(Charset)} has
317+
* unpredictable behavior since the buffered reader reads ahead from the
318+
* error stream.
319+
*
320+
* <p>When the process has terminated, and the standard error has not been redirected,
321+
* reading of the bytes available from the underlying stream is on a best effort basis and
322+
* may be unpredictable.
323+
*
324+
* @param charset the {@code Charset} used to decode bytes to characters
325+
* @return a {@code BufferedReader} for the standard error of the process using the {@code charset}
326+
* @throws NullPointerException if the {@code charset} is {@code null}
327+
* @throws IllegalStateException if called more than once with different charset arguments
328+
* @since 17
329+
*/
330+
public final BufferedReader errorReader(Charset charset) {
331+
Objects.requireNonNull(charset, "charset");
332+
synchronized (this) {
333+
if (errorReader == null) {
334+
errorCharset = charset;
335+
errorReader = new BufferedReader(new InputStreamReader(getErrorStream(), charset));
336+
} else {
337+
if (!errorCharset.equals(charset))
338+
throw new IllegalStateException("BufferedReader was created with charset: " + errorCharset);
339+
}
340+
return errorReader;
341+
}
342+
}
343+
344+
/**
345+
* Returns a {@code BufferedWriter} connected to the normal input of the process
346+
* using the native encoding.
347+
* Writes text to a character-output stream, buffering characters so as to provide
348+
* for the efficient writing of single characters, arrays, and strings.
349+
*
350+
* <p>This method delegates to {@link #outputWriter(Charset)} using the
351+
* {@link Charset} named by the {@code native.encoding} system property.
352+
* If the {@code native.encoding} is not a valid charset name or not supported
353+
* the {@link Charset#defaultCharset()} is used.
354+
*
355+
* @return a {@code BufferedWriter} to the standard input of the process using the charset
356+
* for the {@code native.encoding} system property
357+
* @since 17
358+
*/
359+
public final BufferedWriter outputWriter() {
360+
return outputWriter(CharsetHolder.nativeCharset());
361+
}
362+
363+
/**
364+
* Returns a {@code BufferedWriter} connected to the normal input of the process
365+
* using a Charset.
366+
* Writes text to a character-output stream, buffering characters so as to provide
367+
* for the efficient writing of single characters, arrays, and strings.
368+
*
369+
* <p>Characters written by the writer are encoded to bytes using {@link OutputStreamWriter}
370+
* and the {@link Charset} are written to the standard input of the process represented
371+
* by this {@code Process}.
372+
* Malformed-input and unmappable-character sequences are replaced with the charset's
373+
* default replacement.
374+
*
375+
* <p>The first call to this method creates the {@link BufferedWriter BufferedWriter},
376+
* if called again with the same {@code charset} the same {@code BufferedWriter} is returned.
377+
* It is an error to call this method again with a different {@code charset}.
378+
*
379+
* <p>If the standard input of the process has been redirected using
380+
* {@link ProcessBuilder#redirectInput(Redirect)
381+
* ProcessBuilder.redirectInput} then the {@code OutputStreamWriter} writes to a
382+
* <a href="ProcessBuilder.html#redirect-input">null output stream</a>.
383+
*
384+
* @apiNote
385+
* A {@linkplain BufferedWriter} writes characters, arrays of characters, and strings.
386+
* Wrapping the {@link BufferedWriter} with a {@link PrintWriter} provides
387+
* efficient buffering and formatting of primitives and objects as well as support
388+
* for auto-flush on line endings.
389+
* Call the {@link BufferedWriter#flush()} method to flush buffered output to the process.
390+
* <p>
391+
* When writing to both {@link #getOutputStream()} and either {@link #outputWriter()}
392+
* or {@link #outputWriter(Charset)}, {@linkplain BufferedWriter#flush BufferedWriter.flush}
393+
* should be called before writes to the {@code OutputStream}.
394+
*
395+
* @param charset the {@code Charset} to encode characters to bytes
396+
* @return a {@code BufferedWriter} to the standard input of the process using the {@code charset}
397+
* @throws NullPointerException if the {@code charset} is {@code null}
398+
* @throws IllegalStateException if called more than once with different charset arguments
399+
* @since 17
400+
*/
401+
public final BufferedWriter outputWriter(Charset charset) {
402+
Objects.requireNonNull(charset, "charset");
403+
synchronized (this) {
404+
if (outputWriter == null) {
405+
outputCharset = charset;
406+
outputWriter = new BufferedWriter(new OutputStreamWriter(getOutputStream(), charset));
407+
} else {
408+
if (!outputCharset.equals(charset))
409+
throw new IllegalStateException("BufferedWriter was created with charset: " + outputCharset);
410+
}
411+
return outputWriter;
412+
}
413+
}
414+
164415
/**
165416
* Causes the current thread to wait, if necessary, until the
166417
* process represented by this {@code Process} object has
@@ -261,7 +512,7 @@ public boolean waitFor(long timeout, TimeUnit unit)
261512
* when the process has terminated.
262513
* <p>
263514
* Invoking this method on {@code Process} objects returned by
264-
* {@link ProcessBuilder#start} and {@link Runtime#exec} forcibly terminate
515+
* {@link ProcessBuilder#start()} and {@link Runtime#exec} forcibly terminate
265516
* the process.
266517
*
267518
* @implSpec
@@ -292,7 +543,7 @@ public Process destroyForcibly() {
292543
* forcibly and immediately terminates the process.
293544
* <p>
294545
* Invoking this method on {@code Process} objects returned by
295-
* {@link ProcessBuilder#start} and {@link Runtime#exec} return
546+
* {@link ProcessBuilder#start()} and {@link Runtime#exec} return
296547
* {@code true} or {@code false} depending on the platform implementation.
297548
*
298549
* @implSpec
@@ -371,7 +622,7 @@ public long pid() {
371622
* {@linkplain java.util.concurrent.CompletableFuture#cancel(boolean) Cancelling}
372623
* the CompletableFuture does not affect the Process.
373624
* <p>
374-
* Processes returned from {@link ProcessBuilder#start} override the
625+
* Processes returned from {@link ProcessBuilder#start()} override the
375626
* default implementation to provide an efficient mechanism to wait
376627
* for process exit.
377628
*
@@ -463,7 +714,7 @@ public boolean isReleasable() {
463714
/**
464715
* Returns a ProcessHandle for the Process.
465716
*
466-
* {@code Process} objects returned by {@link ProcessBuilder#start} and
717+
* {@code Process} objects returned by {@link ProcessBuilder#start()} and
467718
* {@link Runtime#exec} implement {@code toHandle} as the equivalent of
468719
* {@link ProcessHandle#of(long) ProcessHandle.of(pid)} including the
469720
* check for a SecurityManager and {@code RuntimePermission("manageProcess")}.
@@ -589,4 +840,27 @@ public long skip(long n) throws IOException {
589840
return n - remaining;
590841
}
591842
}
843+
844+
/**
845+
* A nested class to delay looking up the Charset for the native encoding.
846+
*/
847+
private static class CharsetHolder {
848+
private final static Charset nativeCharset;
849+
static {
850+
Charset cs;
851+
try {
852+
cs = Charset.forName(StaticProperty.nativeEncoding());
853+
} catch (UnsupportedCharsetException uce) {
854+
cs = Charset.defaultCharset();
855+
}
856+
nativeCharset = cs;
857+
}
858+
859+
/**
860+
* Charset for the native encoding or {@link Charset#defaultCharset().
861+
*/
862+
static Charset nativeCharset() {
863+
return nativeCharset;
864+
}
865+
}
592866
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,394 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import static org.testng.Assert.*;
25+
26+
import org.testng.Assert;
27+
import org.testng.annotations.DataProvider;
28+
import org.testng.annotations.Test;
29+
30+
import jdk.test.lib.process.ProcessTools;
31+
32+
import jdk.test.lib.hexdump.HexPrinter;
33+
import jdk.test.lib.hexdump.HexPrinter.Formatters;
34+
35+
import java.io.BufferedReader;
36+
import java.io.BufferedWriter;
37+
import java.io.IOException;
38+
import java.io.Writer;
39+
import java.nio.ByteBuffer;
40+
import java.nio.file.Path;
41+
import java.nio.file.Files;
42+
import java.nio.charset.Charset;
43+
import java.nio.charset.StandardCharsets;
44+
import java.nio.charset.UnsupportedCharsetException;
45+
import java.util.List;
46+
import java.util.Locale;
47+
48+
import jtreg.SkippedException;
49+
50+
/*
51+
* @test
52+
* @library /test/lib
53+
* @build jdk.test.lib.process.ProcessTools jdk.test.lib.hexdump.HexPrinter
54+
* @run testng ReaderWriterTest
55+
*/
56+
57+
@Test
58+
public class ReaderWriterTest {
59+
60+
static final String ASCII = "ASCII: \u0000_A-Z_a-Z_\u007C_\u007D_\u007E_\u007F_;";
61+
static final String ISO_8859_1 = " Symbols: \u00AB_\u00BB_\u00fc_\u00fd_\u00fe_\u00ff;";
62+
static final String FRACTIONS = " Fractions: \u00bc_\u00bd_\u00be_\u00bf;";
63+
64+
public static final String TESTCHARS = "OneWay: " + ASCII + ISO_8859_1 + FRACTIONS;
65+
public static final String ROUND_TRIP_TESTCHARS = "RoundTrip: " + ASCII + ISO_8859_1 + FRACTIONS;
66+
67+
@DataProvider(name="CharsetCases")
68+
static Object[][] charsetCases() {
69+
return new Object[][] {
70+
{"UTF-8"},
71+
{"ISO8859-1"},
72+
{"US-ASCII"},
73+
};
74+
}
75+
76+
/**
77+
* Test the defaults case of native.encoding. No extra command line flags or switches.
78+
*/
79+
@Test
80+
void testCaseNativeEncoding() throws IOException {
81+
String nativeEncoding = System.getProperty("native.encoding");
82+
Charset cs = Charset.forName(nativeEncoding);
83+
System.out.println("Native.encoding Charset: " + cs);
84+
85+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("ReaderWriterTest$ChildWithCharset");
86+
Process p = pb.start();
87+
writeTestChars(p.outputWriter());
88+
checkReader(p.inputReader(), cs, "Out");
89+
checkReader(p.errorReader(), cs, "Err");
90+
try {
91+
int exitValue = p.waitFor();
92+
if (exitValue != 0)
93+
System.out.println("exitValue: " + exitValue);
94+
} catch (InterruptedException ie) {
95+
Assert.fail("waitFor interrupted");
96+
}
97+
}
98+
99+
/**
100+
* Test that redirects of input and error streams result in Readers that are empty.
101+
* Test that when the output to a process is redirected, the writer acts as
102+
* a null stream and throws an exception as expected for a null output stream
103+
* as specified by ProcessBuilder.
104+
*/
105+
@Test
106+
void testRedirects() throws IOException {
107+
String nativeEncoding = System.getProperty("native.encoding");
108+
Charset cs = Charset.forName(nativeEncoding);
109+
System.out.println("Native.encoding Charset: " + cs);
110+
111+
Path inPath = Path.of("InFile.tmp");
112+
BufferedWriter inWriter = Files.newBufferedWriter(inPath);
113+
inWriter.close();
114+
115+
Path outPath = Path.of("OutFile.tmp");
116+
Path errorPath = Path.of("ErrFile.tmp");
117+
118+
for (int errType = 1; errType < 4; errType++) {
119+
// Three cases to test for which the error stream is empty
120+
// 1: redirectErrorStream(false); redirect of errorOutput to a file
121+
// 2: redirectErrorStream(true); no redirect of errorOutput
122+
// 3: redirectErrorStream(true); redirect of errorOutput to a file
123+
124+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("ReaderWriterTest$ChildWithCharset");
125+
pb.redirectInput(inPath.toFile());
126+
pb.redirectOutput(outPath.toFile());
127+
if (errType == 1 || errType == 3) {
128+
pb.redirectError(errorPath.toFile());
129+
}
130+
if (errType == 2 || errType == 3) {
131+
pb.redirectErrorStream(true);
132+
}
133+
Process p = pb.start();
134+
// Output has been redirected to a null stream; success is IOException on the write
135+
try {
136+
BufferedWriter wr = p.outputWriter();
137+
wr.write("X");
138+
wr.flush();
139+
Assert.fail("writing to null stream should throw IOException");
140+
} catch (IOException ioe) {
141+
// Normal, A Null output stream is closed when created.
142+
}
143+
144+
// InputReader should be empty; and at EOF
145+
BufferedReader inputReader = p.inputReader();
146+
int ch = inputReader.read();
147+
Assert.assertEquals(ch, -1, "inputReader not at EOF: ch: " + (char)ch);
148+
149+
// InputReader should be empty; and at EOF
150+
BufferedReader errorReader = p.errorReader();
151+
ch = errorReader.read();
152+
Assert.assertEquals(ch, -1, "errorReader not at EOF: ch: " + (char)ch);
153+
154+
try {
155+
int exitValue = p.waitFor();
156+
if (exitValue != 0) System.out.println("exitValue: " + exitValue);
157+
} catch (InterruptedException ie) {
158+
Assert.fail("waitFor interrupted");
159+
}
160+
}
161+
}
162+
163+
/**
164+
* Write the test characters to the child using the Process.outputWriter.
165+
* @param writer the Writer
166+
* @throws IOException if an I/O error occurs
167+
*/
168+
private static void writeTestChars(Writer writer) throws IOException {
169+
// Write the test data to the child
170+
try (writer) {
171+
writer.append(ROUND_TRIP_TESTCHARS);
172+
writer.append(System.lineSeparator());
173+
}
174+
}
175+
176+
/**
177+
* Test a child with a character set.
178+
* A Process is spawned; characters are written to and read from the child
179+
* using the character set and compared.
180+
*
181+
* @param encoding a charset name
182+
*/
183+
@Test(dataProvider = "CharsetCases", enabled = true)
184+
void testCase(String encoding) throws IOException {
185+
Charset cs = null;
186+
try {
187+
cs = Charset.forName(encoding);
188+
System.out.println("Charset: " + cs);
189+
} catch (UnsupportedCharsetException use) {
190+
throw new SkippedException("Charset not supported: " + encoding);
191+
}
192+
String cleanCSName = cleanCharsetName(cs);
193+
194+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
195+
"-Dsun.stdout.encoding=" + cleanCSName, // Encode in the child using the charset
196+
"-Dsun.stderr.encoding=" + cleanCSName,
197+
"ReaderWriterTest$ChildWithCharset");
198+
199+
Process p = pb.start();
200+
// Write the test data to the child
201+
writeTestChars(p.outputWriter(cs));
202+
checkReader(p.inputReader(cs), cs, "Out");
203+
checkReader(p.errorReader(cs), cs, "Err");
204+
try {
205+
int exitValue = p.waitFor();
206+
if (exitValue != 0)
207+
System.out.println("exitValue: " + exitValue);
208+
} catch (InterruptedException ie) {
209+
210+
}
211+
}
212+
213+
/**
214+
* Test passing null when a charset is expected
215+
* @throws IOException if an I/O error occurs; not expected
216+
*/
217+
@Test
218+
void testNullCharsets() throws IOException {
219+
// Launch a child; its behavior is not interesting and is ignored
220+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
221+
"ReaderWriterTest$ChildWithCharset");
222+
223+
Process p = pb.start();
224+
try {
225+
writeTestChars(p.outputWriter(null));
226+
Assert.fail("Process.outputWriter(null) did not throw NPE");
227+
} catch (NullPointerException npe) {
228+
// expected, ignore
229+
}
230+
try {
231+
checkReader(p.inputReader(null), null, "Out");
232+
Assert.fail("Process.inputReader(null) did not throw NPE");
233+
} catch (NullPointerException npe) {
234+
// expected, ignore
235+
}
236+
try {
237+
checkReader(p.errorReader(null), null, "Err");
238+
Assert.fail("Process.errorReader(null) did not throw NPE");
239+
} catch (NullPointerException npe) {
240+
// expected, ignore
241+
}
242+
243+
p.destroyForcibly();
244+
try {
245+
// Collect the exit status to cleanup after the process; but ignore it
246+
p.waitFor();
247+
} catch (InterruptedException ie) {
248+
// Ignored
249+
}
250+
}
251+
252+
/**
253+
* Test passing different charset on multiple calls when the same charset is expected.
254+
* @throws IOException if an I/O error occurs; not expected
255+
*/
256+
@Test
257+
void testIllegalArgCharsets() throws IOException {
258+
String nativeEncoding = System.getProperty("native.encoding");
259+
Charset cs = Charset.forName(nativeEncoding);
260+
System.out.println("Native.encoding Charset: " + cs);
261+
Charset otherCharset = cs.equals(StandardCharsets.UTF_8)
262+
? StandardCharsets.ISO_8859_1
263+
: StandardCharsets.UTF_8;
264+
265+
// Launch a child; its behavior is not interesting and is ignored
266+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
267+
"ReaderWriterTest$ChildWithCharset");
268+
269+
Process p = pb.start();
270+
try {
271+
var writer = p.outputWriter(cs);
272+
writer = p.outputWriter(cs); // try again with same
273+
writer = p.outputWriter(otherCharset); // this should throw
274+
Assert.fail("Process.outputWriter(otherCharset) did not throw IllegalStateException");
275+
} catch (IllegalStateException ile) {
276+
// expected, ignore
277+
System.out.println(ile);
278+
}
279+
try {
280+
var reader = p.inputReader(cs);
281+
reader = p.inputReader(cs); // try again with same
282+
reader = p.inputReader(otherCharset); // this should throw
283+
Assert.fail("Process.inputReader(otherCharset) did not throw IllegalStateException");
284+
} catch (IllegalStateException ile) {
285+
// expected, ignore
286+
System.out.println(ile);
287+
}
288+
try {
289+
var reader = p.errorReader(cs);
290+
reader = p.errorReader(cs); // try again with same
291+
reader = p.errorReader(otherCharset); // this should throw
292+
Assert.fail("Process.errorReader(otherCharset) did not throw IllegalStateException");
293+
} catch (IllegalStateException ile) {
294+
// expected, ignore
295+
System.out.println(ile);
296+
}
297+
298+
p.destroyForcibly();
299+
try {
300+
// Collect the exit status to cleanup after the process; but ignore it
301+
p.waitFor();
302+
} catch (InterruptedException ie) {
303+
// Ignored
304+
}
305+
}
306+
307+
private static void checkReader(BufferedReader reader, Charset cs, String label) throws IOException {
308+
try (BufferedReader in = reader) {
309+
String prefix = " " + label + ": ";
310+
String firstline = in.readLine();
311+
System.out.append(prefix).println(firstline);
312+
String secondline = in.readLine();
313+
System.out.append(prefix).println(secondline);
314+
for (String line = in.readLine(); line != null; line = in.readLine()) {
315+
System.out.append(prefix).append(line);
316+
System.out.println();
317+
}
318+
ByteBuffer bb = cs.encode(TESTCHARS);
319+
String reencoded = cs.decode(bb).toString();
320+
if (!firstline.equals(reencoded))
321+
diffStrings(firstline, reencoded);
322+
assertEquals(firstline, reencoded, label + " Test Chars");
323+
324+
bb = cs.encode(ROUND_TRIP_TESTCHARS);
325+
reencoded = cs.decode(bb).toString();
326+
if (!secondline.equals(reencoded))
327+
diffStrings(secondline, reencoded);
328+
assertEquals(secondline, reencoded, label + " Round Trip Test Chars");
329+
}
330+
}
331+
332+
/**
333+
* A cleaned up Charset name that is suitable for Linux LANG environment variable.
334+
* If there are two '-'s the first one is removed.
335+
* @param cs a Charset
336+
* @return the cleanedup Charset name
337+
*/
338+
private static String cleanCharsetName(Charset cs) {
339+
String name = cs.name();
340+
int ndx = name.indexOf('-');
341+
if (ndx >= 0 && name.indexOf('-', ndx + 1) >= 0) {
342+
name = name.substring(0, ndx) + name.substring(ndx + 1);
343+
}
344+
return name;
345+
}
346+
347+
private static void diffStrings(String actual, String expected) {
348+
if (actual.equals(expected))
349+
return;
350+
int lenDiff = expected.length() - actual.length();
351+
if (lenDiff != 0)
352+
System.out.println("String lengths: " + actual.length() + " != " + expected.length());
353+
int first; // find first mismatched character
354+
for (first = 0; first < Math.min(actual.length(), expected.length()); first++) {
355+
if (actual.charAt(first) != expected.charAt(first))
356+
break;
357+
}
358+
int last;
359+
for (last = actual.length() - 1; last >= 0 && (last + lenDiff) >= 0; last--) {
360+
if (actual.charAt(last) != expected.charAt(last + lenDiff))
361+
break; // last mismatched character
362+
}
363+
System.out.printf("actual vs expected[%3d]: 0x%04x != 0x%04x%n", first, (int)actual.charAt(first), (int)expected.charAt(first));
364+
System.out.printf("actual vs expected[%3d]: 0x%04x != 0x%04x%n", last, (int)actual.charAt(last), (int)expected.charAt(last));
365+
System.out.printf("actual [%3d-%3d]: %s%n", first, last, actual.substring(first, last+1));
366+
System.out.printf("expected[%3d-%3d]: %s%n", first, last, expected.substring(first, last + lenDiff + 1));
367+
}
368+
369+
static class ChildWithCharset {
370+
public static void main(String[] args) {
371+
String nativeEncoding = System.getProperty("native.encoding");
372+
System.out.println(TESTCHARS);
373+
byte[] bytes = null;
374+
try {
375+
bytes = System.in.readAllBytes();
376+
System.out.write(bytes); // echo bytes back to parent on stdout
377+
} catch (IOException ioe) {
378+
ioe.printStackTrace(); // Seen by the parent
379+
}
380+
System.out.println("native.encoding: " + nativeEncoding);
381+
System.out.println("sun.stdout.encoding: " + System.getProperty("sun.stdout.encoding"));
382+
System.out.println("LANG: " + System.getenv().get("LANG"));
383+
384+
System.err.println(TESTCHARS);
385+
try {
386+
System.err.write(bytes); // echo bytes back to parent on stderr
387+
} catch (IOException ioe) {
388+
ioe.printStackTrace(); // Seen by the parent
389+
}
390+
System.err.println("native.encoding: " + nativeEncoding);
391+
System.err.println("sun.stderr.encoding: " + System.getProperty("sun.stderr.encoding"));
392+
}
393+
}
394+
}

0 commit comments

Comments
 (0)
Please sign in to comment.