summaryrefslogtreecommitdiff
path: root/isoparser/src/main/java/com/googlecode/mp4parser/boxes/mp4/objectdescriptors/.svn/text-base/BitWriterBuffer.java.svn-base
blob: e6ea67f5e4b12b4f94c6b7c353bd4d2a4a66acc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.googlecode.mp4parser.boxes.mp4.objectdescriptors;

import java.nio.ByteBuffer;

public class BitWriterBuffer {

    private ByteBuffer buffer;
    int initialPos;
    int position = 0;

    public BitWriterBuffer(ByteBuffer buffer) {
        this.buffer = buffer;
        this.initialPos = buffer.position();
    }

    public void writeBits(int i, int numBits) {
        assert i <= ((1 << numBits)-1): String.format("Trying to write a value bigger (%s) than the number bits (%s) allows. " +
                "Please mask the value before writing it and make your code is really working as intended.", i, (1<<numBits)-1);

        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);
        }
        buffer.position(initialPos + position / 8 + ((position % 8 > 0) ? 1 : 0));
    }


}