https://github.com/jinahya/bit-io
A library for reading/writing arbitrary length of bits.
https://github.com/jinahya/bit-io
bit io java
Last synced: 3 months ago
JSON representation
A library for reading/writing arbitrary length of bits.
- Host: GitHub
- URL: https://github.com/jinahya/bit-io
- Owner: jinahya
- License: apache-2.0
- Created: 2013-10-09T04:26:39.000Z (over 12 years ago)
- Default Branch: develop
- Last Pushed: 2023-12-05T22:18:03.000Z (over 2 years ago)
- Last Synced: 2025-12-20T13:58:55.397Z (6 months ago)
- Topics: bit, io, java
- Language: Java
- Homepage:
- Size: 30.7 MB
- Stars: 35
- Watchers: 8
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# bit-io
[](https://github.com/jinahya/bit-io/actions?workflow=Java+CI)
[](https://circleci.com/gh/jinahya/bit-io/tree/develop)
[](https://travis-ci.org/jinahya/bit-io)
[](https://sonarcloud.io/dashboard?id=com.github.jinahya%3Abit-io%3Adevelop)
[](https://snyk.io//test/github/jinahya/bit-io?targetFile=pom.xml)
[](https://img.shields.io/maven-central/v/com.github.jinahya/bit-io)
[](https://javadoc.io/doc/com.github.jinahya/bit-io)
A library for reading/writing non octet aligned values such as `1-bit boolean` or `17-bit unsigned int`.
See [bit-io2](https://github.com/jinahya/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.
````java
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`.
```java
final BitInput bitInput = new DefalutBitInput(byteInput);
```
### Reading values.
```java
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.
```java
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
```
----
[](https://www.paypal.com/cgi-bin/webscr?cmd=_cart&business=A954LDFBW4B9N&lc=KR&item_name=GitHub&amount=5%2e00¤cy_code=USD&button_subtype=products&add=1&bn=PP%2dShopCartBF%3adonate%2dpaypal%2dblue%2epng%3aNonHosted)