com.sun.grizzly.http
Class SocketChannelOutputBuffer

java.lang.Object
  extended by com.sun.grizzly.tcp.http11.InternalOutputBuffer
      extended by com.sun.grizzly.http.SocketChannelOutputBuffer
All Implemented Interfaces:
FileOutputBuffer, OutputBuffer, ByteChunk.ByteOutputChannel
Direct Known Subclasses:
AsynchronousOutputBuffer, SSLAsyncOutputBuffer, SSLOutputBuffer

public class SocketChannelOutputBuffer
extends InternalOutputBuffer
implements FileOutputBuffer

Output buffer. Buffer the bytes until the ByteChunk is full or the request is completed.

Author:
Jean-Francois Arcand, Scott Oaks, Alexey Stashok

Nested Class Summary
protected static class SocketChannelOutputBuffer.AsyncWriteCallbackHandlerImpl
          AsyncWriteCallbackHandler implementation, which is responsible for returning cloned ByteBuffers to the pool
protected  class SocketChannelOutputBuffer.ByteBufferClonerImpl
          ByteBufferCloner implementation, which is called by Grizzly framework at the time, when asynchronous write queue can not write the buffer direcly on socket and instead will put it in queue.
 
Nested classes/interfaces inherited from class com.sun.grizzly.tcp.http11.InternalOutputBuffer
InternalOutputBuffer.OutputStreamOutputBuffer
 
Field Summary
protected static ByteBuffer ACK
          ACK static bytes.
protected  ByteBufferCloner asyncHttpByteBufferCloner
          ByteBufferCloner implementation, which is called by Grizzly framework at the time, when asynchronous write queue can not write the buffer direcly on socket and instead will put it in queue.
protected  AsyncQueueWriter asyncQueueWriter
          Asynchronous queue writer, which will be used if asyncHttp mode is enabled
protected static Queue<ByteBuffer> bufferPool
          ByteBuffer pool to be used with async write
protected  Channel channel
          Underlying output channel.
protected static int DEFAULT_BUFFER_POOL_SIZE
           
protected  boolean discardBytes
           
protected  boolean isAsyncHttpWriteEnabled
          Flag, which indicates if async HTTP write is enabled
protected static Logger logger
           
protected static int maxBufferedBytes
          Default max cached bytes.
protected static int maxBufferPoolSize
           
protected  ByteBuffer outputByteBuffer
          Underlying ByteByteBuffer
protected  SelectionKey selectionKey
          Underlying selection key of the output channel.
 
Fields inherited from class com.sun.grizzly.tcp.http11.InternalOutputBuffer
activeFilters, buf, committed, filterLibrary, finished, headers, lastActiveFilter, outputStream, outputStreamOutputBuffer, pos, response, socketBuffer, useSocketBuffer
 
Constructor Summary
SocketChannelOutputBuffer(Response response, int sendBufferSize, boolean useSocketBuffer)
          Alternate constructor.
 
Method Summary
protected  ByteBuffer createByteBuffer(int size)
          Create the output ByteBuffer
 void discardUpstreamBytes()
          Stop buffering bytes, discard any upcoming writes.
 void endRequest()
          End request.
 void flush()
          Flush the buffered bytes,
 void flushBuffer()
          Writes bytes to the underlying channel.
 void flushChannel(ByteBuffer bb)
          Flush the buffer by looping until the ByteBuffer is empty
protected  AsyncQueueWriter getAsyncQueueWriter()
          Gets the asynchronous queue writer, which will be used if asyncHttp mode is enabled
 Channel getChannel()
          Return the underlying SocketChannel
static int getMaxBufferedBytes()
          Return the maximum of buffered bytes.
