Class JBBPBitInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.igormaznitsa.jbbp.io.JBBPBitInputStream
All Implemented Interfaces:
JBBPCountableBitStream, Closeable, AutoCloseable

public class JBBPBitInputStream extends FilterInputStream implements JBBPCountableBitStream
A Filter stream implementing a countable bit stream interface. It allows read not only bytes but also bits from an input stream. The Class is not a thread-safe one.
Since:
1.0
  • Field Details

    • INITIAL_ARRAY_BUFFER_SIZE

      protected static final int INITIAL_ARRAY_BUFFER_SIZE
      The Initial an Array buffer size for whole stream read.
  • Constructor Details

    • JBBPBitInputStream

      public JBBPBitInputStream(InputStream in)
      A Constructor, the LSB0 bit order will be used by default.
      Parameters:
      in - an input stream to be filtered.
    • JBBPBitInputStream

      public JBBPBitInputStream(InputStream in, JBBPBitOrder order)
      A Constructor.
      Parameters:
      in - an input stream to be filtered.
      order - a bit order mode for the filter.
      See Also:
  • Method Details

    • isDetectedArrayLimit

      public boolean isDetectedArrayLimit()
      Flag shows that read of array was stopped for array limiter restrictions.
      Returns:
      true if array limiter restrictions detected, false otherwise
      Since:
      2.1.0
      See Also:
    • setDetectedArrayLimit

      public void setDetectedArrayLimit(boolean value)
      Set value for array limit detected flag. It is important to set the flag for correct processing of parsing.
      Parameters:
      value - true or false.
      Since:
      2.1.0
      See Also:
    • readBoolArray

      public boolean[] readBoolArray(int items) throws IOException
      Read array of boolean values.
      Parameters:
      items - number of items to be read, if less than zero then read whole stream till the end
      Returns:
      read values as boolean array
      Throws:
      IOException - it will be thrown for transport error
    • readBoolArray

      public boolean[] readBoolArray(int items, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read array of boolean values.
      Parameters:
      items - number of items to be read, if less than zero then read whole stream till the end
      arraySizeLimiter - limiter provides number of allowed array items, must not be null
      Returns:
      read values as boolean array
      Throws:
      IOException - it will be thrown for transport error
      JBBPReachedArraySizeLimitException - if reached limit of read
      Since:
      2.1.0
    • read

      public int read(byte[] array, int offset, int length) throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • readBitsArray

      public byte[] readBitsArray(int items, JBBPBitNumber bitNumber) throws IOException
      Read array of bit sequence.
      Parameters:
      items - number of items to be read, if less than zero then read whole stream till the end
      bitNumber - bit number for each bit sequence item, must be 1..8
      Returns:
      array of read bit items as a byte array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
    • readBitsArray

      public byte[] readBitsArray(int items, JBBPBitNumber bitNumber, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read array of bit sequence.
      Parameters:
      items - number of items to be read, if less than zero then read whole
      bitNumber - bit number for each bit sequence item, must be 1..8 stream till the end
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      array of read bit items as a byte array
      Throws:
      JBBPReachedArraySizeLimitException - if reached limit of read
      IOException - it will be thrown for any transport problem during the operation
      Since:
      2.1.0
    • readByteArray

      public byte[] readByteArray(int items) throws IOException
      Read number of bytes for the stream.
      Parameters:
      items - number of items to be read, if less than zero then read whole stream till the end
      Returns:
      read byte items as a byte array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
    • readByteArray

      public byte[] readByteArray(int items, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of bytes for the stream.
      Parameters:
      items - number of items to be read, if less than zero then read whole stream till the end
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read byte items as a byte array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      Since:
      2.1.0
    • readByteArray

      public byte[] readByteArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of bytes for the stream. Invert their order if byte order is LITTLE_ENDIAN
      Parameters:
      items - number of items to be read, if less than zero then read whole stream till the end
      byteOrder - desired order of bytes
      Returns:
      read byte items as a byte array, if byte order is LITTLE_ENDIAN then the result array will be reversed one
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      Since:
      1.3.0
      See Also:
    • readByteArray

      public byte[] readByteArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of bytes for the stream. Invert their order if byte order is LITTLE_ENDIAN
      Parameters:
      items - number of items to be read, if less than zero then read whole stream till the end
      byteOrder - desired order of bytes
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read byte items as a byte array, if byte order is LITTLE_ENDIAN then the result array will be reversed one
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of array read
      Since:
      2.1.0
      See Also:
    • mark

      public void mark(int readLimit)
      Overrides:
      mark in class FilterInputStream
    • readShortArray

      public short[] readShortArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of short items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode short values
      Returns:
      read items as a short array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      See Also:
    • readShortArray

      public short[] readShortArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of short items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode short values
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read items as a short array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of array read
      Since:
      2.1.0
      See Also:
    • readUIntArray

      public long[] readUIntArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of unsigned integer items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      Returns:
      read items as an unsigned integer array represented through long
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      Since:
      2.0.4
      See Also:
    • readUIntArray

      public long[] readUIntArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of unsigned integer items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read items as an unsigned integer array represented through long
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of array read
      Since:
      2.1.0
      See Also:
    • readUShortArray

      public char[] readUShortArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of unsigned short items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode short values
      Returns:
      read items as a char array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      Since:
      1.3
      See Also:
    • readUShortArray

      public char[] readUShortArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of unsigned short items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode short values
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read items as a char array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of array read
      Since:
      2.1.0
      See Also:
    • readIntArray

      public int[] readIntArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of integer items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      Returns:
      read items as an integer array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      See Also:
    • readIntArray

      public int[] readIntArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of integer items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read items as an integer array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of array read
      See Also:
    • readFloatArray

      public float[] readFloatArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of float items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      Returns:
      read items as float array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      Since:
      1.4.0
      See Also:
    • readFloatArray

      public float[] readFloatArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of float items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read items as float array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of array read
      Since:
      2.1.0
      See Also:
    • readUnsignedShort

      public int readUnsignedShort(JBBPByteOrder byteOrder) throws IOException
      Read a unsigned short value from the stream.
      Parameters:
      byteOrder - the order of bytes to be used to decode the read value
      Returns:
      the unsigned short value read from stream
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      EOFException - if the end of the stream has been reached
      See Also:
    • readInt

      public int readInt(JBBPByteOrder byteOrder) throws IOException
      Read an integer value from the stream.
      Parameters:
      byteOrder - the order of bytes to be used to decode the read value
      Returns:
      the integer value from the stream
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      EOFException - if the end of the stream has been reached
      See Also:
    • readUInt

      public long readUInt(JBBPByteOrder byteOrder) throws IOException
      Read an unsigned integer value from the stream.
      Parameters:
      byteOrder - the order of bytes to be used to decode the read value
      Returns:
      the unsigned integer value from the stream
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      EOFException - if the end of the stream has been reached
      Since:
      2.0.4
      See Also:
    • readFloat

      public float readFloat(JBBPByteOrder byteOrder) throws IOException
      Read a float value from the stream.
      Parameters:
      byteOrder - the order of bytes to be used to decode the read value
      Returns:
      the float value from the stream
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      EOFException - if the end of the stream has been reached
      Since:
      1.4.0
      See Also:
    • readLong

      public long readLong(JBBPByteOrder byteOrder) throws IOException
      Read a long value from the stream.
      Parameters:
      byteOrder - the order of bytes to be used to decode the read value
      Returns:
      the long value from stream
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      EOFException - if the end of the stream has been reached
      See Also:
    • readDouble

      public double readDouble(JBBPByteOrder byteOrder) throws IOException
      Read a double value from the stream.
      Parameters:
      byteOrder - the order of bytes to be used to decode the read value
      Returns:
      the double value from stream
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      EOFException - if the end of the stream has been reached
      Since:
      1.4.0
      See Also:
    • getCounter

      public long getCounter()
      Get the current fully read byte counter.
      Specified by:
      getCounter in interface JBBPCountableBitStream
      Returns:
      the number of bytes read fully from the stream
    • getBitBuffer

      public int getBitBuffer()
      Get the value of the inside bit buffer
      Specified by:
      getBitBuffer in interface JBBPCountableBitStream
      Returns:
      the bit buffer state
    • getBufferedBitsNumber

      public int getBufferedBitsNumber()
      Get the number of buffered bits in the inside bit buffer
      Specified by:
      getBufferedBitsNumber in interface JBBPCountableBitStream
      Returns:
      the number of buffered bits in the bit buffer
    • getBitOrder

      public JBBPBitOrder getBitOrder()
      Get the bit order option for read operations.
      Specified by:
      getBitOrder in interface JBBPCountableBitStream
      Returns:
      the bit order parameter
      See Also:
    • readBitField

      public byte readBitField(JBBPBitNumber numOfBitsToRead) throws IOException
      Read bit field from the stream, if it is impossible then IOException will be thrown.
      Parameters:
      numOfBitsToRead - field width, must not be null
      Returns:
      read value from the stream
      Throws:
      IOException - it will be thrown if EOF or troubles to read the stream
      Since:
      1.3.0
    • readBits

      public int readBits(JBBPBitNumber numOfBitsToRead) throws IOException
      Read number of bits from the input stream. It reads bits from input stream since 0 bit and make reversion to return bits in the right order when 0 bit is 0 bit. if the stream is completed earlier than the data read then reading is just stopped and read value returned. The First read bit is placed as 0th bit.
      Parameters:
      numOfBitsToRead - the number of bits to be read, must be 1..8
      Returns:
      the read bits as integer, -1 if the end of stream has been reached
      Throws:
      IOException - it will be thrown for transport errors to be read
      NullPointerException - if number of bits to be read is null
    • readBoolean

      public boolean readBoolean() throws IOException
      Read a boolean value saved as a byte.
      Returns:
      true if the value is not zero, false if the value is zero
      Throws:
      IOException - it will be thrown for transport errors.
    • readByte

      public int readByte() throws IOException
      Read a byte value.
      Returns:
      the byte value read from the stream
      Throws:
      IOException - it will be thrown for transport errors.
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class FilterInputStream
      Throws:
      IOException
    • readLongArray

      public long[] readLongArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of long items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      Returns:
      read items as a long array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      See Also:
    • readLongArray

      public long[] readLongArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of long items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read items as a long array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of read
      Since:
      2.1.0
      See Also:
    • align

      public void align(long alignByteNumber) throws IOException
      Read padding bytes from the stream and ignore them to align the stream counter.
      Parameters:
      alignByteNumber - the byte number to align the stream
      Throws:
      IOException - it will be thrown for transport errors
      EOFException - it will be thrown if the stream end has been reached the before align border.
    • skip

      public long skip(long numOfBytes) throws IOException
      Overrides:
      skip in class FilterInputStream
      Throws:
      IOException
    • readDoubleArray

      public double[] readDoubleArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read number of double items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      Returns:
      read items as a double array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      Since:
      1.4.0
      See Also:
    • readDoubleArray

      public double[] readDoubleArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read number of double items from the input stream.
      Parameters:
      items - number of items to be read from the input stream, if less than zero then all stream till the end will be read
      byteOrder - the order of bytes to be used to decode values
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      read items as a double array
      Throws:
      IOException - it will be thrown for any transport problem during the operation
      JBBPReachedArraySizeLimitException - if reached limit of array read
      Since:
      2.1.0
      See Also:
    • alignByte

      public void alignByte()
      Align read byte. If there are unread bits in inside bit buffer, it will be aligned to read the next byte from stream instead of reading from the bit buffer.
    • hasAvailableData

      public boolean hasAvailableData() throws IOException
      Check that there is available data to read from stream. NB: It will read one byte from the stream and will save it into inside bit buffer.
      Returns:
      true if there is data to be read
      Throws:
      IOException - it will be thrown for transport errors
    • resetCounter

      public void resetCounter()
      Reset the byte counter for the stream. The Inside bit buffer will be reset also if it is not full.
      Specified by:
      resetCounter in interface JBBPCountableBitStream
      See Also:
    • read

      public int read(byte[] array) throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • read

      public int read() throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • readString

      public String readString(JBBPByteOrder byteOrder) throws IOException
      Read string in UTF8 format.
      Parameters:
      byteOrder - byte order, must not be null
      Returns:
      read string, can be null
      Throws:
      IOException - it will be thrown for transport error or wrong format
      Since:
      1.4.0
      See Also:
    • readStringArray

      public String[] readStringArray(int items, JBBPByteOrder byteOrder) throws IOException
      Read array of strings from stream.
      Parameters:
      items - number of items, or -1 if read whole stream
      byteOrder - order of bytes in structure, must not be null
      Returns:
      array, it can contain null among values, must not be null
      Throws:
      IOException - thrown for transport errors
      Since:
      1.4.0
      See Also:
    • readStringArray

      public String[] readStringArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) throws IOException
      Read array of strings from stream.
      Parameters:
      items - number of items, or -1 if read whole stream
      byteOrder - order of bytes in structure, must not be null
      arraySizeLimiter - limiter provides number of allowed array items for non-limited array, must not be null
      Returns:
      array, it can contain null among values, must not be null
      Throws:
      IOException - thrown for transport errors
      JBBPReachedArraySizeLimitException - if reached limit of array read
      Since:
      2.1.0
      See Also: