org.glassfish.grizzly.streams
Class AbstractStreamReader

java.lang.Object
  extended by org.glassfish.grizzly.streams.AbstractStreamReader
All Implemented Interfaces:
java.io.Closeable, Stream, StreamReader
Direct Known Subclasses:
DefaultStreamReader, TransformerStreamReader

public abstract class AbstractStreamReader
extends java.lang.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
protected  Connection connection
           
protected  Input input
           
protected  java.util.concurrent.atomic.AtomicBoolean isClosed
           
 
Constructor Summary
protected AbstractStreamReader(Connection connection, Input streamInput)
          Create a new ByteBufferReader.
 
Method Summary
 int available()
          Return the number of bytes available for get calls.
 void close()
          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 StreamReader
 Buffer getBufferWindow()
          
 Connection getConnection()
          Get the Connection this StreamReader belongs to.
 boolean hasAvailable()
          Return true if StreamReader has available data, which could be read, or false otherwise.
 boolean isClosed()
          Returns true, if StreamReader has been closed, or false otherwise.
 boolean isSupportBufferWindow()
          
 GrizzlyFuture<java.lang.Integer> notifyAvailable(int size)
          Method returns Future, using which it's possible check if StreamReader has required amount of bytes available for reading reading.
 GrizzlyFuture<java.lang.Integer> notifyAvailable(int size, CompletionHandler<java.lang.Integer> completionHandler)
          Method returns Future, using which it's possible check if StreamReader has required amount of bytes available for reading reading.
 GrizzlyFuture<java.lang.Integer> notifyCondition(Condition condition)
          Method returns Future, using which it's possible check if StreamReader meets specific Condition.
 GrizzlyFuture<java.lang.Integer> notifyCondition(Condition condition, CompletionHandler<java.lang.Integer> completionHandler)
          Method returns Future, using which it's possible check if StreamReader meets specific Condition.
 boolean readBoolean()
          Get the next boolean in the stream.
 void readBooleanArray(boolean[] data)
          Fill data with booleans (byte 1=true, 0=false) from the stream.
 byte readByte()
          Get the next byte in the stream.
 void readByteArray(byte[] data)
          Fill data with bytes from the stream.
 void readByteArray(byte[] data, int offset, int length)
          Fill data with bytes from the stream.
 void readBytes(Buffer buffer)
          Fill the buffer with data from the stream (that is, copy data from the stream to fill buffer from position to limit).
 char readChar()
          Get the next character in the stream.
 void readCharArray(char[] data)
          Fill data with characters from the stream.
 double readDouble()
          Get the next double in the stream.
 void readDoubleArray(double[] data)
          Fill data with characters from the stream.
 float readFloat()
          Get the next float in the stream.
 void readFloatArray(float[] data)
          Fill data with characters from the stream.
 int readInt()
          Get the next int in the stream.
 void readIntArray(int[] data)
          Fill data with characters from the stream.
 long readLong()
          Get the next long in the stream.
 void readLongArray(long[] data)
          Fill data with characters from the stream.
 short readShort()
          Get the next short in the stream.
 void readShortArray(short[] data)
          Fill data with characters from the stream.
 void skip(int length)
           
 Buffer takeBufferWindow()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connection

protected final Connection connection

input

protected final Input input

isClosed

protected final java.util.concurrent.atomic.AtomicBoolean isClosed
Constructor Detail

AbstractStreamReader

protected AbstractStreamReader(Connection connection,
                               Input streamInput)
Create a new ByteBufferReader.

Parameters:
connection - the Connection to be associated with this AbstractStreamReader
streamInput - the stream source
Method Detail

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Get the next boolean in the stream. Requires 1 byte.

Specified by:
readBoolean in interface StreamReader
Throws:
java.io.IOException

readByte

public byte readByte()
              throws java.io.IOException
Get the next byte in the stream. Requires 1 byte.

Specified by:
readByte in interface StreamReader
Throws:
java.io.IOException

readChar

public char readChar()
              throws java.io.IOException
Get the next character in the stream. Requires 2 bytes.

Specified by:
readChar in interface StreamReader
Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Get the next short in the stream. Requires 2 bytes.

Specified by:
readShort in interface StreamReader
Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Get the next int in the stream. Requires 4 bytes.

Specified by:
readInt in interface StreamReader
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Get the next long in the stream. Requires 8 bytes.

Specified by:
readLong in interface StreamReader
Throws:
java.io.IOException

readFloat

public final float readFloat()
                      throws java.io.IOException
Get the next float in the stream. Requires 4 bytes.

Specified by:
readFloat in interface StreamReader
Throws:
java.io.IOException

readDouble

public final double readDouble()
                        throws java.io.IOException
Get the next double in the stream. Requires 8 bytes.

Specified by:
readDouble in interface StreamReader
Throws:
java.io.IOException

readBooleanArray

public void readBooleanArray(boolean[] data)
                      throws java.io.IOException
Fill 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:
readBooleanArray in interface StreamReader
Throws:
java.io.IOException

