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.WritableByteChannel;
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 WritableByteChannel} filter which delegates to the wrapped {@link WritableByteChannel}.
34 * <p>
35 * A {@code FilterWritableByteChannel} wraps some other channel, which it uses as its basic source of data, possibly transforming the data along the way or
36 * providing additional functionality. The class {@code FilterWritableByteChannel} itself simply overrides methods of {@code WritableByteChannel} with versions
37 * that pass all requests to the wrapped channel. Subclasses of {@code FilterWritableByteChannel} may of course override any methods declared or inherited by
38 * {@code WritableByteChannel}, and may also provide additional fields and methods.
39 * </p>
40 * <p>
41 * You construct s simple instance with the {@link FilterWritableByteChannel#FilterWritableByteChannel(WritableByteChannel) Channel constructor} and more
42 * advanced instances through the {@link Builder}.
43 * </p>
44 *
45 * @param <C> the {@link WritableByteChannel} 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 FilterWritableByteChannel<C extends WritableByteChannel> extends FilterChannel<C> implements WritableByteChannel {
57
58 /**
59 * Builds instances of {@link FilterWritableByteChannel} for subclasses.
60 *
61 * @param <F> The {@link FilterWritableByteChannel} type.
62 * @param <C> The {@link WritableByteChannel} type wrapped by the FilterChannel.
63 * @param <B> The builder type.
64 */
65 public abstract static class AbstractBuilder<F extends FilterWritableByteChannel<C>, C extends WritableByteChannel, B extends AbstractBuilder<F, C, B>>
66 extends FilterChannel.AbstractBuilder<F, C, B> {
67
68 /**
69 * Constructs a new builder for {@link FilterWritableByteChannel}.
70 */
71 public AbstractBuilder() {
72 // empty
73 }
74 }
75
76 /**
77 * Builds instances of {@link FilterByteChannel}.
78 */
79 public static class Builder extends AbstractBuilder<FilterWritableByteChannel<WritableByteChannel>, WritableByteChannel, Builder> {
80
81 /**
82 * Builds instances of {@link FilterByteChannel}.
83 */
84 protected Builder() {
85 // empty
86 }
87
88 @Override
89 public FilterWritableByteChannel<WritableByteChannel> get() throws IOException {
90 return new FilterWritableByteChannel<>(this);
91 }
92 }
93
94 /**
95 * Creates a new {@link Builder}.
96 *
97 * @return a new {@link Builder}.
98 */
99 public static Builder forWritableByteChannel() {
100 return new Builder();
101 }
102
103 FilterWritableByteChannel(final Builder builder) throws IOException {
104 super(builder);
105 }
106
107 /**
108 * Constructs a new instance.
109 *
110 * @param channel The channel to wrap.
111 */
112 public FilterWritableByteChannel(final C channel) {
113 super(channel);
114 }
115
116 @Override
117 public int write(final ByteBuffer src) throws IOException {
118 return channel.write(src);
119 }
120 }