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

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