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);