A Java 8+ flavored version of bit-io.
Add this module as a dependency. Check the central for the current version.
<dependency>
<groupId>com.github.jinahya</groupId>
<artifactId>bit-io2</artifactId>
</dependency>
OutputStream stream = outputStream();
BitOutput output = BitOutputFactory.from(stream);
output.writeBoolean(true); // 1 bit 1
output.writeInt(true, 3, 1); // 3 bits 4
output.writeLong(false, 37, 0L); // 37 bits 41
long padded = output.align(1);
assert padded == 7L;
assert (padded + 41) % Byte.SIZE == 0;
InputStream stream = inputStream();
BitInput input = BitInputFactory.from(stream);
boolean v1 = input.readBoolean(); // 1 bit 1
int v2 = input.readInt(true, 3); // 3 bits 4
assert v2 == 1;
long v3 = input.readLong(false, 37); // 37 bits 41
assert v3 == 0L;
long discarded = input.align(1);
assert discarded == 7L;
assert (discarded + 41) % Byte.SIZE == 0;
See Specifications and Recipes for more information.