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.FilterInputStream;
21 import java.io.FilterOutputStream;
22 import java.io.FilterReader;
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.nio.channels.ByteChannel;
26
27 import org.apache.commons.io.input.ProxyInputStream;
28 import org.apache.commons.io.input.ProxyReader;
29 import org.apache.commons.io.output.ProxyOutputStream;
30 import org.apache.commons.io.output.ProxyWriter;
31
32 /**
33 * A {@link ByteChannel} filter which delegates to the wrapped {@link ByteChannel}.
34 * <p>
35 * A {@code FilterByteChannel} wraps some other channel, which it uses as its basic source of data, possibly transforming the data along the way or providing
36 * additional functionality. The class {@code FilterByteChannel} itself simply overrides methods of {@code ByteChannel} with versions that pass all requests to
37 * the wrapped channel. Subclasses of {@code FilterByteChannel} may of course override any methods declared or inherited by {@code FilterByteChannel}, and may
38 * also provide additional fields and methods.
39 * </p>
40 * <p>
41 * You construct s simple instance with the {@link FilterByteChannel#FilterByteChannel(ByteChannel) channel constructor} and more advanced instances through the
42 * {@link Builder}.
43 * </p>
44 *
45 * @param <C> the {@link ByteChannel} type.
46 * @see FilterInputStream
47 * @see FilterOutputStream
48 * @see FilterReader
49 * @see FilterWritableByteChannel
50 * @see ProxyInputStream
51 * @see ProxyOutputStream
52 * @see ProxyReader
53 * @see ProxyWriter
54 * @since 2.22.0
55 */
56 public class FilterByteChannel<C extends ByteChannel> extends FilterChannel<C> implements ByteChannel {
57
58 /**
59 * Builds instances of {@link FilterByteChannel} for subclasses.
60 *
61 * @param <F> The {@link FilterByteChannel} type.
62 * @param <C> The {@link ByteChannel} type wrapped by the FilterChannel.
63 * @param <B> The builder type.
64 */
65 public abstract static class AbstractBuilder<F extends FilterByteChannel<C>, C extends ByteChannel, B extends AbstractBuilder<F, C, B>>
66 extends FilterChannel.AbstractBuilder<F, C, B> {
67
68 /**
69 * Constructs a new builder for {@link FilterByteChannel}.
70 */
71 protected AbstractBuilder() {
72 // empty
73 }
74 }
75
76 /**
77 * Builds instances of {@link FilterByteChannel}.
78 */
79 public static class Builder extends AbstractBuilder<FilterByteChannel<ByteChannel>, ByteChannel, Builder> {
80
81 /**
82 * Builds instances of {@link FilterByteChannel}.
83 */
84 protected Builder() {
85 // empty
86 }
87
88 @Override
89 public FilterByteChannel<ByteChannel> get() throws IOException {
90 return new FilterByteChannel<>(this);
91 }
92 }
93
94 /**
95 * Creates a new {@link Builder}.
96 *
97 * @return a new {@link Builder}.
98 */
99 public static Builder forByteChannel() {
100 return new Builder();
101 }
102
103 FilterByteChannel(final AbstractBuilder<?, ?, ?> builder) throws IOException {
104 super(builder);
105 }
106
107 /**
108 * Constructs a new instance.
109 *
110 * @param byteChannel The channel to wrap.
111 */
112 public FilterByteChannel(final C byteChannel) {
113 super(byteChannel);
114 }
115
116 @Override
117 public int read(final ByteBuffer dst) throws IOException {
118 return channel.read(dst);
119 }
120
121 @Override
122 public int write(final ByteBuffer src) throws IOException {
123 return channel.write(src);
124 }
125 }