Class OutputBuffer
- java.lang.Object
-
- org.glassfish.grizzly.http.io.OutputBuffer
-
- Direct Known Subclasses:
ServerOutputBuffer
public class OutputBuffer extends java.lang.ObjectAbstraction 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 java.util.logging.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 java.util.concurrent.ExecutorgetThreadPool()java.util.function.Supplier<java.util.Map<java.lang.String,java.lang.String>>getTrailers()voidinitialize(HttpHeader outputHeader, boolean sendfileEnabled, FilterChainContext ctx)booleanisAsyncEnabled()Deprecated.will always return truebooleanisClosed()voidnotifyCanWrite(WriteHandler handler)voidnotifyCanWrite(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(java.io.File file, long offset, long length, CompletionHandler<WriteResult> handler)Will useFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)to send file to the remote endpoint.voidsendfile(java.io.File file, CompletionHandler<WriteResult> handler)Callswrite(file, 0, file.length()).voidsetAsyncEnabled(boolean asyncEnabled)Deprecated.voidsetBufferSize(int bufferSize)voidsetTrailers(java.util.function.Supplier<java.util.Map<java.lang.String,java.lang.String>> trailersSupplier)voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(char[] cbuf)voidwrite(char[] cbuf, int off, int len)voidwrite(java.lang.String str)voidwrite(java.lang.String str, int off, int len)voidwriteBuffer(Buffer buffer)Writes the contents of the specifiedBufferto the client.voidwriteByte(int b)voidwriteByteBuffer(java.nio.ByteBuffer byteBuffer)Writes the contents of the specifiedByteBufferto the client.voidwriteChar(int c)
-
-
-
Method Detail
-
initialize
public void initialize(HttpHeader outputHeader, boolean sendfileEnabled, 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:
java.lang.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 java.io.IOException- Throws:
java.io.IOException
-
acknowledge
public void acknowledge() throws java.io.IOExceptionAcknowledge a HTTPExpectheader. The response status code and reason phrase should be set before invoking this method.- Throws:
java.io.IOException- if an error occurs writing the acknowledgment.
-
writeChar
public void writeChar(int c) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(char[] cbuf, int off, int len) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(char[] cbuf) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(java.lang.String str) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(java.lang.String str, int off, int len) throws java.io.IOException- Throws:
java.io.IOException
-
writeByte
public void writeByte(int b) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Throws:
java.io.IOException
-
sendfile
public void sendfile(java.io.File file, CompletionHandler<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:
java.lang.IllegalArgumentException- iffileis null- Since:
- 2.2
- See Also:
sendfile(java.io.File, long, long, org.glassfish.grizzly.CompletionHandler)
-
sendfile
public void sendfile(java.io.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- 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:
java.lang.IllegalArgumentException- if the response has already been committed at the time this method was invoked.java.lang.IllegalStateException- if a file transfer request has already been made or if send file support isn't available.java.lang.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 java.io.IOException- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOExceptionFlush the response.- Throws:
java.io.IOException- an underlying I/O error occurred
-
writeByteBuffer
public void writeByteBuffer(java.nio.ByteBuffer byteBuffer) throws java.io.IOExceptionWrites 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:
java.io.IOException- if an error occurs during the write
-
writeBuffer
public void writeBuffer(Buffer buffer) throws java.io.IOException
Writes 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:
java.io.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().
-
canWrite
public boolean canWrite()
-
notifyCanWrite
@Deprecated public void notifyCanWrite(WriteHandler handler, int length)
Deprecated.thelengthparameter will be ignored. Please usenotifyCanWrite(org.glassfish.grizzly.WriteHandler).
-
notifyCanWrite
public void notifyCanWrite(WriteHandler handler)
-
setTrailers
public void setTrailers(java.util.function.Supplier<java.util.Map<java.lang.String,java.lang.String>> trailersSupplier)
-
getTrailers
public java.util.function.Supplier<java.util.Map<java.lang.String,java.lang.String>> getTrailers()
-
getThreadPool
protected java.util.concurrent.Executor getThreadPool()
- Returns:
Executor, which will be used for notifying user registeredWriteHandler.
-
-