FileDocCategorySizeDatePackage
BitwiseInputStream.javaAPI DocAndroid 5.1 API3658Thu Mar 12 22:22:10 GMT 2015com.android.internal.util

BitwiseInputStream

public 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.

param
buf a byte array containing data

        mBuf = buf;
        mEnd = buf.length << 3;
        mPos = 0;
    
Methods Summary
public intavailable()
Return the number of bit still available for reading.

        return mEnd - mPos;
    
public intread(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.

param
bits the amount of data to read (gte 0, lte 8)
return
byte of read data (possibly partially filled, from lsb)

        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.

param
bits the amount of data to read
return
newly allocated byte array of read data

        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 voidskip(int bits)
Increment the current position and ignore contained data.

param
bits the amount by which to increment the position

        if ((mPos + bits) > mEnd) {
            throw new AccessException("illegal skip " +
                "(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")");
        }
        mPos += bits;