/** * Returns a reader from a writer representation.Internally, it uses a * writer thread and a pipe stream. * * @param representation * The representation to read from. * @return The character reader. * @throws IOException */ public static Reader getReader(final WriterRepresentation representation) throws IOException { final PipedWriter pipedWriter = new PipedWriter(); final PipedReader pipedReader = new PipedReader(pipedWriter); final Application application = Application.getCurrent(); // Gets a thread that will handle the task of continuously // writing the representation into the input side of the pipe application.getTaskService().execute(new Runnable() { public void run() { try { representation.write(pipedWriter); pipedWriter.close(); } catch (IOException ioe) { Context.getCurrentLogger().log(Level.FINE, "Error while writing to the piped reader.", ioe); } } }); return pipedReader; }
application.getTaskService().execute(new Runnable() { public void run() { try {
/** * 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 { final Pipe pipe = Pipe.open(); final Application application = Application.getCurrent(); // Get a thread that will handle the task of continuously // writing the representation into the input side of the pipe application.getTaskService().execute(new Runnable() { public void run() { try { final 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); } } }); return pipe.source(); }
if (getTaskService() != null) { getTaskService().start();
if (getTaskService() != null) { getTaskService().stop();