| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class BitwiseInputStream { |
| 30 |
|
| 31 |
|
| 32 |
private byte[] mBuf; |
| 33 |
|
| 34 |
|
| 35 |
private int mPos; |
| 36 |
|
| 37 |
|
| 38 |
private int mEnd; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static class AccessException extends Exception { |
| 44 |
public AccessException(String s) { |
| 45 |
super("BitwiseInputStream access failed: " + s); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@UnsupportedAppUsage |
| 55 |
public BitwiseInputStream(byte buf[]) { |
| 56 |
mBuf = buf; |
| 57 |
mEnd = buf.length << 3; |
| 58 |
mPos = 0; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@UnsupportedAppUsage |
| 65 |
public int available() { |
| 66 |
return mEnd - mPos; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@UnsupportedAppUsage |
| 79 |
public int read(int bits) throws AccessException { |
| 80 |
int index = mPos >>> 3; |
| 81 |
int offset = 16 - (mPos & 0x07) - bits; |
| 82 |
if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) { |
| 83 |
throw new AccessException("illegal read " + |
| 84 |
"(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")"); |
| 85 |
} |
| 86 |
int data = (mBuf[index] & 0xFF) << 8; |
| 87 |
if (offset < 8) data |= mBuf[index + 1] & 0xFF; |
| 88 |
data >>>= offset; |
| 89 |
data &= (-1 >>> (32 - bits)); |
| 90 |
mPos += bits; |
| 91 |
return data; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@UnsupportedAppUsage |
| 101 |
public byte[] readByteArray(int bits) throws AccessException { |
| 102 |
int bytes = (bits >>> 3) + ((bits & 0x07) > 0 ? 1 : 0); |
| 103 |
byte[] arr = new byte[bytes]; |
| 104 |
for (int i = 0; i < bytes; i++) { |
| 105 |
int increment = Math.min(8, bits - (i << 3)); |
| 106 |
arr[i] = (byte)(read(increment) << (8 - increment)); |
| 107 |
} |
| 108 |
return arr; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
@UnsupportedAppUsage |
| 117 |
public void skip(int bits) throws AccessException { |
| 118 |
if ((mPos + bits) > mEnd) { |
| 119 |
throw new AccessException("illegal skip " + |
| 120 |
"(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")"); |
| 121 |
} |
| 122 |
mPos += bits; |
| 123 |
} |
| 124 |
} |