public class Bitstream<T> extends Object
Modifier and Type | Class and Description |
---|---|
static class |
Bitstream.ByteArrayStream
Specific
byte stream . |
static class |
Bitstream.ByteBufferStream
Specific
byte stream . |
static class |
Bitstream.ByteInputStream
Specific
byte stream . |
static class |
Bitstream.ByteOutputStream
Specific
byte stream . |
static interface |
Bitstream.ByteStream<T>
General byte stream.
|
Modifier and Type | Field and Description |
---|---|
static int |
EOS
End of stream marker, -1 or 0xFFFFFFFF
|
Constructor and Description |
---|
Bitstream(Bitstream.ByteStream<T> stream,
boolean outputMode) |
Modifier and Type | Method and Description |
---|---|
boolean |
canInput()
Return true if stream can handle input, i.e.
|
boolean |
canOutput()
Return true if stream can handle output, i.e.
|
static void |
checkBounds(byte[] sb,
int offset,
int remaining) |
void |
close()
Closing the underlying stream, implies
flush() . |
int |
flush()
Synchronizes all underlying
output stream operations, or do nothing. |
int |
getBitBuffer()
Returns the current bit buffer.
|
int |
getBitCount()
Number of remaining bits in cache to read before next byte-read (input mode)
or number of remaining bits to be cached before next byte-write (output mode).
|
int |
getBitPosition()
Return the next bit number to be read or write counting from [0..7].
|
int |
getLastBitPos()
Return the last bit number read or written counting from [0..7].
|
Bitstream.ByteStream<T> |
getStream()
Returns the currently used
Bitstream.ByteStream . |
T |
getSubStream()
Returns the currently used
Bitstream.ByteStream 's Bitstream.ByteStream.getStream() . |
boolean |
getThrowIOExceptionOnEOF()
Returns true if I/O methods throw an
IOException if EOS appears, otherwise false (default). |
void |
mark(int readLimit)
Set
markpos to current position, allowing the stream to be reset() . |
long |
position()
Returns the bit position in the stream.
|
long |
position(long newPosition)
Sets this stream's bit position.
|
int |
readBit(boolean msbFirst) |
int |
readBits31(int n)
Return incoming bits as read via
readBit(boolean) LSB-first as little-endian. |
int |
readUInt16(boolean bigEndian)
Return incoming
uint16_t as read via readBits31(int) LSB-first as little-endian,
hence bytes are swapped if bigEndian. |
static int |
readUInt16(boolean bigEndian,
byte[] bytes,
int offset)
Return incoming
uint16_t value and swap bytes according to bigEndian. |
long |
readUInt32(boolean bigEndian)
Return incoming
uint32_t as read via readBits31(int) LSB-first as little-endian,
hence bytes are swapped if bigEndian. |
static long |
readUInt32(boolean bigEndian,
byte[] bytes,
int offset)
Return incoming
uint32_t and swap bytes according to bigEndian. |
int |
readUInt8()
Return incoming
uint8_t as read via readBits31(int) . |
void |
reset()
Reset stream position to markpos as set via
mark(int) . |
void |
setStream(T stream,
boolean outputMode)
Sets the underlying stream, without
close() ing the previous one. |
void |
setThrowIOExceptionOnEOF(boolean enable)
Enables or disables throwing an
IOException in case EOS appears. |
long |
skip(long n)
It is implementation dependent, whether backward skip giving a negative number is supported or not.
|
static String |
toBinString(boolean msbFirst,
int v,
int bitCount) |
static String |
toHexBinString(boolean msbFirst,
byte[] data,
int offset,
int len) |
static String |
toHexBinString(boolean msbFirst,
ByteBuffer data,
int offset,
int len) |
static String |
toHexBinString(boolean msbFirst,
int v,
int bitCount) |
String |
toString() |
protected String |
toStringImpl() |
static int |
toUInt32Int(int int32)
Returns the reinterpreted given
int32_t value
as uint32_t if ≤ Integer.MAX_VALUE
as within an int storage. |
static long |
toUInt32Long(int int32)
Reinterpret the given
int32_t value as uint32_t ,
i.e. |
static int |
uint32LongToInt(long uint32)
|
int |
writeBit(boolean msbFirst,
int bit) |
int |
writeBits31(int n,
int bits)
Write the given bits via
writeBit(boolean, int) LSB-first as little-endian. |
int |
writeInt16(boolean bigEndian,
short int16)
Write the given 16 bits via
writeBits31(int, int) LSB-first as little-endian,
hence bytes are swapped if bigEndian. |
int |
writeInt32(boolean bigEndian,
int int32)
Write the given 32 bits via
writeBits31(int, int) LSB-first as little-endian,
hence bytes are swapped if bigEndian. |
int |
writeInt8(byte int8)
Write the given 8 bits via
writeBits31(int, int) . |
public static final int EOS
public Bitstream(Bitstream.ByteStream<T> stream, boolean outputMode) throws IllegalArgumentException
stream
- outputMode
- IllegalArgumentException
- if requested outputMode doesn't match stream's canInput()
and canOutput()
.public final void setThrowIOExceptionOnEOF(boolean enable)
IOException
in case EOS
appears.
Default behavior for I/O methods is not to throw an IOException
, but to return EOS
.
public final boolean getThrowIOExceptionOnEOF()
IOException
if EOS
appears, otherwise false (default).public final void setStream(T stream, boolean outputMode) throws IllegalArgumentException, IOException
close()
ing the previous one.
If the previous stream was in output mode
,
flush()
is being called.
IllegalArgumentException
- if requested outputMode doesn't match stream's canInput()
and canOutput()
.IOException
- could be caused by flush()
.public final Bitstream.ByteStream<T> getStream()
Bitstream.ByteStream
.public final T getSubStream()
Bitstream.ByteStream
's Bitstream.ByteStream.getStream()
.public final void close() throws IOException
flush()
.
Implementation will null
the stream references,
hence #setStream(Object)
must be called before re-using instance.
If the closed stream was in output mode
,
flush()
is being called.
IOException
public final int flush() throws IllegalStateException, IOException
output stream
operations, or do nothing.
Method also flushes incomplete bytes to the underlying Bitstream.ByteStream
and hence skips to the next byte position.
EOS
caused by writing, otherwise zero.IllegalStateException
- if not in output mode or stream closedIOException
public final boolean canInput()
readBit(boolean)
.public final boolean canOutput()
writeBit(boolean, int)
.public final void mark(int readLimit) throws IllegalStateException
markpos
to current position, allowing the stream to be reset()
.readlimit
- maximum number of bytes able to read before invalidating the markpos
.IllegalStateException
- if not in input mode or stream closedpublic final void reset() throws IllegalStateException, IOException
mark(int)
.
markpos is kept, hence reset()
can be called multiple times.
IllegalStateException
- if not in input mode or stream closedIllegalStateException
- if markpos has not been set via mark(int)
or reset operation failed.IOException
- if reset operation failed.public final int getBitCount()
Counting down from 7..0 7..0, starting with 0.
In input mode, zero indicates reading a new byte and cont. w/ 7. In output mode, the cached byte is written when flipping over to 0.
public final int getLastBitPos()
Returned value is normalized [0..7], i.e. independent from msb or lsb read order.
public final int getBitPosition()
Returned value is normalized [0..7], i.e. independent from msb or lsb read order.
public final int getBitBuffer()
getBitCount()
public final long position()
public final long position(long newPosition) throws UnsupportedOperationException, IllegalArgumentException, IllegalStateException, IOException
A set mark is cleared.
Returns EOS
is end-of-stream is reached,
otherwise the new position.
Known supporting implementation is Bitstream.ByteBufferStream
and Bitstream.ByteArrayStream
.
newPosition
- The new positive position.EOS
if end-of-stream is reached.UnsupportedOperationException
- if not supported, i.e. Bitstream.ByteInputStream
or Bitstream.ByteOutputStream
IllegalArgumentException
- If the newPosition
is negativeIOException
- if read error occurs or EOS is reached and setThrowIOExceptionOnEOF(boolean)
is set to true.IllegalStateException
public final int readBit(boolean msbFirst) throws UnsupportedOperationException, IllegalStateException, IOException
msbFirst
- if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB.EOS
if end-of-stream is reached.IOException
IllegalStateException
- if not in input mode or stream closedUnsupportedOperationException
public final int writeBit(boolean msbFirst, int bit) throws IllegalStateException, IOException
msbFirst
- if true outgoing stream bit order is MSB to LSB, otherwise LSB to MSB.bit
- EOS
if end-of-stream is reached.IOException
IllegalStateException
- if not in output mode or stream closedpublic long skip(long n) throws IllegalStateException, IOException
n
- number of bits to skipIOException
- if read error occurs or EOS is reached and setThrowIOExceptionOnEOF(boolean)
is set to true.IllegalStateException
- if closedpublic int readBits31(int n) throws IllegalArgumentException, IOException
readBit(boolean)
LSB-first as little-endian.
The incoming bit order is from low- to most-significant-bit, maintaining bit LSB-first order.
n
- number of bits, maximum 31 bitsEOS
.IllegalStateException
- if not in input mode or stream closedIllegalArgumentException
- if n > 31IOException
public int writeBits31(int n, int bits) throws IllegalStateException, IllegalArgumentException, IOException
writeBit(boolean, int)
LSB-first as little-endian.
The outgoing bit order is from low- to most-significant-bit, maintaining bit LSB-first order.
n
- number of bits, maximum 31 bitsbits
- the bits to writeEOS
.IllegalStateException
- if not in output mode or stream closedIllegalArgumentException
- if n > 31IOException
public final int readUInt8() throws IllegalStateException, IOException
uint8_t
as read via readBits31(int)
.
In case of a int8_t
2-complement signed value, simply cast the result to byte
after checking for EOS
.
EOS
or the 8bit unsigned value within the lower bits.IllegalStateException
- if not in input mode or stream closedIOException
public final int writeInt8(byte int8) throws IllegalStateException, IOException
writeBits31(int, int)
.EOS
or the written 8bit value.IllegalStateException
- if not in output mode or stream closedIOException
public final int readUInt16(boolean bigEndian) throws IllegalStateException, IOException
uint16_t
as read via readBits31(int)
LSB-first as little-endian,
hence bytes are swapped if bigEndian.
In case of a int16_t
2-complement signed value, simply cast the result to short
after checking for EOS
.
bigEndian
- if true, swap incoming bytes to little-endian, otherwise leave them as little-endian.EOS
or the 16bit unsigned value within the lower bits.IllegalStateException
- if not in input mode or stream closedIOException
public static final int readUInt16(boolean bigEndian, byte[] bytes, int offset) throws IndexOutOfBoundsException
uint16_t
value and swap bytes according to bigEndian.
In case of a int16_t
2-complement signed value, simply cast the result to short
.
bigEndian
- if false, swap incoming bytes to little-endian, otherwise leave them as big-endian.IndexOutOfBoundsException
public final int writeInt16(boolean bigEndian, short int16) throws IllegalStateException, IOException
writeBits31(int, int)
LSB-first as little-endian,
hence bytes are swapped if bigEndian.bigEndian
- if true, swap given bytes to little-endian, otherwise leave them as little-endian.EOS
or the written 16bit value.IllegalStateException
- if not in output mode or stream closedIOException
public final long readUInt32(boolean bigEndian) throws IllegalStateException, IOException
uint32_t
as read via readBits31(int)
LSB-first as little-endian,
hence bytes are swapped if bigEndian.
In case of a int32_t
2-complement signed value, simply cast the result to int
after checking for EOS
.
bigEndian
- if true, swap incoming bytes to little-endian, otherwise leave them as little-endian.EOS
or the 32bit unsigned value within the lower bits.IllegalStateException
- if not in input mode or stream closedIOException
public static final long readUInt32(boolean bigEndian, byte[] bytes, int offset) throws IndexOutOfBoundsException
uint32_t
and swap bytes according to bigEndian.
In case of a int32_t
2-complement signed value, simply cast the result to int
.
bigEndian
- if false, swap incoming bytes to little-endian, otherwise leave them as big-endian.IndexOutOfBoundsException
public final int writeInt32(boolean bigEndian, int int32) throws IllegalStateException, IOException
writeBits31(int, int)
LSB-first as little-endian,
hence bytes are swapped if bigEndian.bigEndian
- if true, swap given bytes to little-endian, otherwise leave them as little-endian.EOS
or the written 32bit value.IllegalStateException
- if not in output mode or stream closedIOException
public static final long toUInt32Long(int int32)
int32_t
value as uint32_t
,
i.e. perform the following cast to long
:
final long l = 0xffffffffL & int32;
public static final int toUInt32Int(int int32)
int32_t
value
as uint32_t
if ≤ Integer.MAX_VALUE
as within an int
storage.
Otherwise return -1.public static final int uint32LongToInt(long uint32)
protected String toStringImpl()
public static String toBinString(boolean msbFirst, int v, int bitCount)
public static String toHexBinString(boolean msbFirst, int v, int bitCount)
public static final String toHexBinString(boolean msbFirst, byte[] data, int offset, int len)
public static final String toHexBinString(boolean msbFirst, ByteBuffer data, int offset, int len)
public static void checkBounds(byte[] sb, int offset, int remaining) throws IndexOutOfBoundsException
IndexOutOfBoundsException