// little-endian sclass BitBuffer { new ByteBuffer byteBuffer; // TODO (optimization): use a growing circular buffer int currentByte, currentBit; void add(bool b) { if (b) currentByte ||= 1 << currentBit; if (currentBit == 7) { byteBuffer.add((byte) currentByte); currentByte = 0; currentBit = 0; } else ++currentBit; } bool hasFullByte() { ret !byteBuffer.isEmpty(); } byte popFullByte() { ret byteBuffer.popFirst(); } // TODO: optimize void writeBits(int data, int nBits) }