/** * Returns a readable byte channel based on the given representation's content and its * write(WritableByteChannel) method. Internally, it uses a writer thread and a pipe channel. * * @param representation the representation to get the {@link OutputStream} from. * @return A readable byte channel. * @throws IOException */ public static ReadableByteChannel getChannel(final Representation representation) throws IOException { ReadableByteChannel result = null; if (Edition.CURRENT != Edition.GAE) { final java.nio.channels.Pipe pipe = java.nio.channels.Pipe.open(); org.restlet.Application application = org.restlet.Application.getCurrent(); // Get a thread that will handle the task of continuously // writing the representation into the input side of the pipe Runnable task = new Runnable() { public void run() { try { WritableByteChannel wbc = pipe.sink(); representation.write(wbc); wbc.close(); } catch (IOException ioe) { Context.getCurrentLogger() .log(Level.FINE, "Error while writing to the piped channel.", ioe); } } }; if (application != null && application.getTaskService() != null) { application.getTaskService().execute(task); } else { new Thread(task).start(); } result = pipe.source(); } else { Context.getCurrentLogger() .log( Level.WARNING, "The GAE edition is unable to return a channel for a representation given its write(WritableByteChannel) method."); } return result; }