I currently have the following sample code that I am trying to convert OutputStream to InputStream, which I got the idea from Method 2
in http://blog.ostermiller.org/convert-java-outputstream-inputstream
But my question here is, the save method could throw IOException, and I would like to catch that and re-throw that as part of this getInputStream method.
I am trying to wrap the IOException thrown by save(out) to a runtime exception, but I know that this runtime exception cannot be caught by the parent thread. So I am stuck on this, can anyone point me some directions?
public InputStream getInputStream() throws IOException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
Thread t = new Thread(new Runnable() {
public void run () {
try {
save(out); // this save method can throw IOException
} catch (IOException e) {
throw new RuntimeException("Failed to save entries to output stream", e);
}
}
});
t.start();
return in;
}
private void save(OutputStream out) throws IOException {...}
I have read How to catch an Exception from a thread but felt my question is still different, because I want to re-throw the exception in parent thread, the question above solves the problem that catches the exception only