1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.io.channels;
19
20 import java.io.Closeable;
21 import java.lang.reflect.Proxy;
22 import java.nio.channels.AsynchronousChannel;
23 import java.nio.channels.ByteChannel;
24 import java.nio.channels.Channel;
25 import java.nio.channels.GatheringByteChannel;
26 import java.nio.channels.InterruptibleChannel;
27 import java.nio.channels.NetworkChannel;
28 import java.nio.channels.ReadableByteChannel;
29 import java.nio.channels.ScatteringByteChannel;
30 import java.nio.channels.SeekableByteChannel;
31 import java.nio.channels.WritableByteChannel;
32 import java.util.LinkedHashSet;
33 import java.util.Objects;
34 import java.util.Set;
35
36 /**
37 * Creates a close-shielding proxy for a {@link Channel}.
38 *
39 * <p>
40 * The returned proxy implements all {@link Channel} sub-interfaces that are both supported by this implementation and actually implemented by the given
41 * delegate.
42 * </p>
43 * <p>
44 * The following interfaces are supported:
45 * </p>
46 * <ul>
47 * <li>{@link AsynchronousChannel}</li>
48 * <li>{@link ByteChannel}</li>
49 * <li>{@link Channel}</li>
50 * <li>{@link GatheringByteChannel}</li>
51 * <li>{@link InterruptibleChannel}</li>
52 * <li>{@link NetworkChannel}</li>
53 * <li>{@link ReadableByteChannel}</li>
54 * <li>{@link ScatteringByteChannel}</li>
55 * <li>{@link SeekableByteChannel}</li>
56 * <li>{@link WritableByteChannel}</li>
57 * </ul>
58 *
59 * @see Channel
60 * @see Closeable
61 * @since 2.21.0
62 */
63 public final class CloseShieldChannel {
64
65 private static final Class<?>[] EMPTY = {};
66
67 private static Set<Class<?>> collectChannelInterfaces(final Class<?> type, final Set<Class<?>> out) {
68 Class<?> currentType = type;
69 // Visit interfaces
70 while (currentType != null) {
71 for (final Class<?> iface : currentType.getInterfaces()) {
72 if (CloseShieldChannelHandler.isSupported(iface) && out.add(iface)) {
73 collectChannelInterfaces(iface, out);
74 }
75 }
76 currentType = currentType.getSuperclass();
77 }
78 return out;
79 }
80
81 /**
82 * Wraps a channel to shield it from being closed.
83 *
84 * @param channel The underlying channel to shield, not {@code null}.
85 * @param <T> A supported channel type.
86 * @return A proxy that shields {@code close()} and enforces closed semantics on other calls.
87 * @throws ClassCastException if {@code T} is not a supported channel type.
88 * @throws NullPointerException if {@code channel} is {@code null}.
89 */
90 @SuppressWarnings({ "unchecked", "resource" }) // caller closes
91 public static <T extends Channel> T wrap(final T channel) {
92 Objects.requireNonNull(channel, "channel");
93 // Fast path: already our shield
94 if (Proxy.isProxyClass(channel.getClass()) && Proxy.getInvocationHandler(channel) instanceof CloseShieldChannelHandler) {
95 return channel;
96 }
97 // Collect only Channel sub-interfaces.
98 final Set<Class<?>> set = collectChannelInterfaces(channel.getClass(), new LinkedHashSet<>());
99 // fallback to root surface
100 return (T) Proxy.newProxyInstance(channel.getClass().getClassLoader(), // use delegate's loader
101 set.isEmpty() ? new Class<?>[] { Channel.class } : set.toArray(EMPTY), new CloseShieldChannelHandler(channel));
102 }
103
104 private CloseShieldChannel() {
105 // no instance
106 }
107 }