Skip to content

Instantly share code, notes, and snippets.

@robheittman
Created November 16, 2010 01:26
Show Gist options
  • Save robheittman/701288 to your computer and use it in GitHub Desktop.
Save robheittman/701288 to your computer and use it in GitHub Desktop.
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){
@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