Package org.glassfish.grizzly.streams
Class AbstractStreamReader
- java.lang.Object
-
- org.glassfish.grizzly.streams.AbstractStreamReader
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Stream,StreamReader
- Direct Known Subclasses:
DefaultStreamReader,TransformerStreamReader
public abstract class AbstractStreamReader extends Object implements StreamReader
Each method reads data from the current ByteBuffer. If not enough data is present in the current ByteBuffer, discard is called on the current ByteBuffer and we advance to the next ByteBuffer, or block if not enough data is present. If close() is called, all subsequent method calls will throw an IllegalStateException, and any threads blocked waiting for more data will be unblocked, and continue with an IllegalStateException from the blocking method call.dataReceived and close may be safely invoked by multiple threads. The other methods must be invoked only by one thread, which is the reader of this data stream.
- Author:
- Ken Cavanaugh, Alexey Stashok
-
-
Field Summary
Fields Modifier and Type Field Description protected Connectionconnectionprotected Inputinputprotected AtomicBooleanisClosed
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractStreamReader(Connection connection, Input streamInput)Create a new ByteBufferReader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Return the number of bytes available for get calls.voidclose()Closes the StreamReader and causes all subsequent method calls on this object to throw IllegalStateException.<E> GrizzlyFuture<E>decode(Transformer<Stream,E> decoder)Read and decode data from the StreamReader<E> GrizzlyFuture<E>decode(Transformer<Stream,E> decoder, CompletionHandler<E> completionHandler)Read and decode data from the StreamReaderBuffergetBufferWindow()ConnectiongetConnection()Get theConnectionthis StreamReader belongs to.booleanhasAvailable()Return true if StreamReader has available data, which could be read, or false otherwise.booleanisClosed()Returns true, if StreamReader has been closed, or false otherwise.booleanisSupportBufferWindow()GrizzlyFuture<Integer>notifyAvailable(int size)Method returnsFuture, using which it's possible check if StreamReader has required amount of bytes available for reading reading.GrizzlyFuture<Integer>notifyAvailable(int size, CompletionHandler<Integer> completionHandler)Method returnsFuture, using which it's possible check if StreamReader has required amount of bytes available for reading reading.GrizzlyFuture<Integer>notifyCondition(Condition condition)GrizzlyFuture<Integer>notifyCondition(Condition condition, CompletionHandler<Integer> completionHandler)booleanreadBoolean()Get the next boolean in the stream.voidreadBooleanArray(boolean[] data)Fill data with booleans (byte 1=true, 0=false) from the stream.bytereadByte()Get the next byte in the stream.voidreadByteArray(byte[] data)Fill data with bytes from the stream.voidreadByteArray(byte[] data, int offset, int length)Fill data with bytes from the stream.voidreadBytes(Buffer buffer)Fill the buffer with data from the stream (that is, copy data from the stream to fill buffer from position to limit).charreadChar()Get the next character in the stream.voidreadCharArray(char[] data)Fill data with characters from the stream.doublereadDouble()Get the next double in the stream.voidreadDoubleArray(double[] data)Fill data with characters from the stream.floatreadFloat()Get the next float in the stream.voidreadFloatArray(float[] data)Fill data with characters from the stream.intreadInt()Get the next int in the stream.voidreadIntArray(int[] data)Fill data with characters from the stream.longreadLong()Get the next long in the stream.voidreadLongArray(long[] data)Fill data with characters from the stream.shortreadShort()Get the next short in the stream.voidreadShortArray(short[] data)Fill data with characters from the stream.voidskip(int length)BuffertakeBufferWindow()
-
-
-
Field Detail
-
connection
protected final Connection connection
-
input
protected final Input input
-
isClosed
protected final AtomicBoolean isClosed
-
-
Constructor Detail
-
AbstractStreamReader
protected AbstractStreamReader(Connection connection, Input streamInput)
Create a new ByteBufferReader.- Parameters:
connection- theConnectionto be associated with thisAbstractStreamReaderstreamInput- the stream source
-
-
Method Detail
-
readBoolean
public boolean readBoolean() throws IOExceptionGet the next boolean in the stream. Requires 1 byte.- Specified by:
readBooleanin interfaceStreamReader- Throws:
IOException
-
readByte
public byte readByte() throws IOExceptionGet the next byte in the stream. Requires 1 byte.- Specified by:
readBytein interfaceStreamReader- Throws:
IOException
-
readChar
public char readChar() throws IOExceptionGet the next character in the stream. Requires 2 bytes.- Specified by:
readCharin interfaceStreamReader- Throws:
IOException
-
readShort
public short readShort() throws IOExceptionGet the next short in the stream. Requires 2 bytes.- Specified by:
readShortin interfaceStreamReader- Throws:
IOException
-
readInt
public int readInt() throws IOExceptionGet the next int in the stream. Requires 4 bytes.- Specified by:
readIntin interfaceStreamReader- Throws:
IOException
-
readLong
public long readLong() throws IOExceptionGet the next long in the stream. Requires 8 bytes.- Specified by:
readLongin interfaceStreamReader- Throws:
IOException
-
readFloat
public final float readFloat() throws IOExceptionGet the next float in the stream. Requires 4 bytes.- Specified by:
readFloatin interfaceStreamReader- Throws:
IOException
-
readDouble
public final double readDouble() throws IOExceptionGet the next double in the stream. Requires 8 bytes.- Specified by:
readDoublein interfaceStreamReader- Throws:
IOException
-
readBooleanArray
public void readBooleanArray(boolean[] data) throws IOExceptionFill data with booleans (byte 1=true, 0=false) from the stream. If this method returns normally, data has been filled completely. Requires data.length bytes.- Specified by:
readBooleanArrayin interfaceStreamReader- Throws:
IOException
-
readByteArray
public void readByteArray(byte[] data) throws IOExceptionFill data with bytes from the stream. If this method returns normally, data has been filled completely. Requires data.length bytes.- Specified by:
readByteArrayin interfaceStreamReader- Throws:
IOException
-
readByteArray
public void readByteArray(byte[] data, int offset, int length) throws IOExceptionFill data with bytes from the stream. If this method returns normally, data has been filled completely. Requires data.length bytes.- Specified by:
readByteArrayin interfaceStreamReader- Throws:
IOException
-
readBytes
public void readBytes(Buffer buffer) throws IOException
Fill the buffer with data from the stream (that is, copy data from the stream to fill buffer from position to limit). This is useful when data must be read from one stream and then added to another stream for further processing.- Specified by:
readBytesin interfaceStreamReader- Throws:
IOException
-
readCharArray
public void readCharArray(char[] data) throws IOExceptionFill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 2*data.length bytes.- Specified by:
readCharArrayin interfaceStreamReader- Throws:
IOException
-
readShortArray
public void readShortArray(short[] data) throws IOExceptionFill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 2*data.length bytes.- Specified by:
readShortArrayin interfaceStreamReader- Throws:
IOException
-
readIntArray
public void readIntArray(int[] data) throws IOExceptionFill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 4*data.length bytes.- Specified by:
readIntArrayin interfaceStreamReader- Throws:
IOException
-
readLongArray
public void readLongArray(long[] data) throws IOExceptionFill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 8*data.length bytes.- Specified by:
readLongArrayin interfaceStreamReader- Throws:
IOException
-
readFloatArray
public void readFloatArray(float[] data) throws IOExceptionFill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 4*data.length bytes.- Specified by:
readFloatArrayin interfaceStreamReader- Throws:
IOException
-
readDoubleArray
public void readDoubleArray(double[] data) throws IOExceptionFill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 8*data.length bytes.- Specified by:
readDoubleArrayin interfaceStreamReader- Throws:
IOException
-
skip
public void skip(int length)
- Specified by:
skipin interfaceStreamReader
-
decode
public <E> GrizzlyFuture<E> decode(Transformer<Stream,E> decoder)
Read and decode data from the StreamReader- Specified by:
decodein interfaceStreamReader- Type Parameters:
E- decoded data type- Parameters:
decoder-Transformer- Returns:
Future, which will hold the decoding state.
-
decode
public <E> GrizzlyFuture<E> decode(Transformer<Stream,E> decoder, CompletionHandler<E> completionHandler)
Read and decode data from the StreamReader- Specified by:
decodein interfaceStreamReader- Type Parameters:
E- decoded data type- Parameters:
decoder-TransformercompletionHandler-CompletionHandler, which will be notified, when decoder will become ready.- Returns:
Future, which will hold the decoding state.
-
notifyAvailable
public GrizzlyFuture<Integer> notifyAvailable(int size)
Method returnsFuture, using which it's possible check if StreamReader has required amount of bytes available for reading reading.- Specified by:
notifyAvailablein interfaceStreamReader- Parameters:
size- number of bytes, which should become available on StreamReader.- Returns:
Future, using which it's possible to check whether StreamReader has required amount of bytes available for reading.
-
notifyAvailable
public GrizzlyFuture<Integer> notifyAvailable(int size, CompletionHandler<Integer> completionHandler)
Method returnsFuture, using which it's possible check if StreamReader has required amount of bytes available for reading reading.CompletionHandleris also passed to get notified, once required number of bytes will become available for reading.- Specified by:
notifyAvailablein interfaceStreamReader- Parameters:
size- number of bytes, which should become available on StreamReader.completionHandler-CompletionHandler, which will be notified once required number of bytes will become available.- Returns:
Future, using which it's possible to check whether StreamReader has required amount of bytes available for reading.
-
notifyCondition
public GrizzlyFuture<Integer> notifyCondition(Condition condition)
- Specified by:
notifyConditionin interfaceStreamReader- Parameters:
condition-ConditionStreamReader should meet.- Returns:
Future, using which it's possible to check whether StreamReader meets the requiredCondition.
-
notifyCondition
public GrizzlyFuture<Integer> notifyCondition(Condition condition, CompletionHandler<Integer> completionHandler)
Method returnsFuture, using which it's possible check if StreamReader meets specificCondition.CompletionHandleris also passed to get notified, once theConditionwill be satisfied.- Specified by:
notifyConditionin interfaceStreamReader- Parameters:
condition-ConditionStreamReader should meet.completionHandler-CompletionHandler, which will be notified, once theConditionwill be satisfied.- Returns:
Future, using which it's possible to check whether StreamReader meets the requiredCondition.
-
close
public void close()
Closes the StreamReader and causes all subsequent method calls on this object to throw IllegalStateException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
isClosed
public boolean isClosed()
Returns true, if StreamReader has been closed, or false otherwise.- Specified by:
isClosedin interfaceStreamReader- Returns:
- true, if StreamReader has been closed, or false otherwise.
-
hasAvailable
public final boolean hasAvailable()
Return true if StreamReader has available data, which could be read, or false otherwise.- Specified by:
hasAvailablein interfaceStreamReader- Returns:
- true if StreamReader has available data, which could be read, or false otherwise.
-
available
public int available()
Return the number of bytes available for get calls. An attempt to get more data than is present in the stream will either result in blocking (if isBlocking() returns true) or a BufferUnderflowException.- Specified by:
availablein interfaceStreamReader
-
isSupportBufferWindow
public boolean isSupportBufferWindow()
- Specified by:
isSupportBufferWindowin interfaceStreamReader
-
getBufferWindow
public Buffer getBufferWindow()
- Specified by:
getBufferWindowin interfaceStreamReader
-
takeBufferWindow
public Buffer takeBufferWindow()
- Specified by:
takeBufferWindowin interfaceStreamReader
-
getConnection
public Connection getConnection()
Get theConnectionthis StreamReader belongs to.- Specified by:
getConnectionin interfaceStream- Returns:
- the
Connectionthis StreamReader belongs to.
-
-