Skip to content

Commit

Permalink
JS: stronger typing
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 526909255
  • Loading branch information
eustas committed Jul 4, 2023
1 parent e1f5788 commit ffbe112
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 386 deletions.
32 changes: 16 additions & 16 deletions java/org/brotli/dec/BitReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ static void doReadMoreInput(State s) {
}
throw new BrotliRuntimeException("No more input");
}
int readOffset = s.halfOffset << LOG_HALF_SIZE;
final int readOffset = s.halfOffset << LOG_HALF_SIZE;
int bytesInBuffer = CAPACITY - readOffset;
// Move unused bytes to the head of the buffer.
Utils.copyBytesWithin(s.byteBuffer, 0, readOffset, CAPACITY);
s.halfOffset = 0;
while (bytesInBuffer < CAPACITY) {
int spaceLeft = CAPACITY - bytesInBuffer;
int len = Utils.readInput(s.input, s.byteBuffer, bytesInBuffer, spaceLeft);
final int spaceLeft = CAPACITY - bytesInBuffer;
final int len = Utils.readInput(s.input, s.byteBuffer, bytesInBuffer, spaceLeft);
// EOF is -1 in Java, but 0 in C#.
if (len <= 0) {
s.endOfStreamReached = 1;
Expand All @@ -85,7 +85,7 @@ static void checkHealth(State s, int endOfStream) {
if (s.endOfStreamReached == 0) {
return;
}
int byteOffset = (s.halfOffset << LOG_HALF_SIZE) + ((s.bitOffset + 7) >> 3) - BYTENESS;
final int byteOffset = (s.halfOffset << LOG_HALF_SIZE) + ((s.bitOffset + 7) >> 3) - BYTENESS;
if (byteOffset > s.tailBytes) {
throw new BrotliRuntimeException("Read after end");
}
Expand Down Expand Up @@ -146,7 +146,7 @@ static int peekBits(State s) {
* otherwise BitReader will become broken.
*/
static int readFewBits(State s, int n) {
int val = peekBits(s) & ((1 << n) - 1);
final int val = peekBits(s) & ((1 << n) - 1);
s.bitOffset += n;
return val;
}
Expand All @@ -160,7 +160,7 @@ static int readBits(State s, int n) {
}

private static int readManyBits(State s, int n) {
int low = readFewBits(s, 16);
final int low = readFewBits(s, 16);
doFillBitWindow(s);
return low | (readFewBits(s, n - 16) << 16);
}
Expand Down Expand Up @@ -194,9 +194,9 @@ static void reload(State s) {
}

static void jumpToByteBoundary(State s) {
int padding = (BITNESS - s.bitOffset) & 7;
final int padding = (BITNESS - s.bitOffset) & 7;
if (padding != 0) {
int paddingBits = readFewBits(s, padding);
final int paddingBits = readFewBits(s, padding);
if (paddingBits != 0) {
throw new BrotliRuntimeException("Corrupted padding bits");
}
Expand Down Expand Up @@ -227,10 +227,10 @@ static void copyRawBytes(State s, byte[] data, int offset, int length) {
}

// Get data from shadow buffer with "sizeof(int)" granularity.
int copyNibbles = Math.min(halfAvailable(s), length >> LOG_HALF_SIZE);
final int copyNibbles = Math.min(halfAvailable(s), length >> LOG_HALF_SIZE);
if (copyNibbles > 0) {
int readOffset = s.halfOffset << LOG_HALF_SIZE;
int delta = copyNibbles << LOG_HALF_SIZE;
final int readOffset = s.halfOffset << LOG_HALF_SIZE;
final int delta = copyNibbles << LOG_HALF_SIZE;
System.arraycopy(s.byteBuffer, readOffset, data, offset, delta);
offset += delta;
length -= delta;
Expand All @@ -255,7 +255,7 @@ static void copyRawBytes(State s, byte[] data, int offset, int length) {

// Now it is possible to copy bytes directly.
while (length > 0) {
int len = Utils.readInput(s.input, data, offset, length);
final int len = Utils.readInput(s.input, data, offset, length);
if (len == -1) {
throw new BrotliRuntimeException("Unexpected end of input");
}
Expand All @@ -268,18 +268,18 @@ static void copyRawBytes(State s, byte[] data, int offset, int length) {
* Translates bytes to halves (int/short).
*/
static void bytesToNibbles(State s, int byteLen) {
byte[] byteBuffer = s.byteBuffer;
int halfLen = byteLen >> LOG_HALF_SIZE;
final byte[] byteBuffer = s.byteBuffer;
final int halfLen = byteLen >> LOG_HALF_SIZE;
if (BITNESS == 64) {
int[] intBuffer = s.intBuffer;
final int[] intBuffer = s.intBuffer;
for (int i = 0; i < halfLen; ++i) {
intBuffer[i] = ((byteBuffer[i * 4] & 0xFF))
| ((byteBuffer[(i * 4) + 1] & 0xFF) << 8)
| ((byteBuffer[(i * 4) + 2] & 0xFF) << 16)
| ((byteBuffer[(i * 4) + 3] & 0xFF) << 24);
}
} else {
short[] shortBuffer = s.shortBuffer;
final short[] shortBuffer = s.shortBuffer;
for (int i = 0; i < halfLen; ++i) {
shortBuffer[i] = (short) ((byteBuffer[i * 2] & 0xFF)
| ((byteBuffer[(i * 2) + 1] & 0xFF) << 8));
Expand Down
4 changes: 2 additions & 2 deletions java/org/brotli/dec/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private static void unpackLookupTable(int[] lookup, String map, String rle) {
}
int offset = 1280;
for (int k = 0; k < 19; ++k) {
int value = k & 3;
int rep = rle.charAt(k) - 32;
final int value = k & 3;
final int rep = rle.charAt(k) - 32;
for (int i = 0; i < rep; ++i) {
lookup[offset++] = value;
}
Expand Down
Loading

0 comments on commit ffbe112

Please sign in to comment.