Skip to content

Commit

Permalink
Prevent unnecessary calls to Arrays.toString in read loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
coobird committed Feb 9, 2022
1 parent 3a63909 commit 7bb002e
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ private void debugln(String format, Object... args) {
}
}

/**
* Debug message, optimized to reduce calls on Arrays.toString.
*/
private void debugln(String format, byte[] array) {
if (isDebug) {
debugln(format, Arrays.toString(array));
}
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = is.read(b, off, len);
Expand Down Expand Up @@ -243,8 +252,8 @@ public int read(byte[] b, int off, int len) throws IOException {
System.arraycopy(b, off, tmpBuffer, totalRead - bytesRead, bytesRead);
buffer = tmpBuffer;

debugln("Source: %s", Arrays.toString(b));
debugln("Buffer: %s", Arrays.toString(buffer));
debugln("Source: %s", b);
debugln("Buffer: %s", buffer);

while (position < totalRead && (totalRead - position) >= 2) {
debugln("Start loop, position: %s", position);
Expand Down Expand Up @@ -285,7 +294,7 @@ public int read(byte[] b, int off, int len) throws IOException {
if (position == 0 && totalRead >= 2) {
// Check the first two bytes of stream to see if SOI exists.
// If SOI is not found, this is not a JPEG.
debugln("Check if JPEG. buffer: %s", Arrays.toString(buffer));
debugln("Check if JPEG. buffer: %s", buffer);
if (!(buffer[position] == (byte) 0xFF && buffer[position + 1] == (byte) 0xD8)) {
// Not SOI, so it's not a JPEG.
// We no longer need to keep intercepting.
Expand Down

0 comments on commit 7bb002e

Please sign in to comment.