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 abaae750a652688a9ecffe534c789900d404364d Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Dec 27 08:20:26 2023 -0500 Deprecate CountingInputStream.getMaxLength() in favor of getMaxCount()) --- src/changes/changes.xml | 3 +- .../commons/io/input/BoundedInputStream.java | 36 ++++++++++++++-------- .../commons/io/input/BoundedInputStreamTest.java | 12 +++++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index c7493f81..89022c6d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -79,7 +79,8 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" due-to="Elliotte Rusty Harold">Add PathMatcher to IOFileFilter class Javadoc #536.</action> <action dev="ggregory" type="fix" issue="IO-781" due-to="Marcono1234">Fix CharSequenceInputStream coding exception handling #537.</action> <action dev="ggregory" type="fix" issue="IO-781" due-to="Marcono1234">Deprecate int CountingInputStream#getCount() in favor of long CountingInputStream#getByteCount().</action> - <action dev="ggregory" type="fix" issue="IO-828" due-to="Elliotte Rusty Harold, Gary Gregory">Deprecate CountingInputStream.resetCount().</action> + <action dev="ggregory" type="fix" issue="IO-828" due-to="Elliotte Rusty Harold, Gary Gregory">Deprecate CountingInputStream.resetCount() in favor of resetByteCount().</action> + <action dev="ggregory" type="fix" issue="IO-828" due-to="Gary Gregory">Deprecate CountingInputStream.getMaxLength() in favor of getMaxCount()).</action> <!-- Add --> <action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function<Path, R>).</action> <action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action> 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 7a83007b..8ea82200 100644 --- a/src/main/java/org/apache/commons/io/input/BoundedInputStream.java +++ b/src/main/java/org/apache/commons/io/input/BoundedInputStream.java @@ -44,14 +44,14 @@ public class BoundedInputStream extends ProxyInputStream { * <h2>Using NIO</h2> * * <pre>{@code - * BoundedInputStream s = BoundedInputStream.builder().setPath(Paths.get("MyFile.xml")).setMaxLength(1024).setPropagateClose(false).get(); + * BoundedInputStream s = BoundedInputStream.builder().setPath(Paths.get("MyFile.xml")).setMaxCount(1024).setPropagateClose(false).get(); * } * </pre> * * <h2>Using IO</h2> * * <pre>{@code - * BoundedInputStream s = BoundedInputStream.builder().setFile(new File("MyFile.xml")).setMaxLength(1024).setPropagateClose(false).get(); + * BoundedInputStream s = BoundedInputStream.builder().setFile(new File("MyFile.xml")).setMaxCount(1024).setPropagateClose(false).get(); * } * </pre> * @@ -60,7 +60,7 @@ public class BoundedInputStream extends ProxyInputStream { public static class Builder extends AbstractStreamBuilder<BoundedInputStream, Builder> { /** The max count of bytes to read. */ - private long maxLength = EOF; + private long maxCount = EOF; /** Flag if close should be propagated. */ private boolean propagateClose = true; @@ -68,7 +68,7 @@ public class BoundedInputStream extends ProxyInputStream { @SuppressWarnings("resource") @Override public BoundedInputStream get() throws IOException { - return new BoundedInputStream(getInputStream(), maxLength, propagateClose); + return new BoundedInputStream(getInputStream(), maxCount, propagateClose); } /** @@ -77,11 +77,11 @@ public class BoundedInputStream extends ProxyInputStream { * Default is {@value IOUtils#EOF}. * </p> * - * @param maxLength The maximum number of bytes to return. + * @param maxCount The maximum number of bytes to return. * @return this. */ - public Builder setMaxLength(final long maxLength) { - this.maxLength = maxLength; + public Builder setMaxCount(final long maxCount) { + this.maxCount = maxCount; return this; } @@ -173,7 +173,7 @@ public class BoundedInputStream extends ProxyInputStream { */ @Override public int available() throws IOException { - if (isMaxLength()) { + if (isMaxCount()) { onMaxLength(maxCount, getCount()); return 0; } @@ -203,12 +203,24 @@ public class BoundedInputStream extends ProxyInputStream { return getCountingInputStream().getByteCount(); } + /** + * Gets the max count of bytes to read. + * + * @return The max count of bytes to read. + * @since 2.16.0 + */ + public long getMaxCount() { + return maxCount; + } + /** * Gets the max count of bytes to read. * * @return The max count of bytes to read. * @since 2.12.0 + * @deprecated Use {@link #getMaxCount()}. */ + @Deprecated public long getMaxLength() { return maxCount; } @@ -220,10 +232,10 @@ public class BoundedInputStream extends ProxyInputStream { * @since 2.16.0 */ public long getRemaining() { - return getMaxLength() - getCount(); + return getMaxCount() - getCount(); } - private boolean isMaxLength() { + private boolean isMaxCount() { return maxCount >= 0 && getCount() >= maxCount; } @@ -277,7 +289,7 @@ public class BoundedInputStream extends ProxyInputStream { */ @Override public int read() throws IOException { - if (isMaxLength()) { + if (isMaxCount()) { onMaxLength(maxCount, getCount()); return EOF; } @@ -307,7 +319,7 @@ public class BoundedInputStream extends ProxyInputStream { */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { - if (isMaxLength()) { + if (isMaxCount()) { onMaxLength(maxCount, getCount()); return EOF; } diff --git a/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java b/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java index a8678a47..79214544 100644 --- a/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java @@ -54,16 +54,20 @@ public class BoundedInputStreamTest { boolRef.set(true); } }; + assertEquals(helloWorld.length, bounded.getMaxCount()); assertEquals(helloWorld.length, bounded.getMaxLength()); assertEquals(0, bounded.getCount()); + assertEquals(bounded.getMaxCount(), bounded.getRemaining()); assertEquals(bounded.getMaxLength(), bounded.getRemaining()); assertFalse(boolRef.get()); int readCount = 0; for (int i = 0; i < helloWorld.length; i++) { assertEquals(helloWorld[i], bounded.read(), "limit = length byte[" + i + "]"); readCount++; + assertEquals(helloWorld.length, bounded.getMaxCount()); assertEquals(helloWorld.length, bounded.getMaxLength()); assertEquals(readCount, bounded.getCount()); + assertEquals(bounded.getMaxCount() - readCount, bounded.getRemaining()); assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining()); } assertEquals(-1, bounded.read(), "limit = length end"); @@ -136,20 +140,20 @@ public class BoundedInputStreamTest { compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded)); } - try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxLength(0).get()) { + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(0).get()) { compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); } - try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxLength(helloWorld.length) + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(helloWorld.length) .get()) { compare("limit = length", helloWorld, IOUtils.toByteArray(bounded)); } - try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxLength(helloWorld.length + 1) + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(helloWorld.length + 1) .get()) { compare("limit > length", helloWorld, IOUtils.toByteArray(bounded)); } - try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxLength(helloWorld.length - 6) + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(helloWorld.length - 6) .get()) { compare("limit < length", hello, IOUtils.toByteArray(bounded)); }