FileDocCategorySizeDatePackage
ByteBufferInputStream.javaAPI DocGlassfish v2 API9244Mon Jul 09 13:46:46 BST 2007com.sun.enterprise.web.connector.grizzly

ByteBufferInputStream

public class ByteBufferInputStream extends InputStream
This class implement IO stream operations on top of a ByteBuffer. Under the hood, this class use a temporary Selector pool to for reading bytes when the client ask for more and the current Selector is not yet ready.
author
Jeanfrancois Arcand

Fields Summary
private static int
defaultReadTimeout
protected ByteBuffer
byteBuffer
The wrapped ByteBuffer
protected SelectionKey
key
The SelectionKey used by this stream.
protected int
readTimeout
The time to wait before timing out when reading bytes
protected int
readTry
Number of times to retry before return EOF
Constructors Summary
public ByteBufferInputStream()

    
    // ------------------------------------------------- Constructor -------//
    
    
      
    
public ByteBufferInputStream(ByteBuffer byteBuffer)

        this.byteBuffer = byteBuffer;
    
Methods Summary
public intavailable()
Return the available bytes

return
the wrapped byteBuffer.remaining()

        return (byteBuffer.remaining());
    
public voidclose()
Close this stream.

    
protected intdoRead()
Read bytes using the read ReadSelector

        if ( key == null ) return -1;
        
        byteBuffer.clear();
        int count = 1;
        int byteRead = 0;
        Selector readSelector = null;
        SelectionKey tmpKey = null;
        
        try{
            SocketChannel socketChannel = (SocketChannel)key.channel();
            while (count > 0){
                count = socketChannel.read(byteBuffer);
                if ( count > -1 )
                    byteRead += count;
                else
                    byteRead = count;
            }
            
            if ( byteRead == 0 ){
                readSelector = SelectorFactory.getSelector();
                
                if ( readSelector == null ){
                    return 0;
                }
                count = 1;
                tmpKey = socketChannel
                        .register(readSelector,SelectionKey.OP_READ);
                tmpKey.interestOps(tmpKey.interestOps() | SelectionKey.OP_READ);
                int code = readSelector.select(readTimeout);
                tmpKey.interestOps(
                        tmpKey.interestOps() & (~SelectionKey.OP_READ));
                
                if ( code == 0 ){
                    return 0; // Return on the main Selector and try again.
                }
                
                while (count > 0){
                    count = socketChannel.read(byteBuffer);
                    if ( count > -1 )
                        byteRead += count;
                    else
                        byteRead = count;
                }
            }
        } finally {
            if (tmpKey != null)
                tmpKey.cancel();
            
            if ( readSelector != null){
                // Bug 6403933
                try{
                    readSelector.selectNow();
                } catch (IOException ex){
                    ;
                }
                SelectorFactory.returnSelector(readSelector);
            }

            byteBuffer.flip();
        }
        return byteRead;
    
public java.nio.ByteBuffergetByteBuffer()
Get the wrapped ByteBuffer

        return  byteBuffer;
    
public static intgetDefaultReadTimeout()

        return defaultReadTimeout;
    
public intgetReadTimeout()
Return the timeout between two consecutives Selector.select() when a temporary Selector is used.

        return readTimeout;
    
public booleanmarkSupported()
Return true if mark is supported.

        return false;
    
public intread(byte[] b, int offset, int length)
Read the first byte of the wrapped ByteBuffer.

        if (!byteBuffer.hasRemaining()) {
            int eof = 0;
            for (int i=0; i < readTry; i++) {
                eof = doRead();
                
                if ( eof != 0 ){
                    break;
                }
            }
            
            if (eof <= 0){
                return -1;
            }
        }
        
        if (length > byteBuffer.remaining()) {
            length = byteBuffer.remaining();
        }
        byteBuffer.get(b, offset, length);
        
        return (length);
    
public intread(java.nio.ByteBuffer bb)
Read the first byte of the wrapped ByteBuffer.

        if (!byteBuffer.hasRemaining()) {
            int eof = 0;
            for (int i=0; i < readTry; i++) {
                eof = doRead();
                
                if ( eof != 0 ){
                    break;
                }
            }
            
            if (eof <= 0){
                return -1;
            }
        }
        bb.put(byteBuffer);
        
        return bb.position();
    
public intread()
Read the first byte from the wrapped ByteBuffer.

        if (!byteBuffer.hasRemaining()){
            int eof = 0;
            for (int i=0; i < readTry; i++) {
                eof = doRead();
                if ( eof != 0 ){
                    break;
                }
            }
        }
        return (byteBuffer.hasRemaining() ? (byteBuffer.get() & 0xff): -1);
    
public intread(byte[] b)
Read the bytes from the wrapped ByteBuffer.

        return (read(b, 0, b.length));
    
public voidrecycle()
Recycle this object.

        byteBuffer = null;
        key = null;
        readTimeout = defaultReadTimeout;
    
public voidsetByteBuffer(java.nio.ByteBuffer byteBuffer)
Set the wrapped ByteBuffer

param
byteBuffer The wrapped byteBuffer

        this.byteBuffer = byteBuffer;
    
public static voidsetDefaultReadTimeout(int aDefaultReadTimeout)

        defaultReadTimeout = aDefaultReadTimeout;
    
public voidsetReadTimeout(int rt)
Set the timeout between two consecutives Selector.select() when a temporary Selector is used.

        readTimeout = rt;
    
public voidsetSelectionKey(java.nio.channels.SelectionKey key)
Set the SelectionKey used to reads bytes.

        this.key = key;