Skip to content

Commit e34706d

Browse files
committed
Add CloseShieldChannel to close-shielded NIO Channels
- Only convert set to array if required - Extract constant - Use the simple class name for CloseShieldChannel in toString()
1 parent 37cab2b commit e34706d

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The <action> type attribute can be add,update,fix,remove.
6666
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add org.apache.commons.io.file.PathUtils.getPath(String, String).</action>
6767
<action dev="ggregory" type="add" due-to="Gary Gregory">Add org.apache.commons.io.channels.ByteArraySeekableByteChannel.</action>
6868
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterable.asIterable().</action>
69-
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add CloseShieldChannel to close-shielded NIO Channels.</action>
69+
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add CloseShieldChannel to close-shielded NIO Channels #786.</action>
7070
<!-- UPDATE -->
7171
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 85 to 88 #774, #783.</action>
7272
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] Bump commons-codec:commons-codec from 1.18.0 to 1.19.0.</action>

src/main/java/org/apache/commons/io/channels/CloseShieldChannel.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,16 @@
3737
*/
3838
public final class CloseShieldChannel {
3939

40-
private static Class<?>[] collectChannelInterfaces(final Class<?> type) {
41-
final Set<Class<?>> out = new LinkedHashSet<>();
42-
collectChannelInterfaces(type, out);
43-
return out.toArray(new Class<?>[0]);
44-
}
40+
private static final Class<?>[] EMPTY = {};
4541

46-
private static void collectChannelInterfaces(final Class<?> type, final Set<Class<?>> out) {
42+
private static Set<Class<?>> collectChannelInterfaces(final Class<?> type, final Set<Class<?>> out) {
4743
// Visit interfaces
4844
for (final Class<?> iface : type.getInterfaces()) {
4945
if (Channel.class.isAssignableFrom(iface) && out.add(iface)) {
5046
collectChannelInterfaces(iface, out);
5147
}
5248
}
49+
return out;
5350
}
5451

5552
/**
@@ -63,18 +60,14 @@ private static void collectChannelInterfaces(final Class<?> type, final Set<Clas
6360
public static <T extends Channel> T wrap(final T channel) {
6461
Objects.requireNonNull(channel, "channel");
6562
// Fast path: already our shield
66-
if (Proxy.isProxyClass(channel.getClass())) {
67-
if (Proxy.getInvocationHandler(channel) instanceof CloseShieldChannelHandler) {
68-
return channel;
69-
}
63+
if (Proxy.isProxyClass(channel.getClass()) && Proxy.getInvocationHandler(channel) instanceof CloseShieldChannelHandler) {
64+
return channel;
7065
}
7166
// Collect only Channel sub-interfaces.
72-
Class<?>[] ifaces = collectChannelInterfaces(channel.getClass());
73-
if (ifaces.length == 0) {
74-
ifaces = new Class<?>[] { Channel.class }; // fallback to root surface
75-
}
67+
final Set<Class<?>> set = collectChannelInterfaces(channel.getClass(), new LinkedHashSet<>());
68+
// fallback to root surface
7669
return (T) Proxy.newProxyInstance(channel.getClass().getClassLoader(), // use delegate's loader
77-
ifaces, new CloseShieldChannelHandler(channel));
70+
set.isEmpty() ? new Class<?>[] { Channel.class } : set.toArray(EMPTY), new CloseShieldChannelHandler(channel));
7871
}
7972

8073
private CloseShieldChannel() {

src/main/java/org/apache/commons/io/channels/CloseShieldChannelHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
103103
private Object invokeObjectMethod(final Object proxy, final Method method, final Object[] args) {
104104
switch (method.getName()) {
105105
case "toString":
106-
return "CloseShield(" + delegate + ")";
106+
return "CloseShieldChannel(" + delegate + ")";
107107
case "hashCode":
108108
return Objects.hashCode(delegate);
109109
case "equals": {

0 commit comments

Comments
 (0)