jsr340-experts@servlet-spec.java.net

[jsr340-experts] Re: Initial draft of NIO proposal

From: Greg Wilkins <gregw_at_intalio.com>
Date: Thu, 15 Sep 2011 10:46:10 +1000

Here is a suggestion of how we could add jdk7 inspired reads to the
ServletInputStream, but also allow the buffer pre-allocation to be
avoided.

interface ByteBufferReference
{
   ByteBuffer getBuffer();
}

abstract class ServletInputStream
{
   ...
   abstract Future<Integer> read(ByteBuffer dst);
   abstract Future<Integer> read(ByteBufferReference dst);
   abstract <A> void read(ByteBuffer dst, A attachment,
CompletionHandler<Integer,? super A> handler);
   abstract <A> void read(ByteBufferReference dst, A attachment,
CompletionHandler<Integer,? super A> handler);
}

Simple usage would be possible with the preallocation provided by asDest:

 ServletInputStream in=request.getInputStream();
 byte[] buf = new byte[4096];
 Future<Integer> result = in.read(ByteBuffer.wrap(buf,0,buf.length))

 // do something else....

 result.get(10,TimeUnit.SECONDS);