Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Use InputStream.transferTo #2669

Merged
merged 3 commits into from
Sep 3, 2024
Merged

Conversation

dsmiley
Copy link
Contributor

@dsmiley dsmiley commented Aug 26, 2024

Instead of longer code that accomplishes the same.

Instead of longer code that accomplishes the same.
if (null != dest) {
dest.flush();
if (dest == null) {
dest = NullOutputStream.INSTANCE;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A commons-io utility

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

@@ -261,6 +263,16 @@ public void write(OutputStream os) throws IOException {
}
}

private static InputStream boundedInputStream(final InputStream is, final long maxLength)
throws IOException {
return new BoundedInputStream(is, maxLength) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A commons-io utility

@@ -22,6 +22,7 @@
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

@Deprecated
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add the since tag to help us remember when to remove this deprecated method? There are so many deprecated methods that just kind of hang around because we don't have good mechanisms for deciding/remembering to remove them!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted it; we don't need to wait for internal utilities

try (BAOS bos = new BAOS()) {
long sz = 0;
int next = is.read();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code was very sad, reading one char at a time!

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int r;
/** Reads input completely into a byte array. Closes the stream. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels super trappy to have this method close the stream and Utils.toByteArray leave it open, but not something we can fix here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; it's unusual for a method to accept an InputStream and also close it. I at least documented it. And I reduced its scope from public. I was tempted to rename the method to something like streamAsBytesAndClose so that the closing is super apparent at the caller. Can still do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have the andClose pattern in other places...

@madrob
Copy link
Contributor

madrob commented Aug 26, 2024

We're on commons-io 2.15.1? We should upgrade to 2.16.1 in this PR, since you're already looking at it and I remember that some stream closing behavior changed between these two versions. Otherwise somebody is going to be very confused without context when tests break.

Copy link
Contributor

@epugh epugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The upgrade to commons-io would be nice.

@janhoy
Copy link
Contributor

janhoy commented Aug 26, 2024

We're on commons-io 2.15.1? We should upgrade to 2.16.1 in this PR

Already open PR for it, but blocked on forbidden-apis: #2398 and policeman-tools/forbidden-apis#249

@dsmiley
Copy link
Contributor Author

dsmiley commented Aug 27, 2024

We're on commons-io 2.15.1? We should upgrade to 2.16.1 in this PR, since you're already looking at it and I remember that some stream closing behavior changed between these two versions. Otherwise somebody is going to be very confused without context when tests break.

That's a reason not to embed that change. If this PR breaks a test, I want to debug it on its own right without wondering if it's my fault or commons-io change.

@dsmiley
Copy link
Contributor Author

dsmiley commented Aug 27, 2024

@madrob I made further changes based on your comments in the schema designer. LMK what you think.

Otherwise I think it's mergeable (no JIRA).

@dsmiley dsmiley merged commit c6651a7 into apache:main Sep 3, 2024
5 checks passed
@dsmiley dsmiley deleted the useTransferTo branch September 3, 2024 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants