com.sun.grizzly.http.algorithms
Class StreamAlgorithmBase

java.lang.Object
  extended by com.sun.grizzly.http.algorithms.StreamAlgorithmBase
All Implemented Interfaces:
StreamAlgorithm<SocketChannel>
Direct Known Subclasses:
ContentLengthAlgorithm, NoParsingAlgorithm, StateMachineAlgorithm, StaticStreamAlgorithm

public abstract class StreamAlgorithmBase
extends Object
implements StreamAlgorithm<SocketChannel>

Base class for StreamAlgorithm implementation.

Author:
Jeanfrancois Arcand

Nested Class Summary
static class StreamAlgorithmBase.DummyHandler
          Empty handler used when Grizzly is bundled outside of GlassFish.
 
Field Summary
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  Interceptor handler
          An Interceptor implementation used to implement a static resources cache.
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  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  SocketChannel socketChannel
          The SocketChannel associated with this algorithm.
protected  int state
          If the stream wasn't read fully, keep the state of the http parsing.
protected  boolean useByteBufferView
          If true, use a ByteBuffer view instead of ByteBuffer
protected  boolean useDirectByteBuffer
          Are we using direct ByteBuffer
 
Constructor Summary
StreamAlgorithmBase()
           
 
Method Summary
 ByteBuffer allocate(boolean useDirectByteBuffer, boolean useByteBufferView, int size)
          Allocate a ByteBuffer
 int contentLength()
          Return the stream content-length.
protected  String dump(ByteBuffer byteBuffer)
          Dump the ByteBuffer content.
 int getPort()
          Return the port
 int headerLength()
          Return the stream header length.
abstract  boolean parse(ByteBuffer byteBuffer)
          Manipulate the bytes stream and determine if the process can continue.
 ByteBuffer postParse(ByteBuffer byteBuffer)
          After parsing the bytes, post process the ByteBuffer
 ByteBuffer preParse(ByteBuffer byteBuffer)
          Before parsing the bytes, initialize and prepare the algorithm.
 void recycle()
          Reset this object to its default state.
 ByteBuffer rollbackParseState(ByteBuffer byteBuffer)
          Rollback the ByteBuffer to its previous state in case an error as occured.
 void setChannel(SocketChannel socketChannel)
          The SocketChannel used by this class.
 void setPort(int port)
          Set the port
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sun.grizzly.util.StreamAlgorithm
getHandler
 

Field Detail

contentLength

protected int contentLength
The actual length of the stream we are able to read.


curLimit

protected int curLimit
The ByteBuffer current limit value


curPosition

protected int curPosition
The ByteBuffer current position value


headerLength

protected int headerLength
The position, within the ByteBuffer, when the HTTP headers are completely reads.


lastStatePosition

protected int lastStatePosition
In case we were'nt able to parse the request line, we will continue parsing from that position.


state

protected int state
If the stream wasn't read fully, keep the state of the http parsing.


primaryByteBuffer

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.


useByteBufferView

protected boolean useByteBufferView
If true, use a ByteBuffer view instead of ByteBuffer


useDirectByteBuffer

protected boolean useDirectByteBuffer
Are we using direct ByteBuffer


socketChannel

protected SocketChannel socketChannel
The SocketChannel associated with this algorithm.


handler

protected Interceptor handler
An Interceptor implementation used to implement a static resources cache.

Constructor Detail

StreamAlgorithmBase

public StreamAlgorithmBase()
Method Detail

parse

public abstract boolean parse(ByteBuffer byteBuffer)
Manipulate the bytes stream and determine if the process can continue.

Specified by:
parse in interface StreamAlgorithm<SocketChannel>
Parameters:
byteBuffer - the bytes read.
Returns:
true if the algorithm determines the process can continue.

contentLength

public int contentLength()
Return the stream content-length. If the content-length wasn't parsed, return -1.

Specified by:
contentLength in interface StreamAlgorithm<SocketChannel>
Returns:
content length or -1 if content length was not parsed

headerLength

public int headerLength()
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' .

Specified by:
headerLength in interface StreamAlgorithm<SocketChannel>
Returns:
header length

preParse

public ByteBuffer preParse(ByteBuffer byteBuffer)
Before parsing the bytes, initialize and prepare the algorithm.

Specified by:
preParse in interface StreamAlgorithm<SocketChannel>
Parameters:
byteBuffer - the ByteBuffer used by this algorithm
Returns:
ByteBuffer used by this algorithm

postParse

public ByteBuffer postParse(ByteBuffer byteBuffer)
After parsing the bytes, post process the ByteBuffer

Specified by:
postParse in interface StreamAlgorithm<SocketChannel>
Parameters:
byteBuffer - the ByteBuffer used by this algorithm
Returns:
ByteBuffer used by this algorithm

recycle

public void recycle()
Reset this object to its default state.

Specified by:
recycle in interface StreamAlgorithm<SocketChannel>

allocate

public ByteBuffer allocate(boolean useDirectByteBuffer,
                           boolean useByteBufferView,
                           int size)
Allocate a ByteBuffer

Specified by:
allocate in interface StreamAlgorithm<SocketChannel>
Parameters:
useDirectByteBuffer - to create a direct ByteBuffer
useByteBufferView - to create ByteBuffer view
size - the size of the ByteBuffer
Returns:
a ByteBuffer

rollbackParseState

public ByteBuffer rollbackParseState(ByteBuffer byteBuffer)
Rollback the ByteBuffer to its previous state in case an error as occured.

Specified by:
rollbackParseState in interface StreamAlgorithm<SocketChannel>
Returns:
ByteBuffer

dump

protected String dump(ByteBuffer byteBuffer)
Dump the ByteBuffer content. This is used only for debugging purpose.


setChannel

public void setChannel(SocketChannel socketChannel)
The SocketChannel used by this class.

Specified by:
setChannel in interface StreamAlgorithm<SocketChannel>

setPort

public void setPort(int port)
Set the port

Specified by:
setPort in interface StreamAlgorithm<SocketChannel>
Parameters:
port - port number

getPort

public int getPort()
Return the port

Specified by:
getPort in interface StreamAlgorithm<SocketChannel>
Returns:
port number being used


Copyright © 2013 Oracle Corporation. All Rights Reserved.