A library for reading/writing non octet aligned values such as 1-bit boolean
or 17-bit unsigned int
.
See bit-io2 for Java8+ flavored version.
type | size(min) | size(max) | notes |
---|---|---|---|
boolean |
1 | 1 | readBoolean() , writeBoolean(boolean) |
The size(min) is 1
and the size(max) is 2^e - (unsigned ? 1 : 0)
.
type | e | size(min) | size(max) | notes |
---|---|---|---|---|
byte |
3 | 1 | 7/8 | readByte(unsigned, size) , writeByte(unsigned, size, byte) |
short |
4 | 1 | 15/16 | readShort(unsigned, size) , writeShort(unsigned, size, short) |
int |
5 | 1 | 31/32 | readInt(unsigned, size) , writeInt(unsigned, size, int) |
long |
6 | 1 | 63/64 | readLong(unsigned, size) , writeLong(unsigned, size, long) |
char |
1 | 16 | readChar(size) , writeChar(size, char) |
No methods supplied for floating-point types.
- You need to prepare an instance of
ByteInput
for reading octets. - You can read bits from an instance of
BitInput
which uses theByteInput
instance.
Prepare an instance of ByteInput
from various sources.
new ArrayByteInput(byte[], int);
new BufferByteInput(java.nio.ByteBuffer);
new DataByteInput(java.io.DataInput);
new StreamByteInput(java.io.InputStream);
Construct with an already existing ByteInput
.
final BitInput bitInput = new DefalutBitInput(byteInput);
final BitInput input;
final boolean b = input.readBoolean(); // 1-bit boolean 1 1
final int ui6 = input.readInt(true, 6); // 6-bit unsigned int 6 7
final long sl47 = input.readLong(false, 47); // 47-bit signed long 47 54
final long discarded = input.align(1); // aligns to (1*8)-bit 2 56
assert discarded == 2L;
b llllllll llllllll llllllll llllllll llllllll llllll
iiiiiil dd
- You need to prepare an instance of
ByteOutput
for writing octets. - You can write bits to an instance of
BitInput
which uses theByteOutput
instance.
There are counter classes and contructors to ByteInput
.
There are also counter classes and constructors to BitInput
.
final BitOutput output;
output.writeBoolean(false); // 1-bit boolean 1 1
output.writeInt(false, 9, -72); // 9-bit signed int 9 10
output.writeBoolean(true); // 1-bit boolean 1 11
output.writeLong(true, 33, 99L); // 33-bit unsigned long 33 44
final long padded = output.align(4); // aligns to (4*8)-bit 20 64
assert padded == 20L;
b b pppp pppppppp pppppppp
iiiiiii ii lllll llllllll llllllll llllllll llll
01101110 00100000 00000000 00000000 00000110 00110000 00000000 00000000