Using ordinary stream writers/readers with piped streams
<p>Consider the following test code. </p>

<p>I am trying to find out if I can use piped streams like "normal" I/O streams, together with the commonly used Reader and Writer implementations (specifically, another part of the code base I am working on demands that I use OutputStreamWriter).</p>

<p>The problem here is that nothing appears to show up on the read end. The program at least appears to correctly write the message to the write-end of the pipe, but when trying to read from the other end I block indefinetly, or if I (as in this case) check for available bytes, the call returns 0.</p>

<p>What am I doing wrong?</p>

<pre><code>public class PipeTest {

private InputStream input;
private OutputStream output;

public PipeTest() throws IOException {

    input = new PipedInputStream();
    output = new PipedOutputStream((PipedInputStream)input);

}

public void start() {

    Stuff1 stuff1 = new Stuff1(input);
    Stuff2 stuff2 = new Stuff2(output);

    Thread thread = new Thread(stuff1);
    thread.start();
    Thread thread2 = new Thread(stuff2);
    thread2.start();
}

public static void main(String[] args) throws IOException {

    new PipeTest().start();
}

private static class Stuff1 implements Runnable {

    InputStream inputStream;

    public Stuff1(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public void run() {

        String message;
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            //message = reader.readLine();
            System.out.println("Got message!");
            System.out.println(inputStream.available());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

private static class Stuff2 implements Runnable {

    OutputStream outputStream;

    public Stuff2(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    @Override
    public void run() {

        String message = "Hej!!\n";

        OutputStreamWriter writer = new OutputStreamWriter(outputStream);
        try {
            writer.write(message);
            System.out.println("Wrote message!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}
</code></pre>
