org.glassfish.grizzly.http.server.io
Class OutputBuffer

java.lang.Object
  extended by org.glassfish.grizzly.http.server.io.OutputBuffer

public class OutputBuffer
extends Object

Abstraction exposing both byte and character methods to write content to the HTTP messaging system in Grizzly.


Nested Class Summary
static interface OutputBuffer.LifeCycleListener
           
 
Constructor Summary
OutputBuffer()
           
 
Method Summary
 void acknowledge()
          Acknowledge a HTTP Expect header.
 boolean canWrite()
           
 boolean canWrite(int length)
          Deprecated. the length parameter will be ignored. Please use canWrite().
 boolean canWriteChar(int length)
          Deprecated. 
 void close()
           
 void endRequest()
           
 void flush()
          Flush the response.
 int getBufferedDataSize()
          Get the number of bytes buffered on OutputBuffer and ready to be sent.
 int getBufferSize()
           
 void initialize(Response response, FilterChainContext ctx)
           
 boolean isAsyncEnabled()
          Deprecated. will always return true
 boolean isClosed()
           
 void notifyCanWrite(WriteHandler handler)
           
 void notifyCanWrite(WriteHandler handler, int length)
          Deprecated. the length parameter will be ignored. Please use notifyCanWrite(org.glassfish.grizzly.WriteHandler).
 void prepareCharacterEncoder()
           
 void recycle()
          Recycle the output buffer.
 void registerLifeCycleListener(OutputBuffer.LifeCycleListener listener)
           
 boolean removeLifeCycleListener(OutputBuffer.LifeCycleListener listener)
           
 void reset()
          Reset current response.
 void sendfile(File file, CompletionHandler<WriteResult> handler)
           Calls write(file, 0, file.length()).
 void sendfile(File file, long offset, long length, CompletionHandler<WriteResult> handler)
           Will use FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) to send file to the remote endpoint.
 void setAsyncEnabled(boolean asyncEnabled)
          Deprecated. OutputBuffer always supports async mode
 void setBufferSize(int bufferSize)
           
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(char[] cbuf)
           
 void write(char[] cbuf, int off, int len)
           
 void write(String str)
           
 void write(String str, int off, int len)
           
 void writeBuffer(Buffer buffer)
           Writes the contents of the specified Buffer to the client.
 void writeByte(int b)
           
 void writeByteBuffer(ByteBuffer byteBuffer)
           Writes the contents of the specified ByteBuffer to the client.
 void writeChar(int c)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OutputBuffer

public OutputBuffer()
Method Detail

initialize

public void initialize(Response response,
                       FilterChainContext ctx)

isAsyncEnabled

public boolean isAsyncEnabled()
Deprecated. will always return true

Returns true if content will be written in a non-blocking fashion, otherwise returns false.

Returns:
true if content will be written in a non-blocking fashion, otherwise returns false.
Since:
2.1.2

setAsyncEnabled

public void setAsyncEnabled(boolean asyncEnabled)
Deprecated. OutputBuffer always supports async mode

Sets the asynchronous processing state of this OutputBuffer.

Parameters:
asyncEnabled - true if this OutputBuffer will write content without blocking.
Since:
2.1.2

prepareCharacterEncoder

public void prepareCharacterEncoder()

getBufferSize

public int getBufferSize()

registerLifeCycleListener

public void registerLifeCycleListener(OutputBuffer.LifeCycleListener listener)

removeLifeCycleListener

public boolean removeLifeCycleListener(OutputBuffer.LifeCycleListener listener)

setBufferSize

public void setBufferSize(int bufferSize)

reset

public void reset()
Reset current response.

Throws:
IllegalStateException - if the response has already been committed

isClosed

public boolean isClosed()
Returns:
true if this OutputBuffer is closed, otherwise returns false.

getBufferedDataSize

public int getBufferedDataSize()
Get the number of bytes buffered on OutputBuffer and ready to be sent.

Returns:
the number of bytes buffered on OutputBuffer and ready to be sent.

recycle

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


endRequest

public void endRequest()
                throws IOException
Throws:
IOException

