Skip to content

Commit

Permalink
made BinaryReader a bit more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
Jatie1 committed Feb 14, 2022
1 parent cc7161d commit 40fc726
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/main/java/com/jatie/BinaryReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,35 @@ public class BinaryReader {

public static int readInt(final DataInputStream is) throws IOException {
int value = is.readInt();
int b1 = value & 0xff;
int b2 = value >> 8 & 0xff;
int b3 = value >> 16 & 0xff;
int b4 = value >> 24 & 0xff;

return b1 << 24 | b2 << 16 | b3 << 8 | b4;
return value << 24 | b2 << 16 | b3 << 8 | b4;
}

public static void skipString(final DataInputStream is) throws IOException {
if (is.readByte() == 0) {
if (is.read() == 0) {
return;
}
is.skip(getStringLength(is));
}

public static void skipString(final DataInputStream is, int num) throws IOException {
for (; num > 0; num--) {
if (is.readByte() == 0) {
continue;
}
is.skip(getStringLength(is));
skipString(is);
}
}

public static int getStringLength(final DataInputStream is) throws IOException {
int count = 0;
int shift = 0;
boolean more = true;
while (more) {
byte b = is.readByte();
while (true) {
int b = is.read();
count |= (b & 0x7F) << shift;
shift += 7;
if ((b & 0x80) == 0) {
more = false;
break;
}
}
return count;
Expand Down

0 comments on commit 40fc726

Please sign in to comment.