1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.util; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 21 /** 22 * An object that provides bitwise incremental read access to a byte array. 23 * 24 * This is useful, for example, when accessing a series of fields that 25 * may not be aligned on byte boundaries. 26 * 27 * NOTE -- This class is not threadsafe. 28 */ 29 @android.ravenwood.annotation.RavenwoodKeepWholeClass 30 public class BitwiseInputStream { 31 32 // The byte array being read from. 33 private byte[] mBuf; 34 35 // The current position offset, in bits, from the msb in byte 0. 36 private int mPos; 37 38 // The last valid bit offset. 39 private int mEnd; 40 41 /** 42 * An exception to report access problems. 43 */ 44 public static class AccessException extends Exception { AccessException(String s)45 public AccessException(String s) { 46 super("BitwiseInputStream access failed: " + s); 47 } 48 } 49 50 /** 51 * Create object from byte array. 52 * 53 * @param buf a byte array containing data 54 */ 55 @UnsupportedAppUsage BitwiseInputStream(byte buf[])56 public BitwiseInputStream(byte buf[]) { 57 mBuf = buf; 58 mEnd = buf.length << 3; 59 mPos = 0; 60 } 61 62 /** 63 * Return the number of bit still available for reading. 64 */ 65 @UnsupportedAppUsage available()66 public int available() { 67 return mEnd - mPos; 68 } 69 70 /** 71 * Read some data and increment the current position. 72 * 73 * The 8-bit limit on access to bitwise streams is intentional to 74 * avoid endianness issues. 75 * 76 * @param bits the amount of data to read (gte 0, lte 8) 77 * @return byte of read data (possibly partially filled, from lsb) 78 */ 79 @UnsupportedAppUsage read(int bits)80 public int read(int bits) throws AccessException { 81 int index = mPos >>> 3; 82 int offset = 16 - (mPos & 0x07) - bits; // &7==%8 83 if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) { 84 throw new AccessException("illegal read " + 85 "(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")"); 86 } 87 int data = (mBuf[index] & 0xFF) << 8; 88 if (offset < 8) data |= mBuf[index + 1] & 0xFF; 89 data >>>= offset; 90 data &= (-1 >>> (32 - bits)); 91 mPos += bits; 92 return data; 93 } 94 95 /** 96 * Read data in bulk into a byte array and increment the current position. 97 * 98 * @param bits the amount of data to read 99 * @return newly allocated byte array of read data 100 */ 101 @UnsupportedAppUsage readByteArray(int bits)102 public byte[] readByteArray(int bits) throws AccessException { 103 int bytes = (bits >>> 3) + ((bits & 0x07) > 0 ? 1 : 0); // &7==%8 104 byte[] arr = new byte[bytes]; 105 for (int i = 0; i < bytes; i++) { 106 int increment = Math.min(8, bits - (i << 3)); 107 arr[i] = (byte)(read(increment) << (8 - increment)); 108 } 109 return arr; 110 } 111 112 /** 113 * Increment the current position and ignore contained data. 114 * 115 * @param bits the amount by which to increment the position 116 */ 117 @UnsupportedAppUsage skip(int bits)118 public void skip(int bits) throws AccessException { 119 if ((mPos + bits) > mEnd) { 120 throw new AccessException("illegal skip " + 121 "(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")"); 122 } 123 mPos += bits; 124 } 125 } 126