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