Created
November 16, 2010 01:26
-
-
Save robheittman/701288 to your computer and use it in GitHub Desktop.
Resource streaming status trick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inside a Restlet Resource's represent(...) method | |
PipedInputStream pi = new PipedInputStream(); | |
PipedOutputStream po = new PipedOutputStream(pi); | |
Representation ir = new OutputRepresentation(MediaType.TEXT_PLAIN){ | |
@Override | |
public void write(OutputStream realOutput) throws IOException { | |
byte[] b = new byte[8]; | |
int read; | |
while ((read = pi.read(b)) != -1) { | |
realOutput.write(b, 0, read); | |
realOutput.flush(); | |
} | |
} | |
}; | |
OutputStreamWriter ow = new OutputStreamWriter(po); | |
PrintWriter out = new PrintWriter(ow,true); | |
// ... | |
try { | |
new Thread(new LongRunningBeast(out)).start(); | |
return ir; | |
} catch (Exception e) { | |
// return some error representation | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment