com.sun.grizzly.streams
Class AbstractStreamReader

java.lang.Object
  extended by com.sun.grizzly.streams.AbstractStreamReader
All Implemented Interfaces:
StreamReader, Closeable
Direct Known Subclasses:
StreamReaderDecorator, TCPNIOStreamReader, UDPNIOStreamReader

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

Nested Class Summary
protected static class AbstractStreamReader.NotifyObject
           
 
Field Summary
protected  int bufferSize
           
protected  LinkedList dataRecords
           
protected  AbstractStreamReader.NotifyObject notifyObject
           
protected  Object sync
           
protected  long timeoutMillis
           
 
Constructor Summary
  AbstractStreamReader()
           
protected AbstractStreamReader(Connection connection)
          Create a new ByteBufferReader.
 
Method Summary
protected  boolean append(Object data)
           
 boolean appendBuffer(Buffer buffer)
          Add more data to the end of the stream.
 int availableDataSize()
          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.
protected  Object current()
           
protected  void ensureRead()
           
protected  void ensureRead(boolean readIfEmpty)
           
 void finishBuffer()
          Finishes processing of the current StreamReader's source Buffer.
 Buffer getBuffer()
          Return the current StreamReader's source Buffer.
 int getBufferSize()
          Get the preferred Buffer size to be used for StreamReader read operations.
 Connection getConnection()
          Get the Connection this StreamReader belongs to.
 long getTimeout(TimeUnit timeunit)
          Get the timeout for StreamReader I/O operations.
 boolean hasAvailableData()
          Return true if StreamReader has available data, which could be read, or false otherwise.
 boolean isBlocking()
          Returns the StreamReader mode.
 boolean isClosed()
          Returns true, if StreamReader has been closed, or false otherwise.
protected  Buffer newBuffer(int size)
           
 Future<Integer> notifyAvailable(int size)
          Method returns Future, using which it's possible check if StreamReader has required amound of bytes available for reading reading.
 Future<Integer> notifyAvailable(int size, CompletionHandler<Integer> completionHandler)
          Method returns Future, using which it's possible check if StreamReader has required amound of bytes available for reading reading.
 Future<Integer> notifyCondition(Condition<StreamReader> condition)
          Method returns Future, using which it's possible check if StreamReader meets specific Condition.
protected  Object poll()
           
protected  Buffer pollBuffer()
           
protected  boolean prepend(Object data)
           
 boolean prependBuffer(Buffer buffer)
          Add more data to the beginning of the stream.
protected abstract  Object read0()
           
 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.
 Buffer readBuffer()
          Returns the current StreamReader's source Buffer and makes next available Buffer current.
 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 setBlocking(boolean isBlocking)
          Sets the StreamReader mode.
 void setBufferSize(int size)
          Set the preferred Buffer size to be used for StreamReader read operations.
 void setConnection(Connection connection)
           
 void setTimeout(long timeout, TimeUnit timeunit)
          Set the timeout for StreamReader I/O operations.
protected abstract  Buffer unwrap(Object data)
           
protected abstract  Object wrap(Buffer buffer)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sun.grizzly.streams.StreamReader
notifyCondition
 

Field Detail

bufferSize

protected int bufferSize

timeoutMillis

protected long timeoutMillis

sync

protected final Object sync

dataRecords

protected LinkedList dataRecords

notifyObject

protected AbstractStreamReader.NotifyObject notifyObject
Constructor Detail

AbstractStreamReader

public AbstractStreamReader()

AbstractStreamReader

protected AbstractStreamReader(Connection connection)
Create a new ByteBufferReader.

Parameters:
timeout - Time in milliseconds to wait for more data: If 0, the reader will never block.
Throws:
TimeoutException - when a read operation times out waiting for more data.
Method Detail

isBlocking

public boolean isBlocking()
Returns the StreamReader mode. true, if StreamReader is operating in blocking mode, or false otherwise.

Specified by:
isBlocking in interface StreamReader
Returns:
the StreamReader mode.

setBlocking

public void setBlocking(boolean isBlocking)
Sets the StreamReader mode.

Specified by:
setBlocking in interface StreamReader
Parameters:
isBlocking - true, if StreamReader is operating in blocking mode, or false otherwise.

prependBuffer

public boolean prependBuffer(Buffer buffer)
Add more data to the beginning of the stream.

Specified by:
prependBuffer in interface StreamReader
Returns:
true, if buffer was prepended to the StreamReader, or false otherwise

prepend

protected boolean prepend(Object data)

appendBuffer

public boolean appendBuffer(Buffer buffer)
Add more data to the end of the stream.

Specified by:
appendBuffer in interface StreamReader
Returns:
true, if buffer was appended to the StreamReader, or false otherwise

append

protected boolean append(Object data)

