Skip to content

[IO-780] Do not discard CharsetEncoder errors in ReaderInputStream - #874

Merged
garydgregory merged 2 commits into
apache:masterfrom
tanvir-ux:IO-780-reader-input-stream-encode-error
Aug 28, 2026
Merged

[IO-780] Do not discard CharsetEncoder errors in ReaderInputStream#874
garydgregory merged 2 commits into
apache:masterfrom
tanvir-ux:IO-780-reader-input-stream-encode-error

Conversation

@tanvir-ux

@tanvir-ux tanvir-ux commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute? Assisted with drafting the patch and PR text; change and tests authored and verified locally as Md Tanvir Alam.
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

Fixes IO-780.

ReaderInputStream.fillBuffer() called CharsetEncoder.flush() whenever the reader had reached EOF, overwriting lastCoderResult from encode(). When encode() reported malformed input (for example a trailing unpaired surrogate with the default CodingErrorAction.REPORT), the error was discarded and read() returned -1 instead of throwing.

This checks lastCoderResult.isError() after encode() and after flush().

The existing testCodingError() WIP now asserts CharacterCodingException for both read() and read(byte[]), matching the JIRA reproducer.

flush() was overwriting lastCoderResult after encode() reported malformed
input (for example a trailing unpaired surrogate), so read() returned EOF
instead of throwing CharacterCodingException.
@garydgregory

garydgregory commented Aug 27, 2026

Copy link
Copy Markdown
Member

@tanvir-ux
Please follow the instructions in the PR template to avoid wasting time and resources on failing builds.

@tanvir-ux

Copy link
Copy Markdown
Contributor Author

Sorry about the wasted CI — leftover unused assertDoesNotThrow import after flipping testCodingError to assertThrows. Removed it in 6df255f. ReaderInputStreamTest is 27/0 locally.

@garydgregory
garydgregory merged commit a49c603 into apache:master Aug 28, 2026
24 checks passed
@garydgregory

Copy link
Copy Markdown
Member

@tanvir-ux Thank you for the update, merged 🚀

@tanvir-ux

Copy link
Copy Markdown
Contributor Author

Awesome! Thanks a lot!!

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.

2 participants