Class AbstractStreamReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, 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 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.
      • 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.
      • 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.lang.AutoCloseable
        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