Reputation: 30127
Under what circumstances read end can be dead in couple PipedOutputStream
and PipedInputStream
? I am not closing any pipes.
Upvotes: 5
Views: 8603
Reputation: 691
I encountered java.io.IOException: Read end dead
in my code and found out the cause. Posting an example code below. You will get an "Read end dead" exception if you run the code. If you take a close look, the consumer thread reads "hello" from the stream and terminates; meanwhile the producer sleeps for 2 seconds and tries to write " world" but fails. A related problem explained here: http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/
class ReadEnd {
public static void main(String[] args) {
final PipedInputStream in = new PipedInputStream();
new Thread(new Runnable() { //consumer
@Override
public void run() {
try {
byte[] tmp = new byte[1024];
while (in.available() > 0) { // only once...
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}).start();
PipedOutputStream out = null;
try {
out = new PipedOutputStream(in);
out.write("hello".getBytes());
Thread.sleep(2 * 1000);
out.write(" world".getBytes()); //Exception thrown here
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
}
Upvotes: 5