FileDocCategorySizeDatePackage
StreamAlgorithmBase.javaAPI DocGlassfish v2 API11269Fri May 04 22:37:06 BST 2007com.sun.enterprise.web.connector.grizzly.algorithms

StreamAlgorithmBase

public abstract class StreamAlgorithmBase extends Object implements com.sun.enterprise.web.connector.grizzly.StreamAlgorithm
Base class for StreamAlgorithm implementation.
author
Jeanfrancois Arcand

Fields Summary
protected static boolean
embeddedInGlassFish
Is Grizzly embedded in GlassFish.
private int
port
protected int
contentLength
The actual length of the stream we are able to read.
protected int
curLimit
The ByteBuffer current limit value
protected int
curPosition
The ByteBuffer current position value
protected int
headerLength
The position, within the ByteBuffer, when the HTTP headers are completely reads.
protected int
lastStatePosition
In case we were'nt able to parse the request line, we will continue parsing from that position.
protected int
state
If the stream wasn't read fully, keep the state of the http parsing.
protected ByteBuffer
primaryByteBuffer
If a new ByteBuffer is created because the stream is too small, cache the original byteBuffer and reuse it once the transaction has completed.
protected boolean
useByteBufferView
If true, use a ByteBuffer view instead of ByteBuffer
protected boolean
useDirectByteBuffer
Are we using direct ByteBuffer
protected SocketChannel
socketChannel
The SocketChannel associated with this algorithm.
protected com.sun.enterprise.web.connector.grizzly.Handler
handler
An Handler implementation used to implement a static resources cache.
Constructors Summary
public StreamAlgorithmBase()


    
      
        handler = new DummyHandler();
    
Methods Summary
public java.nio.ByteBufferallocate(boolean useDirectByteBuffer, boolean useByteBufferView, int size)
Allocate a ByteBuffer

param
useDirectByteBuffer to create a direct ByteBuffer
param
useByteBufferView to create ByteBuffer view
param
size the size of the ByteBuffer
return
a ByteBuffer

        ByteBuffer byteBuffer;
        if (useByteBufferView){
            byteBuffer = ByteBufferFactory.allocateView(size,
                                                        useDirectByteBuffer);
        } else if ( useDirectByteBuffer ){
            byteBuffer = ByteBuffer.allocateDirect(size);            
        } else {
            byteBuffer = ByteBuffer.allocate(size);                       
        }
        return byteBuffer;
    
public intcontentLength()
Return the stream content-length. If the content-length wasn't parsed, return -1.

        return contentLength;
    
protected java.lang.Stringdump(java.nio.ByteBuffer byteBuffer)
Dump the ByteBuffer content. This is used only for debugging purpose.

                   
        ByteBuffer dd = byteBuffer.duplicate();
        dd.flip();
        
        int length = dd.limit();
        byte[] dump = new byte[length];
        dd.get(dump,0,length);
        return(new String(dump) + "\n----------------------------" + dd 
        + "\ncontentLength: " + contentLength 
        + "\nheaderLength: " + headerLength); 
    
public intgetPort()
Return the port

        return port;
    
public java.lang.ClassgetReadTask(com.sun.enterprise.web.connector.grizzly.SelectorThread selectorThread)
Return the full name of the class responsible for handling OP_READ.

        if ( selectorThread.getMaxReadWorkerThreads() <= 0 
                && !selectorThread.getEnableAsyncExecution())
            return com.sun.enterprise.web.connector.grizzly.DefaultReadTask.class;
        else 
            return com.sun.enterprise.web.connector.grizzly.AsyncReadTask.class;            
    
public intheaderLength()
Return the stream header length. The header length is the length between the start of the stream and the first occurance of character '\r\n' .

        return headerLength;
    
public abstract booleanparse(java.nio.ByteBuffer byteBuffer)
Manipulate the bytes stream and determine if the process can continue.

return
true if the algorithm determines the process can continue.

public java.nio.ByteBufferpostParse(java.nio.ByteBuffer byteBuffer)
After parsing the bytes, post process the ByteBuffer

param
byteBuffer the ByteBuffer used by this algorithm
return
ByteBuffer used by this algorithm

        // Swap buffer to its original size.
        if ( primaryByteBuffer != null) {
            primaryByteBuffer.clear();
            byteBuffer = primaryByteBuffer;
            primaryByteBuffer = null;
        }
        return byteBuffer;
    
public java.nio.ByteBufferpreParse(java.nio.ByteBuffer byteBuffer)
Before parsing the bytes, initialize and prepare the algorithm.

param
byteBuffer the ByteBuffer used by this algorithm
return
ByteBuffer used by this algorithm

        if (byteBuffer.position() == byteBuffer.capacity()){
            // Add space at the end for \n\r
            int bufferSize = contentLength > 0 ?
                                    contentLength + headerLength + 5: 
                                    byteBuffer.capacity() * 2;
            byteBuffer = swapBuffer(byteBuffer,bufferSize);
        }  
        return byteBuffer;
    
public voidrecycle()
Reset this object to its default state.

        contentLength = -1;
        lastStatePosition= -1;
        headerLength = -1; 
        curLimit = -1;
        curPosition = -1;
        state = 0;
    
public java.nio.ByteBufferrollbackParseState(java.nio.ByteBuffer byteBuffer)
Rollback the ByteBuffer to its previous state in case an error as occured.

        /**
         * Set the <code>ByteBuffer</code> position/limit to the value
         * they were before trying to process the stream.
         */
        if (curLimit != -1 && curPosition != -1){
            byteBuffer.limit(curLimit);
            byteBuffer.position(curPosition);

            // Reset so we can process a new request.
            contentLength = -1;
            headerLength = -1;  
        }
        return byteBuffer;
    
public voidsetPort(int port)
Set the port

        this.port = port;
    
public voidsetSocketChannel(java.nio.channels.SocketChannel socketChannel)
The SocketChannel used by this class.

        this.socketChannel = socketChannel;
        if ( socketChannel != null)
            handler.attachChannel(socketChannel); 
    
private java.nio.ByteBufferswapBuffer(java.nio.ByteBuffer byteBuffer, int size)
Allocate a new ByteBuffer and put the content of the current one into it.

        ByteBuffer tmp = allocate(useDirectByteBuffer,useByteBufferView, size);

        byteBuffer.flip();
        tmp.put(byteBuffer);
        
        // Keep a pointer to the original one.
        if ( primaryByteBuffer == null) {
            primaryByteBuffer = byteBuffer;
        }
        byteBuffer = tmp;
        return byteBuffer;