[
https://issues.apache.org/jira/browse/IO-861?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17896036#comment-17896036
]
Matt Magoffin commented on IO-861:
----------------------------------
Sure, here's a very simple example, where I need a specific exception thrown
when the maximum length is reached:
{code:java}
public class MaxUploadSizeInputStream extends BoundedInputStream {
public MaxUploadSizeInputStream(InputStream inputStream, long
maxLength) {
super(inputStream, maxLength);
}
@Override
protected void onMaxLength(long maxLength, long count) throws
IOException {
throw new MaxUploadSizeExceededException(getMaxLength());
}
}
{code}
Here's another one that uses the now-deprecated {{CountingInputStream}}, which
in theory should be refactored to use {{BoundedInputStream}} which has the same
protected {{afterRead()}} method available, but would face the same issue:
{code:java}
public class DatumImportProgressInputStream extends CountingInputStream {
private final long expectedLength;
private final DatumImportService progressContext;
private final ProgressListener progressListener;
public DatumImportProgressInputStream(InputStream in, long
expectedLength,
DatumImportService progressContext, ProgressListener
progressListener) {
super(in);
this.expectedLength = expectedLength;
this.progressContext = progressContext;
this.progressListener = progressListener;
}
@Override
protected synchronized void afterRead(int n) throws IOException {
super.afterRead(n);
if ( n > 0 && progressListener != null ) {
progressListener.progressChanged(this.progressContext,
Math.min(1.0, (double) getByteCount() /
expectedLength));
}
}
}
{code}
> 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)