Skip to content

Commit d0c53cd

Browse files
committed
Reuse ProxyInputStream
1 parent e28584c commit d0c53cd

1 file changed

Lines changed: 38 additions & 37 deletions

File tree

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

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static org.apache.commons.io.IOUtils.EOF;
2020

21-
import java.io.FilterInputStream;
2221
import java.io.IOException;
2322
import java.io.InputStream;
2423

@@ -28,16 +27,16 @@
2827
/**
2928
* Reads bytes up to a maximum length, if its count goes above that, it stops.
3029
* <p>
31-
* This is useful to wrap ServletInputStreams. The ServletInputStream will block if you try to read content from it that isn't there, because it doesn't know
32-
* whether the content hasn't arrived yet or whether the content has finished. So, one of these, initialized with the Content-length sent in the
33-
* ServletInputStream's header, will stop it blocking, providing it's been sent with a correct content length.
30+
* This is useful to wrap {@code ServletInputStream}s. The {@code ServletInputStream} will block if you try to read content from it that isn't there, because it
31+
* doesn't know whether the content hasn't arrived yet or whether the content has finished. So, one of these, initialized with the {@code Content-Length} sent
32+
* in the {@code ServletInputStream}'s header, will stop it blocking, providing it's been sent with a correct content length.
3433
* </p>
3534
*
3635
* @since 2.0
3736
*/
38-
public class BoundedInputStream extends FilterInputStream {
39-
40-
// TODO For 3.0, extend CountintInputStream.
37+
public class BoundedInputStream extends ProxyInputStream {
38+
39+
// TODO For 3.0, extend CountingInputStream.
4140

4241
/**
4342
* Builds a new {@link BoundedInputStream} instance.
@@ -164,13 +163,26 @@ public BoundedInputStream(final InputStream inputStream, final long maxLength) {
164163
* does not.
165164
*/
166165
private BoundedInputStream(final InputStream inputStream, final long maxLength, final boolean propagateClose) {
167-
// Some badly designed methods - e.g. the servlet API - overload length
166+
// Some badly designed methods - e.g. the Servlet API - overload length
168167
// such that "-1" means stream finished
169168
super(inputStream);
170169
this.maxCount = maxLength;
171170
this.propagateClose = propagateClose;
172171
}
173172

173+
/**
174+
* Adds the number of read bytes to the count.
175+
*
176+
* @param n number of bytes read, or -1 if no more bytes are available
177+
* @since 2.16.0
178+
*/
179+
@Override
180+
protected synchronized void afterRead(final int n) {
181+
if (n != EOF) {
182+
count += n;
183+
}
184+
}
185+
174186
/**
175187
* {@inheritDoc}
176188
*/
@@ -184,7 +196,7 @@ public int available() throws IOException {
184196
}
185197

186198
/**
187-
* Invokes the delegate's {@code close()} method if {@link #isPropagateClose()} is {@code true}.
199+
* Invokes the delegate's {@link InputStream#close()} method if {@link #isPropagateClose()} is {@code true}.
188200
*
189201
* @throws IOException if an I/O error occurs.
190202
*/
@@ -239,7 +251,7 @@ public boolean isPropagateClose() {
239251
}
240252

241253
/**
242-
* Invokes the delegate's {@code mark(int)} method.
254+
* Invokes the delegate's {@link InputStream#mark(int)} method.
243255
*
244256
* @param readLimit read ahead limit
245257
*/
@@ -250,7 +262,7 @@ public synchronized void mark(final int readLimit) {
250262
}
251263

252264
/**
253-
* Invokes the delegate's {@code markSupported()} method.
265+
* Invokes the delegate's {@link InputStream#markSupported()} method.
254266
*
255267
* @return true if mark is supported, otherwise false
256268
*/
@@ -273,7 +285,7 @@ protected void onMaxLength(final long maxLength, final long count) throws IOExce
273285
}
274286

275287
/**
276-
* Invokes the delegate's {@code read()} method if the current position is less than the limit.
288+
* Invokes the delegate's {@link InputStream#read()} method if the current position is less than the limit.
277289
*
278290
* @return the byte read or -1 if the end of stream or the limit has been reached.
279291
* @throws IOException if an I/O error occurs.
@@ -284,27 +296,23 @@ public int read() throws IOException {
284296
onMaxLength(maxCount, count);
285297
return EOF;
286298
}
287-
final int result = in.read();
288-
if (result != EOF) {
289-
count++;
290-
}
291-
return result;
299+
return super.read();
292300
}
293301

294302
/**
295-
* Invokes the delegate's {@code read(byte[])} method.
303+
* Invokes the delegate's {@link InputStream#read(byte[])} method.
296304
*
297305
* @param b the buffer to read the bytes into
298306
* @return the number of bytes read or -1 if the end of stream or the limit has been reached.
299307
* @throws IOException if an I/O error occurs.
300308
*/
301309
@Override
302310
public int read(final byte[] b) throws IOException {
303-
return this.read(b, 0, b.length);
311+
return read(b, 0, b.length);
304312
}
305313

306314
/**
307-
* Invokes the delegate's {@code read(byte[], int, int)} method.
315+
* Invokes the delegate's {@link InputStream#read(byte[], int, int)} method.
308316
*
309317
* @param b the buffer to read the bytes into
310318
* @param off The start offset
@@ -318,19 +326,11 @@ public int read(final byte[] b, final int off, final int len) throws IOException
318326
onMaxLength(maxCount, count);
319327
return EOF;
320328
}
321-
final long maxRead = maxCount >= 0 ? Math.min(len, maxCount - count) : len;
322-
final int bytesRead = in.read(b, off, (int) maxRead);
323-
324-
if (bytesRead == EOF) {
325-
return EOF;
326-
}
327-
328-
count += bytesRead;
329-
return bytesRead;
329+
return super.read(b, off, (int) toReadLen(len));
330330
}
331331

332332
/**
333-
* Invokes the delegate's {@code reset()} method.
333+
* Invokes the delegate's {@link InputStream#reset()} method.
334334
*
335335
* @throws IOException if an I/O error occurs.
336336
*/
@@ -353,24 +353,25 @@ public void setPropagateClose(final boolean propagateClose) {
353353
}
354354

355355
/**
356-
* Invokes the delegate's {@code skip(long)} method.
356+
* Invokes the delegate's {@link InputStream#skip(long)} method.
357357
*
358358
* @param n the number of bytes to skip
359359
* @return the actual number of bytes skipped
360360
* @throws IOException if an I/O error occurs.
361361
*/
362362
@Override
363363
public long skip(final long n) throws IOException {
364-
final long toSkip = maxCount >= 0 ? Math.min(n, maxCount - count) : n;
365-
final long skippedBytes = in.skip(toSkip);
366-
count += skippedBytes;
367-
return skippedBytes;
364+
return super.skip(toReadLen(n));
365+
}
366+
367+
private long toReadLen(final long len) {
368+
return maxCount >= 0 ? Math.min(len, maxCount - count) : len;
368369
}
369370

370371
/**
371-
* Invokes the delegate's {@code toString()} method.
372+
* Invokes the delegate's {@link InputStream#toString()} method.
372373
*
373-
* @return the delegate's {@code toString()}
374+
* @return the delegate's {@link InputStream#toString()}
374375
*/
375376
@Override
376377
public String toString() {

0 commit comments

Comments
 (0)