Package org.glassfish.grizzly.http.io
Class InputBuffer
- java.lang.Object
-
- org.glassfish.grizzly.http.io.InputBuffer
-
public class InputBuffer extends Object
Abstraction exposing both byte and character methods to read content from the HTTP messaging system in Grizzly.
-
-
Constructor Summary
Constructors Constructor Description InputBuffer()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanappend(HttpContent httpContent)Appends the specifiedBufferto the internal compositeBuffer.booleanareTrailersAvailable()intavailable()intavailableChar()protected HttpContentblockingRead()Read next chunk of data in this thread, block if needed.voidclose()voidfillFully(int length)/** Fill the buffer (blocking) up to the requested length.protected voidfinished()When invoked, this method will callReadHandler.onAllDataRead()on the currentReadHandler(if any).org.glassfish.grizzly.BuffergetBuffer()Returns the duplicate of the underlyingBufferused to buffer incoming request data.org.glassfish.grizzly.ReadHandlergetReadHandler()protected ExecutorgetThreadPool()Map<String,String>getTrailers()voidinitialize(HttpHeader httpHeader, org.glassfish.grizzly.filterchain.FilterChainContext ctx)Per-request initialization required for the InputBuffer to function properly.voidinitiateAsyncronousDataReceiving()Initiates asynchronous data receiving.booleanisAsyncEnabled()Deprecated.will always return truebooleanisClosed()booleanisFinished()voidmark(int readAheadLimit)Supported with binary and character data.booleanmarkSupported()Only supported with binary data.voidnotifyAvailable(org.glassfish.grizzly.ReadHandler handler)Installs aReadHandlerthat will be notified when any data becomes available to read without blocking.voidnotifyAvailable(org.glassfish.grizzly.ReadHandler handler, int size)Installs aReadHandlerthat will be notified when the specified amount of data is available to be read without blocking.voidprocessingChars()This method should be called if the InputBuffer is being used in conjunction with aReaderimplementation.intread(byte[] b, int off, int len)intread(char[] cbuf, int off, int len)intread(CharBuffer target)org.glassfish.grizzly.BufferreadBuffer()org.glassfish.grizzly.BufferreadBuffer(int size)intreadByte()This method always blocks.intreadChar()booleanready()intreadyData()Depending on the InputBuffer mode, method will return either number of available bytes or characters ready to be read without blocking.voidrecycle()Recycle thisInputBufferfor reuse.voidreplayPayload(org.glassfish.grizzly.Buffer buffer)voidreset()Only supported with binary data.voidsetAsyncEnabled(boolean asyncEnabled)Deprecated.voidsetDefaultEncoding(String encoding)Set the default character encoding for this InputBuffer, which would be applied if no encoding was explicitly set on HTTPHttpRequestPacketand character decoding wasn't started yet.longskip(long n)Skips the specified number of bytes/characters.longskip(long n, boolean block)Deprecated.pls.voidterminate()InvokeReadHandler.onError(Throwable)(assuming aReadHandleris available) } passing a {#link CancellationException} if the currentConnectionis open, or a {#link EOFException} if the connection was unexpectedly closed.protected voidupdateInputContentBuffer(org.glassfish.grizzly.Buffer buffer)
-
-
-
Method Detail
-
initialize
public void initialize(HttpHeader httpHeader, org.glassfish.grizzly.filterchain.FilterChainContext ctx)
Per-request initialization required for the InputBuffer to function properly.
- Parameters:
httpHeader- the header from which input will be obtained.ctx- the FilterChainContext for the chain processing this request
-
setDefaultEncoding
public void setDefaultEncoding(String encoding)
Set the default character encoding for this InputBuffer, which would be applied if no encoding was explicitly set on HTTPHttpRequestPacketand character decoding wasn't started yet.
-
recycle
public void recycle()
Recycle this
InputBufferfor reuse.
-
processingChars
public void processingChars()
This method should be called if the InputBuffer is being used in conjunction with a
Readerimplementation. If this method is not called, any character-based methods called on thisInputBufferwill throw aIllegalStateException.
-
readByte
public int readByte() throws IOExceptionThis method always blocks.- Throws:
IOException- See Also:
InputStream.read()
-
read
public int read(byte[] b, int off, int len) throws IOException- Throws:
IOException- See Also:
InputStream.read(byte[], int, int)
-
readyData
public int readyData()
Depending on the InputBuffer mode, method will return either number of available bytes or characters ready to be read without blocking.- Returns:
- depending on the InputBuffer mode, method will return either number of available bytes or characters ready to be read without blocking.
-
available
public int available()
- See Also:
InputStream.available()
-
getBuffer
public org.glassfish.grizzly.Buffer getBuffer()
Returns the duplicate of the underlyingBufferused to buffer incoming request data. The content of the returned buffer will be that of the underlying buffer. Changes to returned buffer's content will be visible in the underlying buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.- Returns:
- the duplicate of the underlying
Bufferused to buffer incoming request data.
-
readBuffer
public org.glassfish.grizzly.Buffer readBuffer()
- Returns:
- the underlying
Bufferused to buffer incoming request data. UnlikegetBuffer(), this method detaches the returnedBuffer, so user code becomes responsible for handling theBuffer.
-
readBuffer
public org.glassfish.grizzly.Buffer readBuffer(int size)
- Parameters:
size- the requested size of theBufferto be returned.- Returns:
- the
Bufferof a given size, which represents a chunk of the underlyingBufferwhich contains incoming request data. This method detaches the returnedBuffer, so user code becomes responsible for handling its life-cycle.
-
getReadHandler
public org.glassfish.grizzly.ReadHandler getReadHandler()
- Returns:
- the
ReadHandlercurrent in use, if any.
-
read
public int read(CharBuffer target) throws IOException
- Throws:
IOException- See Also:
Reader.read(java.nio.CharBuffer)
-
readChar
public int readChar() throws IOException- Throws:
IOException- See Also:
Reader.read()
-
read
public int read(char[] cbuf, int off, int len) throws IOException- Throws:
IOException- See Also:
Reader.read(char[], int, int)
-
ready
public boolean ready()
- See Also:
Reader.ready()
-
fillFully
public void fillFully(int length) throws IOException/** Fill the buffer (blocking) up to the requested length.- Parameters:
length- how much content should attempt to buffer,-1means buffer entire request.- Throws:
IOException- if an error occurs reading data from the wire.
-
availableChar
public int availableChar()
-
mark
public void mark(int readAheadLimit)
Supported with binary and character data.
- See Also:
InputStream.mark(int),Reader.mark(int)
-
markSupported
public boolean markSupported()
Only supported with binary data.
- See Also:
InputStream.markSupported()
-
reset
public void reset() throws IOExceptionOnly supported with binary data.
- Throws:
IOException- See Also:
InputStream.reset()
-
close
public void close() throws IOException- Throws:
IOException- See Also:
Closeable.close()
-
skip
@Deprecated public long skip(long n, boolean block) throws IOException
Deprecated.pls. useskip(long), the block parameter will be ignoredSkips the specified number of bytes/characters.- Throws:
IOException- See Also:
InputStream.skip(long),Reader.skip(long)
-
skip
public long skip(long n) throws IOExceptionSkips the specified number of bytes/characters.- Throws:
IOException- See Also:
InputStream.skip(long),Reader.skip(long)
-
areTrailersAvailable
public boolean areTrailersAvailable()
-
finished
protected void finished()
When invoked, this method will callReadHandler.onAllDataRead()on the currentReadHandler(if any). This method shouldn't be invoked by developers directly.
-
replayPayload
public void replayPayload(org.glassfish.grizzly.Buffer buffer)
-
isFinished
public boolean isFinished()
- Returns:
trueif all request data has been read, otherwise returnsfalse.
-
isClosed
public boolean isClosed()
- Returns:
trueif this InputBuffer is closed, otherwise returnsfalse.
-
notifyAvailable
public void notifyAvailable(org.glassfish.grizzly.ReadHandler handler)
Installs aReadHandlerthat will be notified when any data becomes available to read without blocking.- Parameters:
handler- theReadHandlerto invoke.- Throws:
IllegalArgumentException- ifhandlerisnull.IllegalStateException- if an attempt is made to register a handler before an existing registered handler has been invoked or if all request data has already been read.
-
notifyAvailable
public void notifyAvailable(org.glassfish.grizzly.ReadHandler handler, int size)Installs aReadHandlerthat will be notified when the specified amount of data is available to be read without blocking.- Parameters:
handler- theReadHandlerto invoke.size- the minimum number of bytes that must be available before theReadHandleris notified.- Throws:
IllegalArgumentException- ifhandlerisnull, or ifsizeis less or equal to zero.IllegalStateException- if an attempt is made to register a handler before an existing registered handler has been invoked.
-
append
public boolean append(HttpContent httpContent) throws IOException
Appends the specifiedBufferto the internal compositeBuffer.- Parameters:
httpContent- theHttpContentto append- Returns:
trueifReadHandlercallback was invoked, otherwise returnsfalse.- Throws:
IOException- if an error occurs appending theBuffer
-
isAsyncEnabled
@Deprecated public boolean isAsyncEnabled()
Deprecated.will always return true- Returns:
- if this buffer is being used to process asynchronous data.
-
setAsyncEnabled
@Deprecated public void setAsyncEnabled(boolean asyncEnabled)
Deprecated.Sets the asynchronous processing state of thisInputBuffer.- Parameters:
asyncEnabled-trueif thisInputBufferis to be used to process asynchronous request data. @deprecated InputBuffer always supports async mode
-
terminate
public void terminate()
Invoke
ReadHandler.onError(Throwable)(assuming aReadHandleris available) } passing a {#link CancellationException} if the currentConnectionis open, or a {#link EOFException} if the connection was unexpectedly closed.- Since:
- 2.0.1
-
initiateAsyncronousDataReceiving
public void initiateAsyncronousDataReceiving()
Initiates asynchronous data receiving. This is service method, usually users don't have to call it explicitly.
-
getThreadPool
protected Executor getThreadPool()
- Returns:
Executor, which will be used for notifying user registeredReadHandler.
-
blockingRead
protected HttpContent blockingRead() throws IOException
Read next chunk of data in this thread, block if needed.- Returns:
HttpContent- Throws:
IOException- if an error occurs reading data from the wire.
-
updateInputContentBuffer
protected void updateInputContentBuffer(org.glassfish.grizzly.Buffer buffer)
-
-