[ 
https://issues.apache.org/jira/browse/IO-861?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17896923#comment-17896923
 ] 

Gary D. Gregory commented on IO-861:
------------------------------------

Hello [~msqr]

Please try git master or a snapshot build from 
[https://repository.apache.org/content/repositories/snapshots/]

Instead of a MaxUploadSizeInputStream extending BoundedInputStream, you can use 
an "OnMaxCount" consumer like this:
{code:java}
        try (BoundedInputStream bounded = BoundedInputStream.builder()
                .setInputStream(new ByteArrayInputStream(hello))
                .setMaxCount(hello.length)
                .setOnMaxCount((m, c) -> {
                    throw new SomeIOException(message);
                })
                .get()) {
            // ... consume the input stream ...
        }

{code}
See the tests: 
[https://github.com/apache/commons-io/blob/62643af540d47afc6e03680ea867a47aad0b943b/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java#L320-L347]

Same style for "afterRead" where you can use an unbounded BoundedInputStream 
instead of the deprecated CountingInputStream:
{code:java}
        try (InputStream bounded = BoundedInputStream.builder()
                .setInputStream(new ByteArrayInputStream(hello))
                .setMaxCount(hello.length)
                .setAfterRead(i -> boolRef.set(true))
                .get()) {
            IOUtils.consume(bounded);
        }
{code}
See the tests: 
[https://github.com/apache/commons-io/blob/62643af540d47afc6e03680ea867a47aad0b943b/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java#L291-L317]

> BoundedInputStream is hard to subclass becauase constructors are deprecated
> ---------------------------------------------------------------------------
>
>                 Key: IO-861
>                 URL: https://issues.apache.org/jira/browse/IO-861
>             Project: Commons IO
>          Issue Type: Improvement
>          Components: Streams/Writers
>    Affects Versions: 2.17.0
>            Reporter: Matt Magoffin
>            Priority: Major
>
> The {{org.apache.commons.io.input.BoundedInputStream}} class added a 
> {{Builder}} in version 2.16 (I think), and also annotated all public 
> constructors with {{{}@Deprecated{}}}. I have classes that extend 
> {{BoundedInputStream}} < 2.16 that make use of those constructors, and I'd 
> like to avoid using deprecated code in version 2.17. I do not see any other 
> way to sub-class {{{}BoundedInputStream{}}}, however.
> The purpose of my subclass is to override the provided {{onMaxLength}} method:
> {code:java}
> protected void onMaxLength(final long maxLength, final long count) throws 
> IOException {
>     // for subclasses
> }
> {code}
> method, which has been specifically designed for sub-classes like mine to 
> override. Would it be possible to remove the deprecation on at least one 
> constructor, or is there some other way I'm missing?



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to