Skip to content

fix: remove redundant buffer copy in IoUtils.toByteArray(InputStream)#963

Open
nkuprins wants to merge 1 commit into
apache:mainfrom
nkuprins:fix/iotutils-redundant-buffer-copy
Open

fix: remove redundant buffer copy in IoUtils.toByteArray(InputStream)#963
nkuprins wants to merge 1 commit into
apache:mainfrom
nkuprins:fix/iotutils-redundant-buffer-copy

Conversation

@nkuprins

Copy link
Copy Markdown

Purpose of the pull request

Closed: #952

What's changed?

IoUtils.toByteArray(InputStream) had a finally block calling output.toByteArray() and discarding the result. ByteArrayOutputStream.toByteArray() allocates a full copy of the internal buffer, so every call allocated the payload twice - a leftover from translating commons-io's try-with-resources version, where the finally would have been output.close() (a no-op for ByteArrayOutputStream).

This removes the try/finally entirely.

Testing

No behaviour change. Existing IoUtilsTest (7 cases) passes unchanged.

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

The finally block called output.toByteArray() and discarded the result.
ByteArrayOutputStream.toByteArray() allocates a full copy of the internal
buffer, so every call allocated the payload twice. ByteArrayOutputStream
needs no closing, so the try/finally is removed entirely.

Closes apache#952
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Remove redundant buffer copy in IoUtils.toByteArray(InputStream)

1 participant