readByteArray

public void readByteArray(byte[] data)
                   throws java.io.IOException
Fill data with bytes from the stream. If this method returns normally, data has been filled completely. Requires data.length bytes.

Specified by:
readByteArray in interface StreamReader
Throws:
java.io.IOException

readByteArray

public void readByteArray(byte[] data,
                          int offset,
                          int length)
                   throws java.io.IOException
Fill data with bytes from the stream. If this method returns normally, data has been filled completely. Requires data.length bytes.

Specified by:
readByteArray in interface StreamReader
Throws:
java.io.IOException

readBytes

public void readBytes(Buffer buffer)
               throws java.io.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:
readBytes in interface StreamReader
Throws:
java.io.IOException

readCharArray

public void readCharArray(char[] data)
                   throws java.io.IOException
Fill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 2*data.length bytes.

Specified by:
readCharArray in interface StreamReader
Throws:
java.io.IOException

readShortArray

public void readShortArray(short[] data)
                    throws java.io.IOException
Fill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 2*data.length bytes.

Specified by:
readShortArray in interface StreamReader
Throws:
java.io.IOException

readIntArray

public void readIntArray(int[] data)
                  throws java.io.IOException
Fill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 4*data.length bytes.

Specified by:
readIntArray in interface StreamReader
Throws:
java.io.IOException

readLongArray

public void readLongArray(long[] data)
                   throws java.io.IOException
Fill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 8*data.length bytes.

Specified by:
readLongArray in interface StreamReader
Throws:
java.io.IOException

readFloatArray

public void readFloatArray(float[] data)
                    throws java.io.IOException
Fill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 4*data.length bytes.

Specified by:
readFloatArray in interface StreamReader
Throws:
java.io.IOException

readDoubleArray

public void readDoubleArray(double[] data)
                     throws java.io.IOException
Fill data with characters from the stream. If this method returns normally, data has been filled completely. Requires 8*data.length bytes.

Specified by:
readDoubleArray in interface StreamReader
Throws:
java.io.IOException

skip

public void skip(int length)
Specified by:
skip in interface StreamReader

decode

public <E> GrizzlyFuture<E> decode(Transformer<Stream,E> decoder)
Read and decode data from the StreamReader

Specified by:
decode in interface StreamReader
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:
decode in interface StreamReader
Type Parameters:
E - decoded data type
Parameters:
decoder - Transformer
completionHandler - CompletionHandler, which will be notified, when decoder will become ready.
Returns:
Future, which will hold the decoding state.

notifyAvailable

public GrizzlyFuture<java.lang.Integer> notifyAvailable(int size)
Method returns Future, using which it's possible check if StreamReader has required amount of bytes available for reading reading.

Specified by:
notifyAvailable in interface StreamReader
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<java.lang.Integer> notifyAvailable(int size,
                                                        CompletionHandler<java.lang.Integer> completionHandler)
Method returns Future, using which it's possible check if StreamReader has required amount of bytes available for reading reading. CompletionHandler is also passed to get notified, once required number of bytes will become available for reading.

Specified by:
notifyAvailable in interface StreamReader
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<java.lang.Integer> notifyCondition(Condition condition)
Method returns Future, using which it's possible check if StreamReader meets specific Condition.

Specified by:
notifyCondition in interface StreamReader
Parameters:
condition - Condition StreamReader should meet.
Returns:
Future, using which it's possible to check whether StreamReader meets the required Condition.

notifyCondition

public GrizzlyFuture<java.lang.Integer> notifyCondition(Condition condition,
                                                        CompletionHandler<java.lang.Integer> completionHandler)
Method returns Future, using which it's possible check if StreamReader meets specific Condition. CompletionHandler is also passed to get notified, once the Condition will be satisfied.

Specified by:
notifyCondition in interface StreamReader
Parameters:
condition - Condition StreamReader should meet.
completionHandler - CompletionHandler, which will be notified, once the Condition will be satisfied.
Returns:
Future, using which it's possible to check whether StreamReader meets the required Condition.

close

public void close()
Closes the StreamReader and causes all subsequent method calls on this object to throw IllegalStateException.

Specified by:
close in interface java.io.Closeable

isClosed

public boolean isClosed()
Returns true, if StreamReader has been closed, or false otherwise.

Specified by:
isClosed in interface StreamReader
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:
hasAvailable in interface StreamReader
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:
available in interface StreamReader

isSupportBufferWindow

public boolean isSupportBufferWindow()

Specified by:
isSupportBufferWindow in interface StreamReader

getBufferWindow

public Buffer getBufferWindow()

Specified by:
getBufferWindow in interface StreamReader

takeBufferWindow

public Buffer takeBufferWindow()

Specified by:
takeBufferWindow in interface StreamReader

getConnection

public Connection getConnection()
Get the Connection this StreamReader belongs to.

Specified by:
getConnection in interface Stream
Returns:
the Connection this StreamReader belongs to.


Copyright © 2011 Oracle Corpration. All Rights Reserved.