Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Resource streaming status trick
// Inside a Restlet Resource's represent(...) method
PipedInputStream pi = new PipedInputStream();
PipedOutputStream po = new PipedOutputStream(pi);
Representation ir = new OutputRepresentation(MediaType.TEXT_PLAIN){
public void write(OutputStream realOutput) throws IOException {
byte[] b = new byte[8];
int read;
while ((read = != -1) {
realOutput.write(b, 0, read);
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