Skip to content

Fix ByteBuffer handling in ForwardingSocketChannel - #96

Merged
eed3si9n merged 1 commit into
sbt:developfrom
lrytz:pr/forwarding-channel-io
Oct 29, 2026
Merged

eed3si9n merged 1 commit into
sbt:developfrom
lrytz:pr/forwarding-channel-io

Conversation

@lrytz

@lrytz lrytz commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

ForwardingSocketChannel makes a socket usable as a SocketChannel. Windows always uses it, and Unix uses it on Java versions before 17. It ignored the position and limit of the ByteBuffers passed to it:

  • read threw IndexOutOfBoundsException at end of stream instead of returning -1, and could put the data at the wrong place in the buffer.
  • write sent the buffer's whole backing array and didn't advance its position, so a loop that writes until the buffer is empty never ended.
  • The read and write variants that take several buffers only used the first one.

Now read and write respect the buffer's position and limit, and the variants with several buffers use all of them. Also, getRemoteAddress returned the local address instead of the remote one.

ForwardingSocketChannel makes a socket usable as a SocketChannel.
Windows always uses it, and Unix uses it on Java versions before 17. It
ignored the position and limit of the ByteBuffers passed to it:

- read threw IndexOutOfBoundsException at end of stream instead of
  returning -1, and could put the data at the wrong place in the buffer.
- write sent the buffer's whole backing array and didn't advance its
  position, so a loop that writes until the buffer is empty never ended.
- The read and write variants that take several buffers only used the
  first one.

Now read and write respect the buffer's position and limit, and the
variants with several buffers use all of them. Also, getRemoteAddress
returned the local address instead of the remote one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lrytz lrytz mentioned this pull request Oct 28, 2026

@eed3si9n eed3si9n left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@eed3si9n
eed3si9n merged commit 68d7c89 into sbt:develop Oct 29, 2026
7 checks passed
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