replaced integer * and % with shift operations, for performance
diff --git a/core/java/com/android/internal/util/BitwiseInputStream.java b/core/java/com/android/internal/util/BitwiseInputStream.java
index 5da3bc6..4757919 100644
--- a/core/java/com/android/internal/util/BitwiseInputStream.java
+++ b/core/java/com/android/internal/util/BitwiseInputStream.java
@@ -51,7 +51,7 @@
*/
public BitwiseInputStream(byte buf[]) {
mBuf = buf;
- mEnd = buf.length * 8;
+ mEnd = buf.length << 3;
mPos = 0;
}
@@ -70,13 +70,13 @@
* @return byte of read data (possibly partially filled, from lsb)
*/
public byte read(int bits) throws AccessException {
- int index = mPos / 8;
- int offset = 16 - (mPos % 8) - bits;
+ 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;
+ int data = (mBuf[index] & 0x00FF) << 8;
if (offset < 8) data |= (mBuf[index + 1] & 0xFF);
data >>>= offset;
data &= (-1 >>> (32 - bits));
@@ -92,10 +92,10 @@
* @return newly allocated byte array of read data
*/
public byte[] readByteArray(int bits) throws AccessException {
- int bytes = (bits / 8) + ((bits % 8) > 0 ? 1 : 0);
+ 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 * 8));
+ int increment = Math.min(8, bits - (i << 3));
arr[i] = (byte)(read(increment) << (8 - increment));
}
return arr;
diff --git a/core/java/com/android/internal/util/BitwiseOutputStream.java b/core/java/com/android/internal/util/BitwiseOutputStream.java
index 5941bf3..17f5c7c 100644
--- a/core/java/com/android/internal/util/BitwiseOutputStream.java
+++ b/core/java/com/android/internal/util/BitwiseOutputStream.java
@@ -51,7 +51,7 @@
*/
public BitwiseOutputStream(int startingLength) {
mBuf = new byte[startingLength];
- mEnd = startingLength * 8;
+ mEnd = startingLength << 3;
mPos = 0;
}
@@ -61,7 +61,7 @@
* @return newly allocated byte array
*/
public byte[] toByteArray() {
- int len = (mPos / 8) + ((mPos % 8) > 0 ? 1 : 0);
+ int len = (mPos >>> 3) + ((mPos & 0x07) > 0 ? 1 : 0); // &7==%8
byte[] newBuf = new byte[len];
System.arraycopy(mBuf, 0, newBuf, 0, len);
return newBuf;
@@ -74,8 +74,8 @@
*/
private void possExpand(int bits) {
if ((mPos + bits) < mEnd) return;
- byte[] newBuf = new byte[((mPos + bits) * 2) / 8];
- System.arraycopy(mBuf, 0, newBuf, 0, mEnd / 8);
+ byte[] newBuf = new byte[(mPos + bits) >>> 2];
+ System.arraycopy(mBuf, 0, newBuf, 0, mEnd >>> 3);
mBuf = newBuf;
}
@@ -91,12 +91,12 @@
}
possExpand(bits);
data &= (-1 >>> (32 - bits));
- int index = mPos / 8;
- int offset = 16 - (mPos % 8) - bits;
+ int index = mPos >>> 3;
+ int offset = 16 - (mPos & 0x07) - bits; // &7==%8
data <<= offset;
mPos += bits;
mBuf[index] |= (data >>> 8);
- if (offset < 8) mBuf[index + 1] |= (data & 0xFF);
+ if (offset < 8) mBuf[index + 1] |= (data & 0x00FF);
}
/**
@@ -107,7 +107,7 @@
*/
public void writeByteArray(int bits, byte[] arr) throws AccessException {
for (int i = 0; i < arr.length; i++) {
- int increment = Math.min(8, bits - (i * 8));
+ int increment = Math.min(8, bits - (i << 3));
if (increment > 0) {
write(increment, (byte)(arr[i] >>> (8 - increment)));
}
diff --git a/tests/AndroidTests/src/com/android/unit_tests/BitwiseStreamsTest.java b/tests/AndroidTests/src/com/android/unit_tests/BitwiseStreamsTest.java
index 2c9075c..a935247 100644
--- a/tests/AndroidTests/src/com/android/unit_tests/BitwiseStreamsTest.java
+++ b/tests/AndroidTests/src/com/android/unit_tests/BitwiseStreamsTest.java
@@ -23,6 +23,8 @@
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Log;
+
public class BitwiseStreamsTest extends AndroidTestCase {
private final static String LOG_TAG = "BitwiseStreamsTest";
@@ -85,4 +87,27 @@
for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8);
assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
}
+
+ @SmallTest
+ public void testFive() throws Exception {
+ int num_runs = 10;
+ long start = android.os.SystemClock.elapsedRealtime();
+ for (int run = 0; run < num_runs; run++) {
+ int offset = run % 8;
+ byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0");
+ BitwiseOutputStream outStream = new BitwiseOutputStream(30);
+ outStream.skip(offset);
+ for (int i = 0; i < inBuf.length; i++) {
+ outStream.write(5, inBuf[i] >>> 3);
+ outStream.write(3, inBuf[i] & 0x07);
+ }
+ BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
+ inStream.skip(offset);
+ byte[] inBufDup = new byte[inBuf.length];
+ for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8);
+ assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
+ }
+ long end = android.os.SystemClock.elapsedRealtime();
+ Log.d(LOG_TAG, "repeated encode-decode took " + (end - start) + " ms");
+ }
}