BitwiseInputStreampublic class BitwiseInputStream extends Object An object that provides bitwise incremental read access to a byte array.
This is useful, for example, when accessing a series of fields that
may not be aligned on byte boundaries.
NOTE -- This class is not threadsafe. |
Fields Summary |
---|
private byte[] | mBuf | private int | mPos | private int | mEnd |
Constructors Summary |
---|
public BitwiseInputStream(byte[] buf)Create object from byte array.
mBuf = buf;
mEnd = buf.length << 3;
mPos = 0;
|
Methods Summary |
---|
public int | available()Return the number of bit still available for reading.
return mEnd - mPos;
| public int | read(int bits)Read some data and increment the current position.
The 8-bit limit on access to bitwise streams is intentional to
avoid endianness issues.
int index = mPos >>> 3;
int offset = 16 - (mPos & 0x07) - bits; // &7==%8
if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) {
throw new AccessException("illegal read " +
"(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")");
}
int data = (mBuf[index] & 0xFF) << 8;
if (offset < 8) data |= mBuf[index + 1] & 0xFF;
data >>>= offset;
data &= (-1 >>> (32 - bits));
mPos += bits;
return data;
| public byte[] | readByteArray(int bits)Read data in bulk into a byte array and increment the current position.
int bytes = (bits >>> 3) + ((bits & 0x07) > 0 ? 1 : 0); // &7==%8
byte[] arr = new byte[bytes];
for (int i = 0; i < bytes; i++) {
int increment = Math.min(8, bits - (i << 3));
arr[i] = (byte)(read(increment) << (8 - increment));
}
return arr;
| public void | skip(int bits)Increment the current position and ignore contained data.
if ((mPos + bits) > mEnd) {
throw new AccessException("illegal skip " +
"(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")");
}
mPos += bits;
|
|