The Pipe class creates a pair of Channel
The amount of data the pipeline holds is implementation-dependent. The only guarantee is that the bytes written to the SinkChannel will reappear on the SourceChannel in the same order. Example 3-11 demonstrates how pipes are used. Example 3-11. Worker thread writing to a pipe package com.ronsoft.books.nio.channels; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.nio.channels.Pipe; import java.nio.channels.Channels; import java.util.Random; /** * Test Pipe objects using a worker thread. * * Created April, 2002 * @author Ron Hitchens (ron@ronsoft.com) */ public class PipeTest{ public static void main (String [] argv) throws Exception { // Wrap a channel around stdout WritableByteChannel out = Channels.newChannel (System.out); // Start worker and get read end of channel ReadableByteChannel workerChannel = startWorker (10); ByteBuffer buffer = ByteBuffer.allocate (100); while (workerChannel.read (buffer) >= 0) { buffer.flip(); out.write (buffer); buffer.clear(); } } // This method could return a SocketChannel or // FileChannel instance just as easily private static ReadableByteChannel startWorker (int reps) throws Exception { Pipe pipe = Pipe.open(); Worker worker = new Worker (pipe.sink(), reps); worker.start(); return (pipe.source()); } // —————————————————————– /** * A worker thread object which writes data down a channel. 124
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services