close

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

Specified by:
close in interface 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.

ensureRead

protected void ensureRead()
                   throws IOException
Throws:
IOException

ensureRead

protected void ensureRead(boolean readIfEmpty)
                   throws IOException
Throws:
IOException

pollBuffer

protected Buffer pollBuffer()

poll

protected Object poll()

hasAvailableData

public final boolean hasAvailableData()
Return true if StreamReader has available data, which could be read, or false otherwise.

Specified by:
hasAvailableData in interface StreamReader
Returns:
true if StreamReader has available data, which could be read, or false otherwise.

availableDataSize

public int availableDataSize()
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:
availableDataSize in interface StreamReader

readBoolean

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

Specified by:
readBoolean in interface StreamReader
Throws:
IOException

readByte

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

Specified by:
readByte in interface StreamReader
Throws:
IOException

readChar

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

Specified by:
readChar in interface StreamReader
Throws:
IOException

readShort

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

Specified by:
readShort in interface StreamReader
Throws:
IOException

readInt

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

Specified by:
readInt in interface StreamReader
Throws:
IOException

readLong

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

Specified by:
readLong in interface StreamReader
Throws:
IOException

readFloat

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

Specified by:
readFloat in interface StreamReader
Throws:
IOException

readDouble

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

Specified by:
readDouble in interface StreamReader
Throws:
IOException

readBooleanArray

public void readBooleanArray(boolean[] data)
                      throws 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:
IOException

readByteArray

public void readByteArray(byte[] data)
                   throws 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:
IOException

readByteArray

public void readByteArray(byte[] data,
                          int offset,
                          int length)
                   throws 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:
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:
readBytes in interface StreamReader
Throws:
IOException

readCharArray

public void readCharArray(char[] data)
                   throws 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:
IOException

readShortArray

public void readShortArray(short[] data)
                    throws 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:
IOException

readIntArray

public void readIntArray(int[] data)
                  throws 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:
IOException

readLongArray

public void readLongArray(long[] data)
                   throws 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:
IOException

readFloatArray

public void readFloatArray(float[] data)
                    throws 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:
IOException

readDoubleArray

public void readDoubleArray(double[] data)
                     throws 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:
IOException

notifyAvailable

public Future<Integer> notifyAvailable(int size)
Method returns Future, using which it's possible check if StreamReader has required amound 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 Future<Integer> notifyAvailable(int size,
                                       CompletionHandler<Integer> completionHandler)
Method returns Future, using which it's possible check if StreamReader has required amound 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 Future<Integer> notifyCondition(Condition<StreamReader> 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.

readBuffer

public Buffer readBuffer()
                  throws IOException
Returns the current StreamReader's source Buffer and makes next available Buffer current.

Specified by:
readBuffer in interface StreamReader
Returns:
the current StreamReader's source Buffer
Throws:
IOException

getBuffer

public Buffer getBuffer()
Return the current StreamReader's source Buffer. Unlike StreamReader.readBuffer(), this method doesn't make any internal updates of current Buffer.

Specified by:
getBuffer in interface StreamReader
Returns:
the current StreamReader's source Buffer.

current

protected Object current()

finishBuffer

public void finishBuffer()
Finishes processing of the current StreamReader's source Buffer. This method doesn't call Buffer.dispose().

Specified by:
finishBuffer in interface StreamReader

getConnection

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

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

setConnection

public void setConnection(Connection connection)

newBuffer

protected Buffer newBuffer(int size)

getBufferSize

public int getBufferSize()
Get the preferred Buffer size to be used for StreamReader read operations.

Specified by:
getBufferSize in interface StreamReader
Returns:
the preferred Buffer size to be used for StreamReader read operations.

setBufferSize

public void setBufferSize(int size)
Set the preferred Buffer size to be used for StreamReader read operations.

Specified by:
setBufferSize in interface StreamReader
Parameters:
size - the preferred Buffer size to be used for StreamReader read operations.

getTimeout

public long getTimeout(TimeUnit timeunit)
Get the timeout for StreamReader I/O operations.

Specified by:
getTimeout in interface StreamReader
Parameters:
timeunit - timeout unit TimeUnit.
Returns:
the timeout for StreamReader I/O operations.

setTimeout

public void setTimeout(long timeout,
                       TimeUnit timeunit)
Set the timeout for StreamReader I/O operations.

Specified by:
setTimeout in interface StreamReader
Parameters:
timeout - the timeout for StreamReader I/O operations.
timeunit - timeout unit TimeUnit.

read0

protected abstract Object read0()
                         throws IOException
Throws:
IOException

wrap

protected abstract Object wrap(Buffer buffer)

unwrap

protected abstract Buffer unwrap(Object data)


Copyright © 2009 SUN Microsystems. All Rights Reserved.