Add CloseShieldChannel to close-shielded NIO Channels - #786
Merged
Conversation
…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.
Contributor
There was a problem hiding this comment.
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.
Member
|
@ppkarwasz TY. PR merged! |
Channels.closeShield for close-shield proxies of NIO channels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
Channels.closeShield(Channel)to return a JDK proxy that preserves the delegate’sChannelsub-interfaces and shields the underlying channel fromclose().Behavior
close()flips shield state; delegate is not closed.isOpen()reflects shield state and delegates if shield is not closed.ClosedChannelException; safe queries (e.g.,NetworkChannel.supportedOptions()) still work.this(e.g.,SeekableByteChannel.position/truncate,NetworkChannel.bind/setOption) return the proxy.equals/hashCode/toStringTests
SeekableByteChannel/NetworkChanneland super-interfaces are tested.