Skip to content

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

Merged
ArneBab merged 6 commits into
hyphanet:nextfrom
bertm:single-bytes
Jun 22, 2025
Merged

Fix single-byte read() in various InputStream implementations#1058
ArneBab merged 6 commits into
hyphanet:nextfrom
bertm:single-bytes

Conversation

@bertm

@bertm bertm commented May 11, 2025

Copy link
Copy Markdown
Contributor

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.

@Bombe Bombe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You have now written the same method about six times (seven, actually, remembering the other PR)… maybe we should put that into some InputStreamUtils helper class?

@ArneBab ArneBab merged commit d22dd53 into hyphanet:next Jun 22, 2025
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.

3 participants