Top Description Fields Constructors Methods
org.glassfish.grizzly.http.io

public Class InputBuffer

extends Object
Class Inheritance
Static Imports
org.glassfish.grizzly.http.util.Constants.DEFAULT_HTTP_CHARACTER_ENCODING

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

Field Summary

Modifier and TypeField and Description
private float
averageCharsPerByte

Used to estimate how many characters can be produced from a variable number of bytes.

private boolean
closed

Flag indicating whether or not this InputBuffer has been closed.

private Connection<L>
private boolean
contentRead

Flag indicating all request content has been read.

private FilterChainContext
ctx

The FilterChainContext associated with this InputBuffer.

private CharsetDecoder
decoder

The CharsetDecoder used to convert binary to character data.

private final Map<String, CharsetDecoder>
decoders

CharsetDecoders cache

private String
encoding

The default character encoding to use if none is specified by the org.glassfish.grizzly.http.HttpRequestPacket.

private volatile ReadHandler
handler

The ReadHandler to be notified as content is read.

private HttpHeader
httpHeader

The org.glassfish.grizzly.http.HttpHeader associated with this InputBuffer

private Buffer
inputContentBuffer

The Buffer consisting of the bytes from the HTTP message chunk(s).

private boolean
isWaitingDataAsynchronously

Flag shows if we're currently waiting for input data asynchronously (is OP_READ enabled for the Connection)

private static final Logger
private static final Level
private int
markPos

The mark position within the current binary content.

private boolean
processingChars

Flag indicating whether or not this InputBuffer is processing character data.

private int
readAheadLimit

Represents how many bytes can be read before markPos is invalidated.

private int
readCount

Counter to track how many bytes have been read.

private int
requestedSize

The length of the content that must be read before notifying the ReadHandler.

private final CharBuffer
singleCharBuf

CharBuffer for converting a single character at a time.

protected Map<String, String>
trailers

Trailer headers, if any.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if ReadHandler callback was invoked, otherwise returns false.
append
(final HttpContent
the HttpContent to append
httpContent
)

Appends the specified Buffer to the internal composite Buffer.

public boolean
public int
public int
protected HttpContent

Returns:

HttpContent
blockingRead
()

Read next chunk of data in this thread, block if needed.

private static void
checkHttpTrailer(final HttpContent httpContent)

Check if passed HttpContent is HttpTrailer, which represents trailer chunk (when chunked Transfer-Encoding is used), if it is a trailer chunk - then copy all the available trailer headers to request headers map.

private boolean
checkMarkAfterRead(final long
read bytes count
n
)

Returns: true if mark is still active, or false if the mark hasn't been set or has been invalidated

public void
private int

Returns:

the number of bytes actually read
fill
(final int
how much content should attempt to be read, -1 means read till the end of the message.
requestedLen
)

Used to add additional HTTP message chunk content to inputContentBuffer.

private int

Returns:

the number of chars actually read
fillAvailableChars
(final int
how much content should attempt to be read
requestedLen
,
final CharBuffer dst)

Used to convert pre-read (buffered) bytes to chars.

private int

Returns:

the number of chars actually read
fillChars
(final int
how much content should attempt to be read
requestedLen
,
final CharBuffer dst)

Used to convert bytes to chars.

public void
fillFully(final int
how much content should attempt to buffer, -1 means buffer entire request.
length
)

