Introduction
A simple wrapper for bit level I/O with In|OutputStream.
Details
You can read/write variable length of bits from/to InputStream and OutputStream.
Exampe
class Sample {
public String id; // identifier; US-ASCII
public int grade; // grade; 4-bit unsigned integer
public String name; // name; UTF-8 String
public int age; // age; 7-bit unsigned integer; maximum 127
public boolean married; // married or not
public Integer balance; // account balance; 29-bit unsigned integer. null if hidden
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitOutput bo= new BitOutput(baos);
bo.writeASCII(sample.id); // 1 + 31 + 7N
bo.writeUnsignedInt(4, sample.grade); // 4
bo.writeSTRING(sample.name, "UTF-8"); // 1 + 31 + 8N
bo.writeUnsignedInt(7, sample.age); // 7
bo.writeBoolean(sample.married); // 1
bo.writeUnsignedINTEGER(29, sample.balance); // 1 + 29
output.align(1); // align to octet
ByteArrayInputStream bais = new ByteArrayOutputStream(baos.toByteArray());
BitInput bi = new BitInput(bais);
String id = bi.readASCII();
int grade = bi.readUnsignedInt(4);
String name = bi.readSTRING("UTF-8");
int age = bi.readUnsignedInt(7);
boolean married = bi.readBoolean();
Integer balance = bi.readUnsignedINTEGER(29);
bi.align(1); // align to octetValues
| type | bit range | notes |
| Boolean | 1 | 1 for true; 0 for false |
| BOOLEAN | 1 + Boolean | nullable Boolean |
| unsignedInt | 1 ~ 31 | unsigned int |
| unsignedINTEGER | 1 + unsignedInt | nullable unsigned Integer |
| Int | 2 ~ 32 | signed int |
| INTEGER | 1 + Int | nullable signed Integer |
| unsignedLong | 1 ~ 63 | unsigned long |
| unsignedLONG | 1 + unsignedLong | nullable unsigned Long |
| Long | 2 ~ 64 | signed long |
| LONG | 1 + Long | nullable signed Long |
| Bytes | 31 + 8N | non-nullable byte array; N: number of bytes |
| BYTES | 1 + Bytes | nullable byte array |
| String | Bytes | non-nullable String |
| STRING | 1 + Bytes | nullable String |
| ASCII | 1 + 31 + 7N | N: number of characters |
| UTF (@deprecated) | 16 + 8N | DataInput#readUTF / DataOutput#writeUTF |
Apidocs