FileDocCategorySizeDatePackage
CometReader.javaAPI DocGlassfish v2 API4507Tue Jul 10 13:20:24 BST 2007com.sun.enterprise.web.connector.grizzly.comet

CometReader

public class CometReader extends Object
Non blocking IO reader. This class can be used from a CometHandler to execute non blocking read. This is usefull when the client is pipelining data. The CometHandler will be notified as soon as bytes are arriving. CometHandler who wants to be notified just need to register themself by calling CometContext.registerAsyncRead()
author
Jeanfrancois Arcand

Fields Summary
private SocketChannel
socketChannel
The non blocking channel.
private ByteBuffer
byteBuffer
The ByteBuffer used to execute the first read in CometTask.
private int
nRead
How many bytes have we read.
private boolean
ready
Is this CometReader ready
Constructors Summary
public CometReader()

            
    
      
    
Methods Summary
public booleanisReady()
Return true if this instance is ready to read.

        return ready;
    
public intread(byte[] buf)
Read bytes without blocking.

        return read(buf,0,buf.length);
    
public intread(byte[] buf, int off, int len)
Read bytes without blocking.

        if (!ready){
            throw new IllegalStateException("This CometReader is no longer usable");
        }
        // The CometTask has already read the first bytes for us.
        if (byteBuffer != null){           
            byteBuffer.get(buf,off,len);
            return nRead > len ? len : nRead;
        }
        return socketChannel.read(ByteBuffer.wrap(buf,off,len));
    
public voidrecycle()
Recycle this object.

        nRead = 0;
        byteBuffer = null;
        socketChannel = null;
    
public voidsetByteBuffer(java.nio.ByteBuffer byteBuffer)

        this.byteBuffer = byteBuffer;
    
protected voidsetChannel(java.nio.channels.SocketChannel socketChannel)
Set the underlying SocketChannel.

      
        this.socketChannel = socketChannel;
    
public voidsetNRead(int nRead)

        this.nRead = nRead;
    
public voidsetReady(boolean ready)
false if this instance is no longer ready to read.

        this.ready = ready;