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

Buffer.[set|write]Bytes doesn't respect length when reading from InputStream #2389

Merged
merged 1 commit into from
Oct 11, 2022

Conversation

idelpivnitskiy
Copy link
Member

@idelpivnitskiy idelpivnitskiy commented Oct 7, 2022

Motivation:

Javadoc for Buffer.writeBytes and Buffer.setBytes says it reads
"length bytes have been read, the end of stream is reached, or an
exception is thrown". However, its internal implementation in Netty
does only a single InputStream.read(byte[], int, int) invocation.
InputStream contract does not guarantee to read the whole requested
length, it says "reads up to len bytes of data from the input stream".
There are implementations that may return less than len bytes on a
single read attempt.

Modifications:

  • NettyBuffer keeps reading from the InputStream until length bytes
    have been read;
  • Add tests to demonstrate a problem;
  • Also add tests for writeBytesUntilEndStream bcz it's a custom
    method that does not exist in Netty;

Result:

Buffer.writeBytes and Buffer.setBytes behave according to their
javadoc when reading from InputStream.

…putStream

Motivation:

Javadoc for `Buffer.writeBytes` and `Buffer.setBytes` says it reads
"length bytes have been read, the end of stream is reached, or an
exception is thrown". However, its internal implementation in Netty
does only a single `InputStream.read(byte[], int, int)` invocation.
`InputStream` contract does not guarantees to read the whole requested
`length`, is says "reads up to len bytes of data from the input stream".
There are implementations that may return less than `len` bytes on a
single read attempt.

Modifications:

- `NettyBuffer` keeps reading from the `InputStream` until length bytes
have been read;
- Add tests to demonstrate a problem;
- Also add tests for `writeBytesUntilEndStream` bcz it's a custom
method that does not exist in Netty;

Result:

`Buffer.writeBytes` and `Buffer.setBytes` behave according to their
javadoc when reading from `InputStream`.
@idelpivnitskiy idelpivnitskiy self-assigned this Oct 7, 2022
Copy link
Contributor

@mgodave mgodave left a comment

Choose a reason for hiding this comment

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

lgtm

@idelpivnitskiy idelpivnitskiy merged commit aebad36 into apple:main Oct 11, 2022
@idelpivnitskiy idelpivnitskiy deleted the buffer branch October 11, 2022 23:42
idelpivnitskiy added a commit to idelpivnitskiy/servicetalk that referenced this pull request Oct 13, 2022
Motivation:

apple#2389 introduced a bug for `Buffer.[set|write]Bytes` methods that
returns `0` instead of `-1` after EOF.

Modifications:

- Make sure we preserve `-1` signal if nothing else was read from the
`InputStream`;

Result:

`Buffer.[set|write]Bytes` correctly return `-1` after EOF.
idelpivnitskiy added a commit that referenced this pull request Oct 13, 2022
Motivation:

#2389 introduced a bug for `Buffer.[set|write]Bytes` methods that
returns `0` instead of `-1` after EOF.

Modifications:

- Make sure we preserve `-1` signal if nothing else was read from the
`InputStream`;

Result:

`Buffer.[set|write]Bytes` correctly return `-1` after EOF.
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