jsr340-experts@servlet-spec.java.net

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

From: Shing Wai Chan <shing.wai.chan_at_oracle.com>
Date: Wed, 14 Sep 2011 19:15:17 -0700

The following may be similar to JDK 7:

public interface javax.servlet.ServletReadableByteChannel extends
AysnchronousChannel {
     Future<Integer> read(ByteBuffer dst);

<A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer,?
super A> handler)
}

public interface javax.servlet.ServletWritableChannel extends
AsynchronousChannel {
     Future<Integer> write(ByteBuffer src)

<A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer,?
super A> handler)
}

public class javax.servlet.Request {
     ...
     public AsynchronousByteChannel getServletReadableByteChannel();
}

public class javax.servlet.Response {
     ...
     public AsynchronousByteChannel getServletWritableByteChannel();
}


Besides having a channel, the main difference with below is in
ByteBufferenceReference.
How do we know what ByteBuffer to allocate in this case?

Shing Wai Chan


On 9/14/11 5:46 PM, Greg Wilkins wrote:
> 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);