title: Is it possible to read the Process stdout InputStream into an NIO ByteBuffer?
body: <p>Is it possible to use NIO to process the stdout from a Process? I have it working with java.io, but this is something of an exercise to learn a bit more about NIO and to explore the possibility of performance improvements. </p>
  
  <p>Basically I want to stream a large volume of text from stdout into a buffer as fast as possible without blocking, and then process the contents of that buffer later. The trouble is, I can't seem to figure out the right voodoo to make it work with NIO. This is where I am right now: </p>
  
  <pre><code>ProcessBuilder pb = new ProcessBuilder( ... );
  Process p = pb.start();
  stdout = new StreamConsumer(p.getInputStream());
  new Thread(stdout).start();
  // other stuff omitted for brevity
  </code></pre>
  
  <p>The class StreamConsumer looks like this:</p>
  
  <pre><code>class StreamConsumer implements Runnable
  {
    private InputStream is;
  
    public StreamConsumer(InputStream is)
    {
      this.is = is;
    }
  
    public void run()
    {
      try
      {
        ReadableByteChannel source = Channels.newChannel(is);
  
        // Is it possible get a channel to a ByteBuffer 
        // or MappedByteBuffer here?
        WritableByteChannel destination = ??;
        ByteBuffer buffer = ByteBuffer.allocateDirect(128 * 1024);
  
        while (source.read(buffer) != -1)
        {
          buffer.flip();
          while (buffer.hasRemaining())
          {
            destination.write(buffer);
          }
          buffer.clear();
        }
  
        source.close();
        destination.close();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
  }
  </code></pre>