Skip to content
Permalink
Browse files
Refactor commons code.
  • Loading branch information
garydgregory committed May 5, 2022
1 parent 66b946e commit e1342d9849393dbe75188738b2798ff03b872954
Showing 1 changed file with 9 additions and 6 deletions.
@@ -28,6 +28,7 @@
* @since 1.7
*/
public class ChecksumVerifyingInputStream extends InputStream {

private final InputStream in;
private long bytesRemaining;
private final long expectedChecksum;
@@ -78,9 +79,7 @@ public int read() throws IOException {
checksum.update(ret);
--bytesRemaining;
}
if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) {
throw new IOException("Checksum verification failed");
}
verify();
return ret;
}

@@ -111,9 +110,7 @@ public int read(final byte[] b, final int off, final int len) throws IOException
checksum.update(b, off, ret);
bytesRemaining -= ret;
}
if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) {
throw new IOException("Checksum verification failed");
}
verify();
return ret;
}

@@ -122,4 +119,10 @@ public long skip(final long n) throws IOException {
// Can't really skip, we have to hash everything to verify the checksum
return read() >= 0 ? 1 : 0;
}

private void verify() throws IOException {
if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) {
throw new IOException("Checksum verification failed");
}
}
}

0 comments on commit e1342d9

Please sign in to comment.