Package com.igormaznitsa.jbbp.io
Class JBBPBitInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.igormaznitsa.jbbp.io.JBBPBitInputStream
- All Implemented Interfaces:
JBBPCountableBitStream,Closeable,AutoCloseable
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 Summary
FieldsModifier and TypeFieldDescriptionprotected static final intThe Initial an Array buffer size for whole stream read.Fields inherited from class java.io.FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionA Constructor, the LSB0 bit order will be used by default.JBBPBitInputStream(InputStream in, JBBPBitOrder order) A Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidalign(long alignByteNumber) Read padding bytes from the stream and ignore them to align the stream counter.voidAlign read byte.intGet the value of the inside bit bufferGet the bit order option for read operations.intGet the number of buffered bits in the inside bit bufferlongGet the current fully read byte counter.booleanCheck that there is available data to read from stream.booleanFlag shows that read of array was stopped for array limiter restrictions.voidmark(int readLimit) intread()intread(byte[] array) intread(byte[] array, int offset, int length) bytereadBitField(JBBPBitNumber numOfBitsToRead) Read bit field from the stream, if it is impossible then IOException will be thrown.intreadBits(JBBPBitNumber numOfBitsToRead) Read number of bits from the input stream.byte[]readBitsArray(int items, JBBPBitNumber bitNumber) Read array of bit sequence.byte[]readBitsArray(int items, JBBPBitNumber bitNumber, JBBPArraySizeLimiter arraySizeLimiter) Read array of bit sequence.boolean[]readBoolArray(int items) Read array of boolean values.boolean[]readBoolArray(int items, JBBPArraySizeLimiter arraySizeLimiter) Read array of boolean values.booleanRead a boolean value saved as a byte.intreadByte()Read a byte value.byte[]readByteArray(int items) Read number of bytes for the stream.byte[]readByteArray(int items, JBBPArraySizeLimiter arraySizeLimiter) Read number of bytes for the stream.byte[]readByteArray(int items, JBBPByteOrder byteOrder) Read number of bytes for the stream.byte[]readByteArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of bytes for the stream.doublereadDouble(JBBPByteOrder byteOrder) Read a double value from the stream.double[]readDoubleArray(int items, JBBPByteOrder byteOrder) Read number of double items from the input stream.double[]readDoubleArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of double items from the input stream.floatreadFloat(JBBPByteOrder byteOrder) Read a float value from the stream.float[]readFloatArray(int items, JBBPByteOrder byteOrder) Read number of float items from the input stream.float[]readFloatArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of float items from the input stream.intreadInt(JBBPByteOrder byteOrder) Read an integer value from the stream.int[]readIntArray(int items, JBBPByteOrder byteOrder) Read number of integer items from the input stream.int[]readIntArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of integer items from the input stream.longreadLong(JBBPByteOrder byteOrder) Read a long value from the stream.long[]readLongArray(int items, JBBPByteOrder byteOrder) Read number of long items from the input stream.long[]readLongArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of long items from the input stream.short[]readShortArray(int items, JBBPByteOrder byteOrder) Read number of short items from the input stream.short[]readShortArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of short items from the input stream.readString(JBBPByteOrder byteOrder) Read string in UTF8 format.String[]readStringArray(int items, JBBPByteOrder byteOrder) Read array of strings from stream.String[]readStringArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read array of strings from stream.longreadUInt(JBBPByteOrder byteOrder) Read an unsigned integer value from the stream.long[]readUIntArray(int items, JBBPByteOrder byteOrder) Read number of unsigned integer items from the input stream.long[]readUIntArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of unsigned integer items from the input stream.intreadUnsignedShort(JBBPByteOrder byteOrder) Read a unsigned short value from the stream.char[]readUShortArray(int items, JBBPByteOrder byteOrder) Read number of unsigned short items from the input stream.char[]readUShortArray(int items, JBBPByteOrder byteOrder, JBBPArraySizeLimiter arraySizeLimiter) Read number of unsigned short items from the input stream.voidreset()voidReset the byte counter for the stream.voidsetDetectedArrayLimit(boolean value) Set value for array limit detected flag.longskip(long numOfBytes) Methods inherited from class java.io.FilterInputStream
available, close, markSupportedMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
INITIAL_ARRAY_BUFFER_SIZE
protected static final int INITIAL_ARRAY_BUFFER_SIZEThe Initial an Array buffer size for whole stream read.
-
-
Constructor Details
-
JBBPBitInputStream
A Constructor, the LSB0 bit order will be used by default.- Parameters:
in- an input stream to be filtered.
-
JBBPBitInputStream
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
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
Read array of boolean values.- Parameters:
items- number of items to be read, if less than zero then read whole stream till the endarraySizeLimiter- 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 errorJBBPReachedArraySizeLimitException- if reached limit of read- Since:
- 2.1.0
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
readBitsArray
Read array of bit sequence.- Parameters:
items- number of items to be read, if less than zero then read whole stream till the endbitNumber- 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 wholebitNumber- bit number for each bit sequence item, must be 1..8 stream till the endarraySizeLimiter- 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 readIOException- it will be thrown for any transport problem during the operation- Since:
- 2.1.0
-
readByteArray
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
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 endarraySizeLimiter- 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
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 endbyteOrder- 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 endbyteOrder- desired order of bytesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- if reached limit of array read- Since:
- 2.1.0
- See Also:
-
mark
public void mark(int readLimit) - Overrides:
markin classFilterInputStream
-
readShortArray
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 readbyteOrder- 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 readbyteOrder- the order of bytes to be used to decode short valuesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- if reached limit of array read- Since:
- 2.1.0
- See Also:
-
readUIntArray
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 readbyteOrder- 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 readbyteOrder- the order of bytes to be used to decode valuesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- if reached limit of array read- Since:
- 2.1.0
- See Also:
-
readUShortArray
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 readbyteOrder- 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 readbyteOrder- the order of bytes to be used to decode short valuesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- if reached limit of array read- Since:
- 2.1.0
- See Also:
-
readIntArray
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 readbyteOrder- 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 readbyteOrder- the order of bytes to be used to decode valuesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- if reached limit of array read- See Also:
-
readFloatArray
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 readbyteOrder- 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 readbyteOrder- the order of bytes to be used to decode valuesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- if reached limit of array read- Since:
- 2.1.0
- See Also:
-
readUnsignedShort
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 operationEOFException- if the end of the stream has been reached- See Also:
-
readInt
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 operationEOFException- if the end of the stream has been reached- See Also:
-
readUInt
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 operationEOFException- if the end of the stream has been reached- Since:
- 2.0.4
- See Also:
-
readFloat
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 operationEOFException- if the end of the stream has been reached- Since:
- 1.4.0
- See Also:
-
readLong
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 operationEOFException- if the end of the stream has been reached- See Also:
-
readDouble
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 operationEOFException- 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:
getCounterin interfaceJBBPCountableBitStream- Returns:
- the number of bytes read fully from the stream
-
getBitBuffer
public int getBitBuffer()Get the value of the inside bit buffer- Specified by:
getBitBufferin interfaceJBBPCountableBitStream- Returns:
- the bit buffer state
-
getBufferedBitsNumber
public int getBufferedBitsNumber()Get the number of buffered bits in the inside bit buffer- Specified by:
getBufferedBitsNumberin interfaceJBBPCountableBitStream- Returns:
- the number of buffered bits in the bit buffer
-
getBitOrder
Get the bit order option for read operations.- Specified by:
getBitOrderin interfaceJBBPCountableBitStream- Returns:
- the bit order parameter
- See Also:
-
readBitField
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
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 readNullPointerException- if number of bits to be read is null
-
readBoolean
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
Read a byte value.- Returns:
- the byte value read from the stream
- Throws:
IOException- it will be thrown for transport errors.
-
reset
- Overrides:
resetin classFilterInputStream- Throws:
IOException
-
readLongArray
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 readbyteOrder- 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 readbyteOrder- the order of bytes to be used to decode valuesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- if reached limit of read- Since:
- 2.1.0
- See Also:
-
align
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 errorsEOFException- it will be thrown if the stream end has been reached the before align border.
-
skip
- Overrides:
skipin classFilterInputStream- Throws:
IOException
-
readDoubleArray
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 readbyteOrder- 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 readbyteOrder- the order of bytes to be used to decode valuesarraySizeLimiter- 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 operationJBBPReachedArraySizeLimitException- 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
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:
resetCounterin interfaceJBBPCountableBitStream- See Also:
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
readString
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
Read array of strings from stream.- Parameters:
items- number of items, or -1 if read whole streambyteOrder- 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 streambyteOrder- order of bytes in structure, must not be nullarraySizeLimiter- 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 errorsJBBPReachedArraySizeLimitException- if reached limit of array read- Since:
- 2.1.0
- See Also:
-