Skip to content

Question: redirectOutput to outputstream #116

@Panlq

Description

@Panlq
public class CommandUtilsTest {
    @Test
    public void testPingCommandReadStreamOutput() throws IOException, InterruptedException {
        // Create receivers to capture stdout and stderr
        PipedOutputStream pipedStdout = new PipedOutputStream();
        PipedInputStream pipedStdoutInput = new PipedInputStream(pipedStdout);

        // Start the thread that writes stdout to the PipedOutputStream
        Thread stdoutThread = new Thread(() -> {
            try {
                new ProcessExecutor().command("ping", "www.google.com", "-c",
                        "5")
                        .redirectOutput(pipedStdout)
                        .execute();
                pipedStdout.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (TimeoutException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        });

        // Start the threads to capture the output from the PipedInputStream
        Thread readerThread = new Thread(() -> {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(pipedStdoutInput))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    // Print to console as it is read (for real-time output)
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }
        });

        // Start the stdout capturing thread
        stdoutThread.start();
        readerThread.start();

        // Now, wait for the threads to finish
        readerThread.join();
        stdoutThread.join();

    }

    @Test
    public void testPipeOutputConnected2Input() throws IOException, InterruptedException {
        final PipedOutputStream output = new PipedOutputStream();
        final PipedInputStream input = new PipedInputStream(output);

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    output.write("Hello Piped Streams!! Used for Inter Thread Communication".getBytes());
                    output.close();

                } catch (IOException io) {
                    io.printStackTrace();
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {

                try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException io) {
                    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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions