Skip to content

Commit 99702ec

Browse files
committed
Reuse CountingInputStream
1 parent 713e964 commit 99702ec

1 file changed

Lines changed: 12 additions & 27 deletions

File tree

src/main/java/org/apache/commons/io/input/BoundedInputStream.java

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737
public class BoundedInputStream extends ProxyInputStream {
3838

39-
// TODO For 3.0, extend CountingInputStream.
39+
// TODO For 3.0, extend CountingInputStream. Or, add a max feature to CountingInputStream.
4040

4141
/**
4242
* Builds a new {@link BoundedInputStream} instance.
@@ -115,12 +115,6 @@ public static Builder builder() {
115115
/** The max count of bytes to read. */
116116
private final long maxCount;
117117

118-
/** The count of bytes read. */
119-
private long count;
120-
121-
/** The marked position. */
122-
private long mark = EOF;
123-
124118
/**
125119
* Flag if close should be propagated.
126120
*
@@ -161,25 +155,17 @@ public BoundedInputStream(final InputStream inputStream, final long maxCount) {
161155
* @param propagateClose {@code true} if calling {@link #close()} propagates to the {@code close()} method of the underlying stream or {@code false} if it
162156
* does not.
163157
*/
158+
@SuppressWarnings("resource") // Caller closes.
164159
private BoundedInputStream(final InputStream inputStream, final long maxCount, final boolean propagateClose) {
165160
// Some badly designed methods - e.g. the Servlet API - overload length
166161
// such that "-1" means stream finished
167-
super(inputStream);
162+
super(new CountingInputStream(inputStream));
168163
this.maxCount = maxCount;
169164
this.propagateClose = propagateClose;
170165
}
171166

172-
/**
173-
* Adds the number of read bytes to the count.
174-
*
175-
* @param n number of bytes read, or -1 if no more bytes are available
176-
* @since 2.16.0
177-
*/
178-
@Override
179-
protected synchronized void afterRead(final int n) {
180-
if (n != EOF) {
181-
count += n;
182-
}
167+
private CountingInputStream getCountingInputStream() {
168+
return (CountingInputStream) in;
183169
}
184170

185171
/**
@@ -188,7 +174,7 @@ protected synchronized void afterRead(final int n) {
188174
@Override
189175
public int available() throws IOException {
190176
if (isMaxLength()) {
191-
onMaxLength(maxCount, count);
177+
onMaxLength(maxCount, getCount());
192178
return 0;
193179
}
194180
return in.available();
@@ -212,8 +198,9 @@ public void close() throws IOException {
212198
* @return The count of bytes read.
213199
* @since 2.12.0
214200
*/
201+
@SuppressWarnings("resource") // no allocation
215202
public long getCount() {
216-
return count;
203+
return getCountingInputStream().getByteCount();
217204
}
218205

219206
/**
@@ -237,7 +224,7 @@ public long getRemaining() {
237224
}
238225

239226
private boolean isMaxLength() {
240-
return maxCount >= 0 && count >= maxCount;
227+
return maxCount >= 0 && getCount() >= maxCount;
241228
}
242229

243230
/**
@@ -257,7 +244,6 @@ public boolean isPropagateClose() {
257244
@Override
258245
public synchronized void mark(final int readLimit) {
259246
in.mark(readLimit);
260-
mark = count;
261247
}
262248

263249
/**
@@ -292,7 +278,7 @@ protected void onMaxLength(final long maxLength, final long count) throws IOExce
292278
@Override
293279
public int read() throws IOException {
294280
if (isMaxLength()) {
295-
onMaxLength(maxCount, count);
281+
onMaxLength(maxCount, getCount());
296282
return EOF;
297283
}
298284
return super.read();
@@ -322,7 +308,7 @@ public int read(final byte[] b) throws IOException {
322308
@Override
323309
public int read(final byte[] b, final int off, final int len) throws IOException {
324310
if (isMaxLength()) {
325-
onMaxLength(maxCount, count);
311+
onMaxLength(maxCount, getCount());
326312
return EOF;
327313
}
328314
return super.read(b, off, (int) toReadLen(len));
@@ -336,7 +322,6 @@ public int read(final byte[] b, final int off, final int len) throws IOException
336322
@Override
337323
public synchronized void reset() throws IOException {
338324
in.reset();
339-
count = mark;
340325
}
341326

342327
/**
@@ -364,7 +349,7 @@ public long skip(final long n) throws IOException {
364349
}
365350

366351
private long toReadLen(final long len) {
367-
return maxCount >= 0 ? Math.min(len, maxCount - count) : len;
352+
return maxCount >= 0 ? Math.min(len, maxCount - getCount()) : len;
368353
}
369354

370355
/**

0 commit comments

Comments
 (0)