Skip to content

Conversation

@ppkarwasz
Copy link
Contributor

Adds Channels.closeShield(Channel) to return a JDK proxy that preserves the delegate’s Channel sub-interfaces and shields the underlying channel from close().

Behavior

  • close() flips shield state; delegate is not closed.
  • isOpen() reflects shield state and delegates if shield is not closed.
  • After shield-close, I/O/mutating methods throw ClosedChannelException; safe queries (e.g., NetworkChannel.supportedOptions()) still work.
  • Fluent methods that return this (e.g., SeekableByteChannel.position/truncate, NetworkChannel.bind/setOption) return the proxy.
  • Stable equals/hashCode/toString
  • The method is idempotent (no double-wrapping).

Tests

  • All methods of SeekableByteChannel / NetworkChannel and super-interfaces are tested.

…nels

Adds `Channels.closeShield(Channel)` to return a JDK proxy that preserves the delegate’s `Channel` sub-interfaces and shields the underlying channel from `close()`.

**Behavior**

* `close()` flips shield state; delegate is not closed.
* `isOpen()` reflects shield state.
* After shield-close, I/O/mutating methods throw `ClosedChannelException`; safe queries (e.g., `NetworkChannel.supportedOptions()`) still work.
* Fluent methods that return `this` (e.g., `SeekableByteChannel.position/truncate`, `NetworkChannel.bind/setOption`) return the **proxy**.
* Stable `equals`/`hashCode`/`toString`
* The method is idempotent (no double-wrapping).

**Tests**

* All methods of `SeekableByteChannel` / `NetworkChannel` and super-interfaces are tested.
@ppkarwasz ppkarwasz requested a review from Copilot September 22, 2025 21:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a new utility method Channels.closeShield(Channel) that creates a close-shield proxy for NIO channels. The proxy preserves the delegate's Channel sub-interfaces and shields the underlying channel from being closed when close() is called on the proxy.

  • Introduces close-shielding functionality via JDK dynamic proxies
  • Implements comprehensive handling of all Channel interface types
  • Provides proper fluent method support and post-close behavior enforcement

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/main/java/org/apache/commons/io/channels/Channels.java Main utility class with closeShield method and interface collection logic
src/main/java/org/apache/commons/io/channels/CloseShieldChannelHandler.java InvocationHandler implementation for proxy behavior and method delegation
src/test/java/org/apache/commons/io/channels/ChannelsTest.java Comprehensive test suite covering all Channel interfaces and edge cases
src/changes/changes.xml Documentation update for the new feature

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@garydgregory garydgregory merged commit b6d09eb into master Sep 25, 2025
20 of 21 checks passed
@garydgregory
Copy link
Member

@ppkarwasz TY. PR merged!

@garydgregory garydgregory changed the title feat: Add Channels.closeShield for close-shield proxies of NIO channels Add CloseShieldChannel to close-shielded NIO Channels Sep 25, 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.

2 participants