From e1342d9849393dbe75188738b2798ff03b872954 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 5 May 2022 09:29:36 -0400 Subject: [PATCH] Refactor commons code. --- .../utils/ChecksumVerifyingInputStream.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStream.java b/src/main/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStream.java index 89eee317e..62eca3108 100644 --- a/src/main/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStream.java +++ b/src/main/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStream.java @@ -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"); + } + } }