/** Fill the buffer (blocking) up to the requested length.

protected void
finished()

When invoked, this method will call org.glassfish.grizzly.ReadHandler#onAllDataRead() on the current ReadHandler (if any).

private void
public Buffer

Returns:

the duplicate of the underlying Buffer used to buffer incoming request data.
getBuffer
()

Returns the duplicate of the underlying Buffer used to buffer incoming request data.

private CharsetDecoder
getDecoder()

Returns: the CharsetDecoder that should be used when converting content from binary to character

public ReadHandler
getReadHandler()

Returns: the ReadHandler current in use, if any.

protected Executor
getThreadPool()

Returns: Executor, which will be used for notifying user registered ReadHandler.

public Map<String, String>
public void
initialize(final HttpHeader
the header from which input will be obtained.
httpHeader
,
final FilterChainContext
the FilterChainContext for the chain processing this request
ctx
)

Per-request initialization required for the InputBuffer to function properly.

public void
initiateAsyncronousDataReceiving()

Initiates asynchronous data receiving.

private void
invokeErrorHandlerOnProperThread(final ReadHandler localHandler, final Throwable error)

private void
invokeHandler(final ReadHandler localHandler, final boolean invokeDataAvailable, final boolean isLast)

private void
invokeHandlerAllRead(final ReadHandler readHandler, final Executor executor)

private void
invokeHandlerOnProperThread(final ReadHandler localHandler, final boolean invokeDataAvailable, final boolean isLast)

public boolean
isAsyncEnabled()
Deprecated will always return true

Returns: if this buffer is being used to process asynchronous data.

public boolean
isClosed()

Returns: true if this InputBuffer is closed, otherwise returns false.

public boolean
isFinished()

Returns: true if all request data has been read, otherwise returns false.

private static void
log(final String message, Object... params)

public void
mark(final int readAheadLimit)

Supported with binary and character data.

public boolean
markSupported()

Only supported with binary data.

public void
notifyAvailable(final ReadHandler
the ReadHandler to invoke.
handler
)

Installs a ReadHandler that will be notified when any data becomes available to read without blocking.

public void
notifyAvailable(final ReadHandler
the ReadHandler to invoke.
handler
,
final int
the minimum number of bytes that must be available before the ReadHandler is notified.
size
)

Installs a ReadHandler that will be notified when the specified amount of data is available to be read without blocking.

public void
processingChars()

This method should be called if the InputBuffer is being used in conjunction with a java.io.Reader implementation.

private void
public int
read(final byte[] b, final int off, final int len)

public int
read(final CharBuffer target)

public int
read(final char[] cbuf, final int off, final int len)

public Buffer
readBuffer()

Returns: the underlying Buffer used to buffer incoming request data.

public Buffer
readBuffer(final int
the requested size of the Buffer to be returned.
size
)

Returns: the Buffer of a given size, which represents a chunk of the underlying Buffer which contains incoming request data.

public int
readByte()

This method always blocks.

public int
public boolean
public int

Returns:

depending on the InputBuffer mode, method will return either number of available bytes or characters ready to be read without blocking.
readyData
()

Depending on the InputBuffer mode, method will return either number of available bytes or characters ready to be read without blocking.

public void
recycle()

Recycle this InputBuffer for reuse.

public void
replayPayload(final Buffer buffer)

public void
reset()

Only supported with binary data.

public void
setAsyncEnabled(boolean
true if this InputBuffer is to be used to process asynchronous request data. @deprecated InputBuffer always supports async mode
asyncEnabled
)
Deprecated

Sets the asynchronous processing state of this InputBuffer.

public void
setDefaultEncoding(final String encoding)

Set the default character encoding for this InputBuffer, which would be applied if no encoding was explicitly set on HTTP org.glassfish.grizzly.http.HttpRequestPacket and character decoding wasn't started yet.

private static boolean
shouldNotifyNow(final int
the amount of data that must be available for a ReadHandler to be notified.
size
,
final int
the amount of data currently available.
available
)

Returns: true if the handler should be notified during a call to notifyAvailable(ReadHandler) or notifyAvailable(ReadHandler, int), otherwise false

public long
skip(final long n, final boolean block)
Deprecated pls. use skip(long), the block parameter will be ignored

Skips the specified number of bytes/characters.

public long
skip(final long n)

Skips the specified number of bytes/characters.

public void
terminate()

Invoke ReadHandler#onError(Throwable) (assuming a ReadHandler is available) } passing a {#link CancellationException} if the current Connection is open, or a {#link EOFException} if the connection was unexpectedly closed.

private CompositeBuffer
protected void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

averageCharsPerByteback to summary
private float averageCharsPerByte

Used to estimate how many characters can be produced from a variable number of bytes.

closedback to summary
private boolean closed

Flag indicating whether or not this InputBuffer has been closed.

connectionback to summary
private Connection<L> connection

The Connection associated with this org.glassfish.grizzly.http.HttpRequestPacket.

contentReadback to summary
private boolean contentRead

Flag indicating all request content has been read.

ctxback to summary
private FilterChainContext ctx

The FilterChainContext associated with this InputBuffer. The FilterChainContext will be using to read POST data in blocking fashion.

decoderback to summary
private CharsetDecoder decoder

The CharsetDecoder used to convert binary to character data.

decodersback to summary
private final Map<String, CharsetDecoder> decoders

CharsetDecoders cache

encodingback to summary
private String encoding

The default character encoding to use if none is specified by the org.glassfish.grizzly.http.HttpRequestPacket.

handlerback to summary
private volatile ReadHandler handler

The ReadHandler to be notified as content is read.

httpHeaderback to summary
private HttpHeader httpHeader

The org.glassfish.grizzly.http.HttpHeader associated with this InputBuffer

inputContentBufferback to summary
private Buffer inputContentBuffer

The Buffer consisting of the bytes from the HTTP message chunk(s).

isWaitingDataAsynchronouslyback to summary
private boolean isWaitingDataAsynchronously

Flag shows if we're currently waiting for input data asynchronously (is OP_READ enabled for the Connection)

LOGGERback to summary
private static final Logger LOGGER
LOGGER_LEVELback to summary
private static final Level LOGGER_LEVEL
markPosback to summary
private int markPos

The mark position within the current binary content. Marking is not supported for character content.

processingCharsback to summary
private boolean processingChars

Flag indicating whether or not this InputBuffer is processing character data.

readAheadLimitback to summary
private int readAheadLimit

Represents how many bytes can be read before markPos is invalidated.

readCountback to summary
private int readCount

Counter to track how many bytes have been read. This counter is only used with the byte stream has been marked.

requestedSizeback to summary
private int requestedSize

The length of the content that must be read before notifying the ReadHandler.

singleCharBufback to summary
private final CharBuffer singleCharBuf

CharBuffer for converting a single character at a time.

trailersback to summary
protected Map<String, String> trailers

Trailer headers, if any.

Constructor Detail

InputBufferback to summary
public InputBuffer()

Method Detail

appendback to summary
public boolean append(final HttpContent httpContent) throws IOException

Appends the specified Buffer to the internal composite Buffer.

Parameters
httpContent:HttpContent

the HttpContent to append

Returns:boolean

true if ReadHandler callback was invoked, otherwise returns false.

Exceptions
IOException:
if an error occurs appending the Buffer
areTrailersAvailableback to summary
public boolean areTrailersAvailable()
availableback to summary
public int available()
See Also
java.io.InputStream#available()
availableCharback to summary
public int availableChar()
blockingReadback to summary
protected HttpContent blockingRead() throws IOException

Read next chunk of data in this thread, block if needed.

Returns:HttpContent

HttpContent

Exceptions
IOException:
if an error occurs reading data from the wire.
checkHttpTrailerback to summary
private static void checkHttpTrailer(final HttpContent httpContent)

Check if passed HttpContent is HttpTrailer, which represents trailer chunk (when chunked Transfer-Encoding is used), if it is a trailer chunk - then copy all the available trailer headers to request headers map.

checkMarkAfterReadback to summary
private boolean checkMarkAfterRead(final long n)
Parameters
n:long

read bytes count

Returns:boolean

true if mark is still active, or false if the mark hasn't been set or has been invalidated

closeback to summary
public void close() throws IOException
See Also
java.io.Closeable#close()
fillback to summary
private int fill(final int requestedLen) throws IOException

Used to add additional HTTP message chunk content to inputContentBuffer.

Parameters
requestedLen:int

how much content should attempt to be read, -1 means read till the end of the message.

Returns:int

the number of bytes actually read

Exceptions
IOException:
if an I/O error occurs while reading content
fillAvailableCharsback to summary
private int fillAvailableChars(final int requestedLen, final CharBuffer dst)

Used to convert pre-read (buffered) bytes to chars.

Parameters
requestedLen:int

how much content should attempt to be read

Returns:int

the number of chars actually read

fillCharsback to summary
private int fillChars(final int requestedLen, final CharBuffer dst) throws IOException

Used to convert bytes to chars.

Parameters
requestedLen:int

how much content should attempt to be read

Returns:int

the number of chars actually read

Exceptions
IOException:
if an I/O error occurs while reading content
fillFullyback to summary
public void fillFully(final int length) throws IOException

/** Fill the buffer (blocking) up to the requested length.

Parameters
length:int

how much content should attempt to buffer, -1 means buffer entire request.

Exceptions
IOException:
if an error occurs reading data from the wire.
finishedback to summary
protected void finished()

When invoked, this method will call org.glassfish.grizzly.ReadHandler#onAllDataRead() on the current ReadHandler (if any). This method shouldn't be invoked by developers directly.

finishedInTheCurrentThreadback to summary
private void finishedInTheCurrentThread(final ReadHandler readHandler)
getBufferback to summary
public Buffer getBuffer()

Returns the duplicate of the underlying Buffer used 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:Buffer

the duplicate of the underlying Buffer used to buffer incoming request data.

getDecoderback to summary
private CharsetDecoder getDecoder()
Returns:CharsetDecoder

the CharsetDecoder that should be used when converting content from binary to character

getReadHandlerback to summary
public ReadHandler getReadHandler()
Returns:ReadHandler

the ReadHandler current in use, if any.

getThreadPoolback to summary
protected Executor getThreadPool()
Returns:Executor

Executor, which will be used for notifying user registered ReadHandler.

getTrailersback to summary
public Map<String, String> getTrailers()
initializeback to summary
public void initialize(final HttpHeader httpHeader, final FilterChainContext ctx)

Per-request initialization required for the InputBuffer to function properly.

Parameters
httpHeader:HttpHeader

the header from which input will be obtained.

ctx:FilterChainContext

the FilterChainContext for the chain processing this request

initiateAsyncronousDataReceivingback to summary
public void initiateAsyncronousDataReceiving()

Initiates asynchronous data receiving. This is service method, usually users don't have to call it explicitly.

invokeErrorHandlerOnProperThreadback to summary
private void invokeErrorHandlerOnProperThread(final ReadHandler localHandler, final Throwable error)
invokeHandlerback to summary
private void invokeHandler(final ReadHandler localHandler, final boolean invokeDataAvailable, final boolean isLast)
invokeHandlerAllReadback to summary
private void invokeHandlerAllRead(final ReadHandler readHandler, final Executor executor)
invokeHandlerOnProperThreadback to summary
private void invokeHandlerOnProperThread(final ReadHandler localHandler, final boolean invokeDataAvailable, final boolean isLast) throws IOException
isAsyncEnabledback to summary
public boolean isAsyncEnabled()

Deprecated

will always return true

Returns:boolean

if this buffer is being used to process asynchronous data.

Annotations
@Deprecated
isClosedback to summary
public boolean isClosed()
Returns:boolean

true if this InputBuffer is closed, otherwise returns false.

isFinishedback to summary
public boolean isFinished()
Returns:boolean

true if all request data has been read, otherwise returns false.

logback to summary
private static void log(final String message, Object... params)
markback to summary
public void mark(final int readAheadLimit)

Supported with binary and character data.

See Also
java.io.InputStream#mark(int), java.io.Reader#mark(int)
markSupportedback to summary
public boolean markSupported()

Only supported with binary data.

See Also
java.io.InputStream#markSupported()
notifyAvailableback to summary
public void notifyAvailable(final ReadHandler handler)

Installs a ReadHandler that will be notified when any data becomes available to read without blocking.

Parameters
handler:ReadHandler

the ReadHandler to invoke.

Exceptions
IllegalArgumentException:
if handler is null.
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.
notifyAvailableback to summary
public void notifyAvailable(final ReadHandler handler, final int size)

Installs a ReadHandler that will be notified when the specified amount of data is available to be read without blocking.

Parameters
handler:ReadHandler

the ReadHandler to invoke.

size:int

the minimum number of bytes that must be available before the ReadHandler is notified.

Exceptions
IllegalArgumentException:
if handler is null, or if size is less or equal to zero.
IllegalStateException:
if an attempt is made to register a handler before an existing registered handler has been invoked.
processingCharsback to summary
public void processingChars()

This method should be called if the InputBuffer is being used in conjunction with a java.io.Reader implementation. If this method is not called, any character-based methods called on this InputBuffer will throw a IllegalStateException.

processTrailersback to summary
private void processTrailers()
readback to summary
public int read(final byte[] b, final int off, final int len) throws IOException
See Also
java.io.InputStream#read(byte[], int, int)
readback to summary
public int read(final CharBuffer target) throws IOException
See Also
java.io.Reader#read(java.nio.CharBuffer)
readback to summary
public int read(final char[] cbuf, final int off, final int len) throws IOException
See Also
java.io.Reader#read(char[], int, int)
readBufferback to summary
public Buffer readBuffer()
Returns:Buffer

the underlying Buffer used to buffer incoming request data. Unlike getBuffer(), this method detaches the returned Buffer, so user code becomes responsible for handling the Buffer.

readBufferback to summary
public Buffer readBuffer(final int size)
Parameters
size:int

the requested size of the Buffer to be returned.

Returns:Buffer

the Buffer of a given size, which represents a chunk of the underlying Buffer which contains incoming request data. This method detaches the returned Buffer, so user code becomes responsible for handling its life-cycle.

readByteback to summary
public int readByte() throws IOException

This method always blocks.

See Also
java.io.InputStream#read()
readCharback to summary
public int readChar() throws IOException
See Also
java.io.Reader#read()
readyback to summary
public boolean ready()
See Also
java.io.Reader#ready()
readyDataback to summary
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:int

depending on the InputBuffer mode, method will return either number of available bytes or characters ready to be read without blocking.

recycleback to summary
public void recycle()

Recycle this InputBuffer for reuse.

replayPayloadback to summary
public void replayPayload(final Buffer buffer)
resetback to summary
public void reset() throws IOException

Only supported with binary data.

See Also
java.io.InputStream#reset()
setAsyncEnabledback to summary
public void setAsyncEnabled(boolean asyncEnabled)

Deprecated

Sets the asynchronous processing state of this InputBuffer.

Parameters
asyncEnabled:boolean

true if this InputBuffer is to be used to process asynchronous request data. @deprecated InputBuffer always supports async mode

Annotations
@Deprecated
@SuppressWarnings:UnusedDeclaration
setDefaultEncodingback to summary
public void setDefaultEncoding(final String encoding)

Set the default character encoding for this InputBuffer, which would be applied if no encoding was explicitly set on HTTP org.glassfish.grizzly.http.HttpRequestPacket and character decoding wasn't started yet.

Annotations
@SuppressWarnings:UnusedDeclaration
shouldNotifyNowback to summary
private static boolean shouldNotifyNow(final int size, final int available)
Parameters
size:int

the amount of data that must be available for a ReadHandler to be notified.

available:int

the amount of data currently available.

Returns:boolean

true if the handler should be notified during a call to notifyAvailable(ReadHandler) or notifyAvailable(ReadHandler, int), otherwise false

skipback to summary
public long skip(final long n, final boolean block) throws IOException

Deprecated

pls. use skip(long), the block parameter will be ignored

Skips the specified number of bytes/characters.

Annotations
@Deprecated
See Also
java.io.InputStream#skip(long), java.io.Reader#skip(long)
skipback to summary
public long skip(final long n) throws IOException

Skips the specified number of bytes/characters.

See Also
java.io.InputStream#skip(long), java.io.Reader#skip(long)
terminateback to summary
public void terminate()

Invoke ReadHandler#onError(Throwable) (assuming a ReadHandler is available) } passing a {#link CancellationException} if the current Connection is open, or a {#link EOFException} if the connection was unexpectedly closed.

Since
2.0.1
toCompositeInputContentBufferback to summary
private CompositeBuffer toCompositeInputContentBuffer()
updateInputContentBufferback to summary
protected void updateInputContentBuffer(final Buffer buffer) throws IOException