Skip to content

refactor: use ByteBuffer fluent API chains with JDK 9 covariant returns#3139

Merged
He-Pin merged 1 commit into
mainfrom
refactor/bytebuffer-fluent-api
Jun 23, 2026
Merged

refactor: use ByteBuffer fluent API chains with JDK 9 covariant returns#3139
He-Pin merged 1 commit into
mainfrom
refactor/bytebuffer-fluent-api

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 23, 2026

Copy link
Copy Markdown
Member

Motivation

JDK 9 changed ByteBuffer.clear(), limit(int), flip(), rewind() to return ByteBuffer instead of Buffer, enabling fluent method chaining. Previously these returned Buffer, requiring either explicit casts or separate statements.

Modification

Consolidate separate buffer.clear() + buffer.limit(n) statements into single expressions passed directly to channel.read()/channel.receive(). Chain buffer.clear().flip() in TlsEngineHelpers.emptyReadBuffer.

File Before After
TcpConnection.innerRead 4 statements 3 statements
UdpConnection.innerRead 3 statements 1 statement
UdpListener.innerReceive 3 statements 1 statement
TlsEngineHelpers.emptyReadBuffer 2 statements 1 statement

Result

More concise I/O code leveraging JDK 9+ ByteBuffer covariant returns. Semantically identical — clear/limit/flip execution order unchanged.

Tests

  • sbt "actor-tests / Test / testOnly org.apache.pekko.io.TcpConnectionSpec" — 34/34 passed
  • sbt "actor-tests / Test / testOnly org.apache.pekko.io.UdpIntegrationSpec" — 7/7 passed

References

Refs #3136

Motivation:
JDK 9 changed ByteBuffer.clear(), limit(int), flip(), rewind() to return
ByteBuffer instead of Buffer, enabling fluent method chaining.

Modification:
Consolidate separate buffer.clear() + buffer.limit(n) statements into
single expressions passed directly to channel.read()/channel.receive().
Chain buffer.clear().flip() in TlsEngineHelpers.emptyReadBuffer.

- TcpConnection.innerRead: 4 statements → 3
- UdpConnection.innerRead: 3 statements → 1
- UdpListener.innerReceive: 3 statements → 1
- TlsEngineHelpers.emptyReadBuffer: 2 statements → 1

Result:
More concise I/O code leveraging JDK 9+ ByteBuffer covariant returns.
Semantically identical — clear/limit/flip order unchanged.

Tests:
- sbt "actor-tests / Test / testOnly org.apache.pekko.io.TcpConnectionSpec" — 34/34 passed
- sbt "actor-tests / Test / testOnly org.apache.pekko.io.UdpIntegrationSpec" — 7/7 passed

References:
Refs #3136

@pjfanning pjfanning 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.

lgtm

@He-Pin He-Pin merged commit 52755c4 into main Jun 23, 2026
9 checks passed
@He-Pin He-Pin deleted the refactor/bytebuffer-fluent-api branch June 23, 2026 09:18
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