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 Details

  • Constructor Details

    • 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 Details

    • 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
    • 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<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<Integer> notifyAvailable(int size, CompletionHandler<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<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<Integer> notifyCondition(Condition condition, CompletionHandler<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 AutoCloseable
      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.
    • 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.