Class OutputBuffer
- java.lang.Object
-
- org.glassfish.grizzly.http.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
Nested Classes Modifier and Type Class Description static interfaceOutputBuffer.LifeCycleListener
-
Field Summary
Fields Modifier and Type Field Description protected static LoggerLOGGERprotected booleansendfileEnabled
-
Constructor Summary
Constructors Constructor Description OutputBuffer()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidacknowledge()Acknowledge a HTTPExpectheader.booleancanWrite()booleancanWrite(int length)Deprecated.thelengthparameter will be ignored.booleancanWriteChar(int length)Deprecated.voidclose()voidendRequest()voidflush()Flush the response.intgetBufferedDataSize()Get the number of bytes buffered on OutputBuffer and ready to be sent.intgetBufferSize()protected ExecutorgetThreadPool()Supplier<Map<String,String>>getTrailers()voidinitialize(HttpHeader outputHeader, boolean sendfileEnabled, org.glassfish.grizzly.filterchain.FilterChainContext ctx)booleanisAsyncEnabled()Deprecated.will always return truebooleanisClosed()voidnotifyCanWrite(org.glassfish.grizzly.WriteHandler handler)voidnotifyCanWrite(org.glassfish.grizzly.WriteHandler handler, int length)Deprecated.thelengthparameter will be ignored.voidprepareCharacterEncoder()voidrecycle()Recycle the output buffer.voidregisterLifeCycleListener(OutputBuffer.LifeCycleListener listener)booleanremoveLifeCycleListener(OutputBuffer.LifeCycleListener listener)voidreset()Reset current response.voidsendfile(File file, long offset, long length, org.glassfish.grizzly.CompletionHandler<org.glassfish.grizzly.WriteResult> handler)Will useFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)to send file to the remote endpoint.voidsendfile(File file, org.glassfish.grizzly.CompletionHandler<org.glassfish.grizzly.WriteResult> handler)Callswrite(file, 0, file.length()).voidsetAsyncEnabled(boolean asyncEnabled)Deprecated.voidsetBufferSize(int bufferSize)voidsetTrailers(Supplier<Map<String,String>> trailersSupplier)voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(char[] cbuf)voidwrite(char[] cbuf, int off, int len)voidwrite(String str)voidwrite(String str, int off, int len)voidwriteBuffer(org.glassfish.grizzly.Buffer buffer)Writes the contents of the specifiedBufferto the client.voidwriteByte(int b)voidwriteByteBuffer(ByteBuffer byteBuffer)Writes the contents of the specifiedByteBufferto the client.voidwriteChar(int c)
-
-
-
Field Detail
-
LOGGER
protected static final Logger LOGGER
-
sendfileEnabled
protected boolean sendfileEnabled
-
-
Method Detail
-
initialize
public void initialize(HttpHeader outputHeader, boolean sendfileEnabled, org.glassfish.grizzly.filterchain.FilterChainContext ctx)
-
isAsyncEnabled
@Deprecated public boolean isAsyncEnabled()
Deprecated.will always return trueReturns
trueif content will be written in a non-blocking fashion, otherwise returnsfalse.- Returns:
trueif content will be written in a non-blocking fashion, otherwise returnsfalse.- Since:
- 2.1.2
-
setAsyncEnabled
@Deprecated public void setAsyncEnabled(boolean asyncEnabled)
Deprecated.Sets the asynchronous processing state of thisOutputBuffer.- Parameters:
asyncEnabled-trueif thisOutputBufferwill write content without blocking. @since 2.1.2 @deprecated OutputBuffer always supports async mode
-
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:
trueif this OutputBuffer is closed, otherwise returnsfalse.
-
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 IOExceptionAcknowledge a HTTPExpectheader. 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, org.glassfish.grizzly.CompletionHandler<org.glassfish.grizzly.WriteResult> handler)
Calls
write(file, 0, file.length()).- Parameters:
file- theFileto transfer.handler-CompletionHandlerthat will be notified of the transfer progress/completion or failure.- Throws:
IllegalArgumentException- iffileis 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, org.glassfish.grizzly.CompletionHandler<org.glassfish.grizzly.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- theFileto transfer.offset- the starting offset within the Filelength- the total number of bytes to transferhandler-CompletionHandlerthat will be notified of the transfer progress/completion or failure.- Throws:
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 noCompletionHandlerwas 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 IOExceptionFlush the response.- Throws:
IOException- an underlying I/O error occurred
-
writeByteBuffer
public void writeByteBuffer(ByteBuffer byteBuffer) throws IOException
Writes the contents of the specified
Note, that passedByteBufferto the client.ByteBufferwill be directly used by underlying connection, so it could be reused only if it has been flushed.- Parameters:
byteBuffer- theByteBufferto write- Throws:
IOException- if an error occurs during the write
-
writeBuffer
public void writeBuffer(org.glassfish.grizzly.Buffer buffer) throws IOExceptionWrites the contents of the specified
Note, that passedBufferto the client.Bufferwill be directly used by underlying connection, so it could be reused only if it has been flushed.- Parameters:
buffer- theBufferto write- Throws:
IOException- if an error occurs during the write
-
canWriteChar
@Deprecated public boolean canWriteChar(int length)
Deprecated.
-
canWrite
@Deprecated public boolean canWrite(int length)
Deprecated.thelengthparameter will be ignored. Please usecanWrite().- See Also:
AsyncQueueWriter.canWrite(org.glassfish.grizzly.Connection, int)
-
canWrite
public boolean canWrite()
- See Also:
Writer.canWrite(org.glassfish.grizzly.Connection)
-
notifyCanWrite
@Deprecated public void notifyCanWrite(org.glassfish.grizzly.WriteHandler handler, int length)
Deprecated.thelengthparameter will be ignored. Please usenotifyCanWrite(org.glassfish.grizzly.WriteHandler).- See Also:
AsyncQueueWriter.notifyWritePossible(org.glassfish.grizzly.Connection, org.glassfish.grizzly.WriteHandler, int)
-
notifyCanWrite
public void notifyCanWrite(org.glassfish.grizzly.WriteHandler handler)
-
-