acknowledge

public void acknowledge()
                 throws IOException
Acknowledge a HTTP Expect header. The response status code and reason phrase should be set before invoking this method.

Throws:
IOException - if an error occurs writing the acknowledgment.

writeChar

public void writeChar(int c)
               throws IOException
Throws:
IOException

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws IOException
Throws:
IOException

write

public void write(char[] cbuf)
           throws IOException
Throws:
IOException

write

public void write(String str)
           throws IOException
Throws:
IOException

write

public void write(String str,
                  int off,
                  int len)
           throws IOException
Throws:
IOException

writeByte

public void writeByte(int b)
               throws IOException
Throws:
IOException

write

public void write(byte[] b)
           throws IOException
Throws:
IOException

sendfile

public void sendfile(File file,
                     CompletionHandler<WriteResult> handler)

Calls write(file, 0, file.length()).

Parameters:
file - the File to transfer.
handler - CompletionHandler that will be notified of the transfer progress/completion or failure.
Throws:
IOException - if an error occurs during the transfer
IllegalArgumentException - if file is null
Since:
2.2
See Also:
sendfile(java.io.File, long, long, org.glassfish.grizzly.CompletionHandler)

sendfile

public void sendfile(File file,
                     long offset,
                     long length,
                     CompletionHandler<WriteResult> handler)

Will use FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) to send file to the remote endpoint. Note that all headers necessary for the file transfer must be set prior to invoking this method as this will case the HTTP header to be flushed to the client prior to sending the file content. This should also be the last call to write any content to the remote endpoint.

It's required that the response be suspended when using this functionality. It will be assumed that if the response wasn't suspended when this method is called, that it's desired that this method manages the suspend/resume cycle.

Parameters:
file - the File to transfer.
offset - the starting offset within the File
length - the total number of bytes to transfer
handler - CompletionHandler that will be notified of the transfer progress/completion or failure.
Throws:
IOException - if an error occurs during the transfer
IOException - if an I/O error occurs
IllegalArgumentException - if the response has already been committed at the time this method was invoked.
IllegalStateException - if a file transfer request has already been made or if send file support isn't available.
IllegalStateException - if the response was in a suspended state when this method was invoked, but no CompletionHandler was provided.
Since:
2.2

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Throws:
IOException

close

public void close()
           throws IOException
Throws:
IOException

flush

public void flush()
           throws IOException
Flush the response.

Throws:
IOException - an underlying I/O error occurred

writeByteBuffer

public void writeByteBuffer(ByteBuffer byteBuffer)
                     throws IOException

Writes the contents of the specified ByteBuffer to the client.

Note, that passed ByteBuffer will be directly used by underlying connection, so it could be reused only if it has been flushed.

Parameters:
byteBuffer - the ByteBuffer to write
Throws:
IOException - if an error occurs during the write

writeBuffer

public void writeBuffer(Buffer buffer)
                 throws IOException

Writes the contents of the specified Buffer to the client.

Note, that passed Buffer will be directly used by underlying connection, so it could be reused only if it has been flushed.

Parameters:
buffer - the Buffer to write
Throws:
IOException - if an error occurs during the write

canWriteChar

@Deprecated
public boolean canWriteChar(int length)
Deprecated. 


canWrite

public boolean canWrite(int length)
Deprecated. the length parameter will be ignored. Please use canWrite().

See Also:
AsyncQueueWriter.canWrite(org.glassfish.grizzly.Connection, int)

canWrite

public boolean canWrite()
See Also:
AsyncQueueWriter.canWrite(org.glassfish.grizzly.Connection)

notifyCanWrite

public void notifyCanWrite(WriteHandler handler,
                           int length)
Deprecated. the length parameter will be ignored. Please use notifyCanWrite(org.glassfish.grizzly.WriteHandler).

See Also:
AsyncQueueWriter.notifyWritePossible(org.glassfish.grizzly.Connection, org.glassfish.grizzly.WriteHandler, int)

notifyCanWrite

public void notifyCanWrite(WriteHandler handler)


Copyright © 2012 Oracle Corporation. All Rights Reserved.