Commit ecaf329b by Oliver Woodman

Improve ParsableByteArray performance + enhance API.

parent ed658b8e
...@@ -37,6 +37,12 @@ public final class ParsableByteArray { ...@@ -37,6 +37,12 @@ public final class ParsableByteArray {
limit = data.length; limit = data.length;
} }
/** Creates a new instance wrapping {@code data}. */
public ParsableByteArray(byte[] data) {
this.data = data;
limit = data.length;
}
/** /**
* Creates a new instance that wraps an existing array. * Creates a new instance that wraps an existing array.
* *
...@@ -171,6 +177,13 @@ public final class ParsableByteArray { ...@@ -171,6 +177,13 @@ public final class ParsableByteArray {
return result; return result;
} }
/** Reads the next three bytes as an unsigned value. */
public int readUnsignedInt24() {
int result = shiftIntoInt(data, position, 3);
position += 3;
return result;
}
/** Reads the next four bytes as an unsigned value. */ /** Reads the next four bytes as an unsigned value. */
public long readUnsignedInt() { public long readUnsignedInt() {
long result = shiftIntoLong(data, position, 4); long result = shiftIntoLong(data, position, 4);
...@@ -180,9 +193,11 @@ public final class ParsableByteArray { ...@@ -180,9 +193,11 @@ public final class ParsableByteArray {
/** Reads the next four bytes as a signed value. */ /** Reads the next four bytes as a signed value. */
public int readInt() { public int readInt() {
int result = shiftIntoInt(data, position, 4); // shiftIntoInt inlined as performance optimization.
position += 4; return (data[position++] & 0xFF) << 24
return result; | (data[position++] & 0xFF) << 16
| (data[position++] & 0xFF) << 8
| data[position++] & 0xFF;
} }
/** Reads the next eight bytes as a signed value. */ /** Reads the next eight bytes as a signed value. */
...@@ -221,8 +236,11 @@ public final class ParsableByteArray { ...@@ -221,8 +236,11 @@ public final class ParsableByteArray {
* @throws IllegalArgumentException Thrown if the top bit of the input data is set. * @throws IllegalArgumentException Thrown if the top bit of the input data is set.
*/ */
public int readUnsignedIntToInt() { public int readUnsignedIntToInt() {
int result = shiftIntoInt(data, position, 4); // shiftIntoInt inlined as performance optimization.
position += 4; final int result = (data[position++] & 0xFF) << 24
| (data[position++] & 0xFF) << 16
| (data[position++] & 0xFF) << 8
| data[position++] & 0xFF;
if (result < 0) { if (result < 0) {
throw new IllegalArgumentException("Top bit not zero: " + result); throw new IllegalArgumentException("Top bit not zero: " + result);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment