You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassCommandUtilsTest {
@TestpublicvoidtestPingCommandReadStreamOutput() throwsIOException, InterruptedException {
// Create receivers to capture stdout and stderrPipedOutputStreampipedStdout = newPipedOutputStream();
PipedInputStreampipedStdoutInput = newPipedInputStream(pipedStdout);
// Start the thread that writes stdout to the PipedOutputStreamThreadstdoutThread = newThread(() -> {
try {
newProcessExecutor().command("ping", "www.google.com", "-c",
"5")
.redirectOutput(pipedStdout)
.execute();
pipedStdout.close();
} catch (IOExceptione) {
// TODO Auto-generated catch blocke.printStackTrace();
} catch (InterruptedExceptione) {
// TODO Auto-generated catch blocke.printStackTrace();
} catch (TimeoutExceptione) {
// TODO Auto-generated catch blocke.printStackTrace();
}
});
// Start the threads to capture the output from the PipedInputStreamThreadreaderThread = newThread(() -> {
try (BufferedReaderreader = newBufferedReader(newInputStreamReader(pipedStdoutInput))) {
Stringline;
while ((line = reader.readLine()) != null) {
// Print to console as it is read (for real-time output)System.out.println(line);
}
} catch (IOExceptione) {
e.printStackTrace();
} finally {
}
});
// Start the stdout capturing threadstdoutThread.start();
readerThread.start();
// Now, wait for the threads to finishreaderThread.join();
stdoutThread.join();
}
@TestpublicvoidtestPipeOutputConnected2Input() throwsIOException, InterruptedException {
finalPipedOutputStreamoutput = newPipedOutputStream();
finalPipedInputStreaminput = newPipedInputStream(output);
Threadthread1 = newThread(newRunnable() {
@Overridepublicvoidrun() {
try {
output.write("Hello Piped Streams!! Used for Inter Thread Communication".getBytes());
output.close();
} catch (IOExceptionio) {
io.printStackTrace();
}
}
});
Threadthread2 = newThread(newRunnable() {
@Overridepublicvoidrun() {
try (BufferedReaderreader = newBufferedReader(newInputStreamReader(input))) {
Stringline;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOExceptionio) {
io.printStackTrace();
}
}
});
thread1.start();
thread2.start();
}
}
why testPingCommandReadStreamOutput raise error
testPipeOutputConnected2Input well done!
java.io.IOException: Write end dead
at java.io.PipedInputStream.read(PipedInputStream.java:310)
PipedInputStream.java:310
at java.io.PipedInputStream.read(PipedInputStream.java:377)
PipedInputStream.java:377
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
StreamDecoder.java:284
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
StreamDecoder.java:326
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
StreamDecoder.java:178
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
BufferedReader.java:161
at java.io.BufferedReader.readLine(BufferedReader.java:324)
BufferedReader.java:324
at java.io.BufferedReader.readLine(BufferedReader.java:389)
CommandUtilsTest.java:163
at java.lang.Thread.run(Thread.java:750)
The text was updated successfully, but these errors were encountered:
There indeed seems to be an issue when using stream redirection with PipedOutputStream -- zt-exec seems to shut the writing thread down before closing the stream but PipedOutputStream needs the opposite to happen.
why testPingCommandReadStreamOutput raise error
testPipeOutputConnected2Input well done!
The text was updated successfully, but these errors were encountered: