FileDocCategorySizeDatePackage
SocketChannelOutputBuffer.javaAPI DocGlassfish v2 API7075Wed Aug 08 17:08:06 BST 2007com.sun.enterprise.web.connector.grizzly

SocketChannelOutputBuffer

public class SocketChannelOutputBuffer extends org.apache.coyote.http11.InternalOutputBuffer
Output buffer. Buffer the bytes until the ByteChunk is full or the request is completed.
author
Jean-Francois Arcand
author
Scott Oaks

Fields Summary
protected SocketChannel
socketChannel
Underlying output socketChannel.
private ByteBuffer
outputByteBuffer
Underlying ByteByteBuffer
private static final ByteBuffer
ACK
ACK static bytes.
Constructors Summary
public SocketChannelOutputBuffer(org.apache.coyote.Response response, int headerBufferSize, boolean useSocketBuffer)
Alternate constructor.

    
    // ----------------------------------------------------------- Constructors
    

           
       
                
        super(response,headerBufferSize, useSocketBuffer); 
        
        if (!useSocketBuffer){
            outputStream = new NIOOutputStream();
        }
        
        outputByteBuffer = ByteBuffer.allocate(headerBufferSize * 10);
    
Methods Summary
public voidendRequest()
End request.

throws
IOException an undelying I/O error occured

        super.endRequest();
        if (!useSocketBuffer && outputByteBuffer.position() != 0) {
            flushBuffer();
        }
    
public voidflush()
Flush the buffered bytes,

        super.flush();
        if (outputByteBuffer.position() != 0){
            flushBuffer();
        }
    
public voidflushBuffer()
Writes bytes to the underlying socketChannel.

        if (outputByteBuffer.position() != 0){
            outputByteBuffer.flip();
            flushChannel(outputByteBuffer);
            outputByteBuffer.clear();
        }
    
public voidflushChannel(java.nio.ByteBuffer bb)
Flush the buffer by looping until the ByteBuffer is empty

param
bb the ByteBuffer to write.

        OutputWriter.flushChannel(socketChannel, bb);
        bb.clear();
    
public java.nio.channels.SocketChannelgetChannel()
Return the underlying SocketChannel

        return socketChannel;
    
public voidrealWriteBytes(byte[] cbuf, int off, int len)
Callback to write data from the buffer.

        if (len > 0) {
            if (len > outputByteBuffer.remaining()){
                int size = Math.max(outputByteBuffer.capacity() * 2,
                                    len + outputByteBuffer.position());
                ByteBuffer tmp = ByteBuffer.allocate(size);
                outputByteBuffer.flip();
                tmp.put(outputByteBuffer);
                outputByteBuffer = tmp;
            }
            outputByteBuffer.put(cbuf, off, len);
        }
    
public voidrecycle()
Recycle the output buffer. This should be called when closing the connection.

        
        response.recycle();
        socketBuffer.recycle();
        pos = 0;
        lastActiveFilter = -1;
        committed = false;
        finished = false;
        outputByteBuffer.clear();

        socketChannel = null;
    
public voidsendAck()
Send an acknoledgement without buffering.

        if (!committed)
            flushChannel(ACK.slice());
    
public voidsetChannel(java.nio.channels.SocketChannel socketChannel)
Set the underlying socket output stream.

        this.socketChannel = socketChannel;