Code example for ByteBuffer

Methods: position

0
    int initialPos;
    int position = 0;
 
    public BitWriterBuffer(ByteBuffer buffer) {
        this.buffer = buffer;
        this.initialPos = buffer.position();
    } 
 
    public void writeBits(int i, int numBits) {
        int left = 8 - position % 8;
        if (numBits <= left) {
            int current = (buffer.get(initialPos + position / 8));
            current = current < 0 ? current + 256 : current;
            current += i << (left - numBits);
            buffer.put(initialPos + position / 8, (byte) (current > 127 ? current - 256 : current));
            position += numBits;
        } else { 
            int bitsSecondWrite = numBits - left;
            writeBits(i >> bitsSecondWrite, left);
            writeBits(i & (1 << bitsSecondWrite) - 1, bitsSecondWrite);
        }