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/12/27 13:02:13 UTC
(commons-io) 02/02: Reuse CountingInputStream
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-io.git
commit 99702ec5ce4c25a909299c4f4a8be616d8ce04dc
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Dec 27 08:02:07 2023 -0500
Reuse CountingInputStream
---
.../commons/io/input/BoundedInputStream.java | 39 +++++++---------------
1 file changed, 12 insertions(+), 27 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/input/BoundedInputStream.java b/src/main/java/org/apache/commons/io/input/BoundedInputStream.java
index f1884b71..7a83007b 100644
--- a/src/main/java/org/apache/commons/io/input/BoundedInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/BoundedInputStream.java
@@ -36,7 +36,7 @@ import org.apache.commons.io.build.AbstractStreamBuilder;
*/
public class BoundedInputStream extends ProxyInputStream {
- // TODO For 3.0, extend CountingInputStream.
+ // TODO For 3.0, extend CountingInputStream. Or, add a max feature to CountingInputStream.
/**
* Builds a new {@link BoundedInputStream} instance.
@@ -115,12 +115,6 @@ public class BoundedInputStream extends ProxyInputStream {
/** The max count of bytes to read. */
private final long maxCount;
- /** The count of bytes read. */
- private long count;
-
- /** The marked position. */
- private long mark = EOF;
-
/**
* Flag if close should be propagated.
*
@@ -161,25 +155,17 @@ public class BoundedInputStream extends ProxyInputStream {
* @param propagateClose {@code true} if calling {@link #close()} propagates to the {@code close()} method of the underlying stream or {@code false} if it
* does not.
*/
+ @SuppressWarnings("resource") // Caller closes.
private BoundedInputStream(final InputStream inputStream, final long maxCount, final boolean propagateClose) {
// Some badly designed methods - e.g. the Servlet API - overload length
// such that "-1" means stream finished
- super(inputStream);
+ super(new CountingInputStream(inputStream));
this.maxCount = maxCount;
this.propagateClose = propagateClose;
}
- /**
- * Adds the number of read bytes to the count.
- *
- * @param n number of bytes read, or -1 if no more bytes are available
- * @since 2.16.0
- */
- @Override
- protected synchronized void afterRead(final int n) {
- if (n != EOF) {
- count += n;
- }
+ private CountingInputStream getCountingInputStream() {
+ return (CountingInputStream) in;
}
/**
@@ -188,7 +174,7 @@ public class BoundedInputStream extends ProxyInputStream {
@Override
public int available() throws IOException {
if (isMaxLength()) {
- onMaxLength(maxCount, count);
+ onMaxLength(maxCount, getCount());
return 0;
}
return in.available();
@@ -212,8 +198,9 @@ public class BoundedInputStream extends ProxyInputStream {
* @return The count of bytes read.
* @since 2.12.0
*/
+ @SuppressWarnings("resource") // no allocation
public long getCount() {
- return count;
+ return getCountingInputStream().getByteCount();
}
/**
@@ -237,7 +224,7 @@ public class BoundedInputStream extends ProxyInputStream {
}
private boolean isMaxLength() {
- return maxCount >= 0 && count >= maxCount;
+ return maxCount >= 0 && getCount() >= maxCount;
}
/**
@@ -257,7 +244,6 @@ public class BoundedInputStream extends ProxyInputStream {
@Override
public synchronized void mark(final int readLimit) {
in.mark(readLimit);
- mark = count;
}
/**
@@ -292,7 +278,7 @@ public class BoundedInputStream extends ProxyInputStream {
@Override
public int read() throws IOException {
if (isMaxLength()) {
- onMaxLength(maxCount, count);
+ onMaxLength(maxCount, getCount());
return EOF;
}
return super.read();
@@ -322,7 +308,7 @@ public class BoundedInputStream extends ProxyInputStream {
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
if (isMaxLength()) {
- onMaxLength(maxCount, count);
+ onMaxLength(maxCount, getCount());
return EOF;
}
return super.read(b, off, (int) toReadLen(len));
@@ -336,7 +322,6 @@ public class BoundedInputStream extends ProxyInputStream {
@Override
public synchronized void reset() throws IOException {
in.reset();
- count = mark;
}
/**
@@ -364,7 +349,7 @@ public class BoundedInputStream extends ProxyInputStream {
}
private long toReadLen(final long len) {
- return maxCount >= 0 ? Math.min(len, maxCount - count) : len;
+ return maxCount >= 0 ? Math.min(len, maxCount - getCount()) : len;
}
/**