Skip to content

Fix single-byte read() in various InputStream implementations #1058

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

Open
wants to merge 6 commits into
base: next
Choose a base branch
from

Conversation

bertm
Copy link
Contributor

@bertm bertm commented May 11, 2025

There are multiple places where the read byte is not correctly converted to unsigned int (leading to incorrect data being returned), or where the single-byte read() path differs from the multi-byte read(byte[]) or read(byte[], int, int) path.

Additionally, do not fear creating short-lived single-byte buffers - their overhead is minimal and can likely be optimized away to some extent.

bertm added 6 commits May 11, 2025 13:53
The return value was not correctly converted from signed byte to
unsigned int, leading to potential data corruption.

Additionally, remove the single-byte buffer field. Small byte[] creation
is cheap and can likely be optimized away by the JVM if this becomes a
hot code path.
The return value was not correctly converted from signed byte to
unsigned int, leading to potential data corruption.

Additionally, remove the single-byte buffer field. Small byte[] creation
is cheap and can likely be optimized away by the JVM if this becomes a
hot code path.
The counter was erroneously incremented when the end of stream was
reached unexpectedly on single-byte read(), but this case was correctly
handled for multi-byte read(byte[]).

Fix the former by reusing the latter.
The counter was erroneously incremented when the end of stream was
reached unexpectedly on single-byte read(), but this case was correctly
handled for multi-byte read(byte[]).

Fix the former by reusing the latter.
The return value was not correctly converted from signed byte to
unsigned int, leading to potential data corruption. Additionally, the
fast-path for multi-byte read(byte[]) was not implemented, which could
be a performance bottleneck when used as a plain InputStream.

Implement multi-byte read and reuse that for single-byte read.
Small byte[] creation is cheap and can likely be optimized away by the
JVM if this becomes a hot code path.
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.

1 participant