Bit Io Save

A library for reading/writing arbitrary length of bits.

Project README

bit-io

GitHub Action CircleCI Build Status Quality Gate Status Known Vulnerabilities Maven Central javadoc

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.

Specifications

boolean

type size(min) size(max) notes
boolean 1 1 readBoolean(), writeBoolean(boolean)

numeric

integral

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)

floating-point

No methods supplied for floating-point types.

Reading

  • You need to prepare an instance of ByteInput for reading octets.
  • You can read bits from an instance of BitInput which uses the ByteInput instance.

Preparing ByteInput

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);

Creating BitInput

Using DefaultBitInput

Construct with an already existing ByteInput.

final BitInput bitInput = new DefalutBitInput(byteInput);

Reading values.

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

Writing

  • You need to prepare an instance of ByteOutput for writing octets.
  • You can write bits to an instance of BitInput which uses the ByteOutput instance.

Preparing ByteOutput

There are counter classes and contructors to ByteInput.

Creating BitOutput

There are also counter classes and constructors to BitInput.

Using DefalutBitOutput

Writing values.

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

Domate via Paypal

Open Source Agenda is not affiliated with "Bit Io" Project. README Source: jinahya/bit-io
Stars
33
Open Issues
2
Last Commit
4 months ago
Repository
License
Tags

Open Source Agenda Badge

Open Source Agenda Rating