You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/05/04 15:46:26 UTC
[commons-compress] 04/04: Reuse FilterInputStream
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 3b8b8f4599dc40a463c6954010d20888821ef8b2
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu May 4 11:46:17 2023 -0400
Reuse FilterInputStream
---
.../archivers/zip/ZipArchiveInputStream.java | 8 +++----
.../utils/ChecksumCalculatingInputStream.java | 26 +++++-----------------
.../utils/ChecksumVerifyingInputStream.java | 25 +++++----------------
3 files changed, 14 insertions(+), 45 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
index 6f34d61f..499bb6e5 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
@@ -26,6 +26,7 @@ import static org.apache.commons.compress.archivers.zip.ZipConstants.ZIP64_MAGIC
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
+import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
@@ -84,10 +85,7 @@ public class ZipArchiveInputStream extends ArchiveInputStream implements InputSt
/**
* Bounded input stream adapted from commons-io
*/
- private class BoundedInputStream extends InputStream {
-
- /** the wrapped input stream */
- private final InputStream in;
+ private class BoundedInputStream extends FilterInputStream {
/** the max length to provide */
private final long max;
@@ -103,8 +101,8 @@ public class ZipArchiveInputStream extends ArchiveInputStream implements InputSt
* @param size The maximum number of bytes to return
*/
public BoundedInputStream(final InputStream in, final long size) {
+ super(in);
this.max = size;
- this.in = in;
}
@Override
diff --git a/src/main/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStream.java b/src/main/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStream.java
index 8b1e55df..4ac65581 100644
--- a/src/main/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStream.java
+++ b/src/main/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStream.java
@@ -16,27 +16,24 @@
*/
package org.apache.commons.compress.utils;
+import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.zip.Checksum;
/**
- * A stream that calculates the checksum of the data read.
+ * Calculates the checksum of the data read.
+ *
* @NotThreadSafe
* @since 1.14
*/
-public class ChecksumCalculatingInputStream extends InputStream {
- private final InputStream in;
+public class ChecksumCalculatingInputStream extends FilterInputStream {
private final Checksum checksum;
public ChecksumCalculatingInputStream(final Checksum checksum, final InputStream inputStream) {
-
- Objects.requireNonNull(checksum, "checksum");
- Objects.requireNonNull(inputStream, "inputStream");
-
- this.checksum = checksum;
- this.in = inputStream;
+ super(Objects.requireNonNull(inputStream, "inputStream"));
+ this.checksum = Objects.requireNonNull(checksum, "checksum");
}
/**
@@ -62,17 +59,6 @@ public class ChecksumCalculatingInputStream extends InputStream {
return ret;
}
- /**
- * Reads a byte array from the stream
- * @throws IOException if the underlying stream throws or the
- * stream is exhausted and the Checksum doesn't match the expected
- * value
- */
- @Override
- public int read(final byte[] b) throws IOException {
- return read(b, 0, b.length);
- }
-
/**
* Reads from the stream into a byte array.
* @throws IOException if the underlying stream throws or the
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 cf13e78b..f0f1793c 100644
--- a/src/main/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStream.java
+++ b/src/main/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStream.java
@@ -16,19 +16,20 @@
*/
package org.apache.commons.compress.utils;
+import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.Checksum;
/**
- * A stream that verifies the checksum of the data read once the stream is
+ * Verifies the checksum of the data read once the stream is
* exhausted.
+ *
* @NotThreadSafe
* @since 1.7
*/
-public class ChecksumVerifyingInputStream extends InputStream {
+public class ChecksumVerifyingInputStream extends FilterInputStream {
- private final InputStream in;
private long bytesRemaining;
private final long expectedChecksum;
private final Checksum checksum;
@@ -43,17 +44,12 @@ public class ChecksumVerifyingInputStream extends InputStream {
*/
public ChecksumVerifyingInputStream(final Checksum checksum, final InputStream in,
final long size, final long expectedChecksum) {
+ super(in);
this.checksum = checksum;
- this.in = in;
this.expectedChecksum = expectedChecksum;
this.bytesRemaining = size;
}
- @Override
- public void close() throws IOException {
- in.close();
- }
-
/**
* @return bytes remaining to read
* @since 1.21
@@ -82,17 +78,6 @@ public class ChecksumVerifyingInputStream extends InputStream {
return ret;
}
- /**
- * Reads a byte array from the stream
- * @throws IOException if the underlying stream throws or the
- * stream is exhausted and the Checksum doesn't match the expected
- * value
- */
- @Override
- public int read(final byte[] b) throws IOException {
- return read(b, 0, b.length);
- }
-
/**
* Reads from the stream into a byte array.
* @throws IOException if the underlying stream throws or the