static int getMaxBufferPoolSize()
          Return the maximum number of cached ByteBuffer
 ByteBuffer getOutputByteBuffer()
           
 SelectionKey getSelectionKey()
          Gets the underlying selection key of the output channel.
 boolean isAsyncHttpWriteEnabled()
          Is async HTTP write enabled.
 boolean isSupportFileSend()
          Returns true, if OutputBuffer supports file-send mode, or false otherwise.
 void realWriteBytes(byte[] cbuf, int off, int len)
          Callback to write data from the buffer.
 void recycle()
          Recycle the output buffer.
 void reset()
          Reset current response.
 void sendAck()
          Send an acknoledgement without buffering.
 long sendFile(FileChannel fileChannel, long position, long length)
          Transfers data from file channel to the socket channel directly.
 void setAsyncHttpWriteEnabled(boolean isAsyncHttpWriteEnabled)
          Set if async HTTP write enabled.
protected  void setAsyncQueueWriter(AsyncQueueWriter asyncQueueWriter)
          Sets the asynchronous queue writer, which will be used if asyncHttp mode is enabled
 void setChannel(Channel channel)
          Set the underlying socket output stream.
static void setMaxBufferedBytes(int aMaxBufferedBytes)
          Set the maximum number of bytes before flushing the ByteBuffer content.
static void setMaxBufferPoolSize(int size)
          Set the maximum size of cached ByteBuffer when async write is enabled.
 void setSelectionKey(SelectionKey selectionKey)
          Sets the underlying selection key of the output channel.
 
Methods inherited from class com.sun.grizzly.tcp.http11.InternalOutputBuffer
addActiveFilter, addFilter, addLastOutputFilter, clearFilters, commit, doWrite, endHeaders, getFilters, getOutputStream, nextRequest, sendHeader, sendHeader, sendHeader, sendStatus, setOutputStream, setSocketBuffer, write, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static Logger logger

DEFAULT_BUFFER_POOL_SIZE

protected static final int DEFAULT_BUFFER_POOL_SIZE
See Also:
Constant Field Values

maxBufferPoolSize

protected static int maxBufferPoolSize

bufferPool

protected static Queue<ByteBuffer> bufferPool
ByteBuffer pool to be used with async write


asyncHttpByteBufferCloner

protected final ByteBufferCloner asyncHttpByteBufferCloner
ByteBufferCloner implementation, which is called by Grizzly framework at the time, when asynchronous write queue can not write the buffer direcly on socket and instead will put it in queue. This implementation tries to get temporary ByteBuffer from the pool, if no ByteBuffer is available - then new one will be created.


channel

protected Channel channel
Underlying output channel.


selectionKey

protected SelectionKey selectionKey
Underlying selection key of the output channel.


isAsyncHttpWriteEnabled

protected boolean isAsyncHttpWriteEnabled
Flag, which indicates if async HTTP write is enabled


asyncQueueWriter

protected AsyncQueueWriter asyncQueueWriter
Asynchronous queue writer, which will be used if asyncHttp mode is enabled


outputByteBuffer

protected ByteBuffer outputByteBuffer
Underlying ByteByteBuffer


ACK

protected static final ByteBuffer ACK
ACK static bytes.


maxBufferedBytes

protected static int maxBufferedBytes
Default max cached bytes.


discardBytes

protected boolean discardBytes
Constructor Detail

SocketChannelOutputBuffer

public SocketChannelOutputBuffer(Response response,
                                 int sendBufferSize,
                                 boolean useSocketBuffer)
Alternate constructor.

Method Detail

createByteBuffer

protected ByteBuffer createByteBuffer(int size)
Create the output ByteBuffer


setChannel

public void setChannel(Channel channel)
Set the underlying socket output stream.


getChannel

public Channel getChannel()
Return the underlying SocketChannel


getSelectionKey

public SelectionKey getSelectionKey()
Gets the underlying selection key of the output channel.

Returns:
the underlying selection key of the output channel.

setSelectionKey

public void setSelectionKey(SelectionKey selectionKey)
Sets the underlying selection key of the output channel.

Parameters:
selectionKey - the underlying selection key of the output channel.

isAsyncHttpWriteEnabled

public boolean isAsyncHttpWriteEnabled()
Is async HTTP write enabled.

Returns:
true, if async HTTP write enabled, or false otherwise.

setAsyncHttpWriteEnabled

public void setAsyncHttpWriteEnabled(boolean isAsyncHttpWriteEnabled)
Set if async HTTP write enabled.

Parameters:
isAsyncHttpWriteEnabled - true, if async HTTP write enabled, or false otherwise.

getAsyncQueueWriter

protected AsyncQueueWriter getAsyncQueueWriter()
Gets the asynchronous queue writer, which will be used if asyncHttp mode is enabled

Returns:
The asynchronous queue writer, which will be used if asyncHttp mode is enabled

setAsyncQueueWriter

protected void setAsyncQueueWriter(AsyncQueueWriter asyncQueueWriter)
Sets the asynchronous queue writer, which will be used if asyncHttp mode is enabled

Parameters:
asyncQueueWriter - The asynchronous queue writer, which will be used if asyncHttp mode is enabled

sendAck

public void sendAck()
             throws IOException
Send an acknoledgement without buffering.

Overrides:
sendAck in class InternalOutputBuffer
Throws:
IOException

realWriteBytes

public void realWriteBytes(byte[] cbuf,
                           int off,
                           int len)
                    throws IOException
Callback to write data from the buffer.

Specified by:
realWriteBytes in interface ByteChunk.ByteOutputChannel
Overrides:
realWriteBytes in class InternalOutputBuffer
Throws:
IOException

flushChannel

public void flushChannel(ByteBuffer bb)
                  throws IOException
Flush the buffer by looping until the ByteBuffer is empty

Parameters:
bb - the ByteBuffer to write.
Throws:
IOException

isSupportFileSend

public boolean isSupportFileSend()
Returns true, if OutputBuffer supports file-send mode, or false otherwise.

Specified by:
isSupportFileSend in interface FileOutputBuffer
Returns:
true, if OutputBuffer supports file-send mode, or false otherwise.

sendFile

public long sendFile(FileChannel fileChannel,
                     long position,
                     long length)
              throws IOException
Transfers data from file channel to the socket channel directly.

Specified by:
sendFile in interface FileOutputBuffer
Parameters:
fileChannel - FileChannel
position - The position within the file at which the transfer is to begin; must be non-negative
length - The maximum number of bytes to be transferred; must be non-negative
Returns:
number of bytes tranferred
Throws:
IOException

flush

public void flush()
           throws IOException
Flush the buffered bytes,

Overrides:
flush in class InternalOutputBuffer
Throws:
IOException - an undelying I/O error occured

endRequest

public void endRequest()
                throws IOException
End request.

Overrides:
endRequest in class InternalOutputBuffer
Throws:
IOException - an undelying I/O error occured

flushBuffer

public void flushBuffer()
                 throws IOException
Writes bytes to the underlying channel.

Throws:
IOException

recycle

public void recycle()
Recycle the output buffer. This should be called when closing the connection.

Overrides:
recycle in class InternalOutputBuffer

reset

public void reset()
Reset current response.

Overrides:
reset in class InternalOutputBuffer
Throws:
IllegalStateException - if the response has already been committed

discardUpstreamBytes

public void discardUpstreamBytes()
Stop buffering bytes, discard any upcoming writes.


getMaxBufferedBytes

public static int getMaxBufferedBytes()
Return the maximum of buffered bytes.

Returns:

setMaxBufferedBytes

public static void setMaxBufferedBytes(int aMaxBufferedBytes)
Set the maximum number of bytes before flushing the ByteBuffer content.

Parameters:
aMaxBufferedBytes -

setMaxBufferPoolSize

public static void setMaxBufferPoolSize(int size)
Set the maximum size of cached ByteBuffer when async write is enabled.

Parameters:
size -

getMaxBufferPoolSize

public static int getMaxBufferPoolSize()
Return the maximum number of cached ByteBuffer

Returns:

getOutputByteBuffer

public ByteBuffer getOutputByteBuffer()


Copyright © 2012 Oracle Corporation. All Rights Reserved.