Skip to content

Commit 9b7fe50

Browse files
committed
Add channel filters
1 parent cd3271f commit 9b7fe50

File tree

10 files changed

+1269
-0
lines changed

10 files changed

+1269
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
*
35+
* @param <C> the {@link ByteChannel} type.
36+
* @see FilterInputStream
37+
* @see FilterOutputStream
38+
* @see FilterReader
39+
* @see FilterWritableByteChannel
40+
* @see ProxyInputStream
41+
* @see ProxyOutputStream
42+
* @see ProxyReader
43+
* @see ProxyWriter
44+
* @since 2.22.0
45+
*/
46+
public class FilterByteChannel<C extends ByteChannel> extends FilterChannel<C> implements ByteChannel {
47+
48+
/**
49+
* Builds instances of {@link FilterByteChannel} for subclasses.
50+
*
51+
* @param <F> The {@link FilterByteChannel} type.
52+
* @param <C> The {@link ByteChannel} type wrapped by the FilterChannel.
53+
* @param <B> The builder type.
54+
*/
55+
public abstract static class AbstractBuilder<F extends FilterByteChannel<C>, C extends ByteChannel, B extends AbstractBuilder<F, C, B>>
56+
extends FilterChannel.AbstractBuilder<F, C, B> {
57+
58+
/**
59+
* Constructs a new builder for {@link FilterByteChannel}.
60+
*/
61+
protected AbstractBuilder() {
62+
// empty
63+
}
64+
}
65+
66+
/**
67+
* Builds instances of {@link FilterByteChannel}.
68+
*/
69+
public static class Builder extends AbstractBuilder<FilterByteChannel<ByteChannel>, ByteChannel, Builder> {
70+
71+
/**
72+
* Builds instances of {@link FilterByteChannel}.
73+
*/
74+
protected Builder() {
75+
// empty
76+
}
77+
78+
@Override
79+
public FilterByteChannel<ByteChannel> get() throws IOException {
80+
return new FilterByteChannel<>(this);
81+
}
82+
}
83+
84+
/**
85+
* Creates a new {@link Builder}.
86+
*
87+
* @return a new {@link Builder}.
88+
*/
89+
public static Builder forByteChannel() {
90+
return new Builder();
91+
}
92+
93+
FilterByteChannel(final AbstractBuilder<?, ?, ?> builder) throws IOException {
94+
super(builder);
95+
}
96+
97+
@Override
98+
public int read(final ByteBuffer dst) throws IOException {
99+
return channel.read(dst);
100+
}
101+
102+
@Override
103+
public int write(final ByteBuffer src) throws IOException {
104+
return channel.write(src);
105+
}
106+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.Channel;
25+
26+
import org.apache.commons.io.build.AbstractStreamBuilder;
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 Channel} filter which delegates to the wrapped {@link Channel}.
34+
*
35+
* @param <C> the {@link Channel} type.
36+
* @see FilterInputStream
37+
* @see FilterOutputStream
38+
* @see FilterReader
39+
* @see FilterWritableByteChannel
40+
* @see ProxyInputStream
41+
* @see ProxyOutputStream
42+
* @see ProxyReader
43+
* @see ProxyWriter
44+
* @since 2.22.0
45+
*/
46+
public class FilterChannel<C extends Channel> implements Channel {
47+
48+
/**
49+
* Builds instances of {@link FilterChannel} for subclasses.
50+
*
51+
* @param <F> The {@link FilterChannel} type.
52+
* @param <C> The {@link Channel} type wrapped by the FilterChannel.
53+
* @param <B> The builder type.
54+
*/
55+
public abstract static class AbstractBuilder<F extends FilterChannel<C>, C extends Channel, B extends AbstractBuilder<F, C, B>>
56+
extends AbstractStreamBuilder<F, AbstractBuilder<F, C, B>> {
57+
58+
/**
59+
* Constructs instance for subclasses.
60+
*/
61+
protected AbstractBuilder() {
62+
// empty
63+
}
64+
}
65+
66+
/**
67+
* Builds instances of {@link FilterChannel}.
68+
*/
69+
public static class Builder extends AbstractBuilder<FilterChannel<Channel>, Channel, Builder> {
70+
71+
/**
72+
* Builds instances of {@link FilterChannel}.
73+
*/
74+
protected Builder() {
75+
// empty
76+
}
77+
78+
@Override
79+
public FilterChannel<Channel> get() throws IOException {
80+
return new FilterChannel<>(this);
81+
}
82+
}
83+
84+
/**
85+
* Creates a new {@link Builder}.
86+
*
87+
* @return a new {@link Builder}.
88+
*/
89+
public static Builder forChannel() {
90+
return new Builder();
91+
}
92+
93+
final C channel;
94+
95+
/**
96+
* @param builder
97+
* @throws IOException if an I/O error occurs.
98+
*/
99+
@SuppressWarnings("unchecked")
100+
FilterChannel(final AbstractBuilder<?, ?, ?> builder) throws IOException {
101+
channel = (C) builder.getChannel(Channel.class);
102+
}
103+
104+
@Override
105+
public void close() throws IOException {
106+
channel.close();
107+
}
108+
109+
@Override
110+
public boolean isOpen() {
111+
return channel.isOpen();
112+
}
113+
114+
/**
115+
* Unwraps this instance by returning the underlying {@link Channel} of type {@code C}.
116+
* <p>
117+
* Use with caution.
118+
* </p>
119+
*
120+
* @return the underlying channel of type {@code C}.
121+
*/
122+
public C unwrap() {
123+
return channel;
124+
}
125+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.ReadableByteChannel;
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 ReadableByteChannel} filter which delegates to the wrapped {@link ReadableByteChannel}.
34+
*
35+
* @param <C> the {@link ReadableByteChannel} type.
36+
* @see FilterInputStream
37+
* @see FilterOutputStream
38+
* @see FilterReader
39+
* @see FilterWritableByteChannel
40+
* @see ProxyInputStream
41+
* @see ProxyOutputStream
42+
* @see ProxyReader
43+
* @see ProxyWriter
44+
* @since 2.22.0
45+
*/
46+
public class FilterReadableByteChannel<C extends ReadableByteChannel> extends FilterChannel<C> implements ReadableByteChannel {
47+
48+
/**
49+
* Builds instances of {@link FilterReadableByteChannel} for subclasses.
50+
*
51+
* @param <F> The {@link FilterReadableByteChannel} type.
52+
* @param <C> The {@link ReadableByteChannel} type wrapped by the FilterChannel.
53+
* @param <B> The builder type.
54+
*/
55+
public abstract static class AbstractBuilder<F extends FilterReadableByteChannel<C>, C extends ReadableByteChannel, B extends AbstractBuilder<F, C, B>>
56+
extends FilterChannel.AbstractBuilder<F, C, B> {
57+
58+
/**
59+
* Constructs a new builder for {@link FilterReadableByteChannel}.
60+
*/
61+
public AbstractBuilder() {
62+
// empty
63+
}
64+
}
65+
66+
/**
67+
* Builds instances of {@link FilterByteChannel}.
68+
*/
69+
public static class Builder extends AbstractBuilder<FilterReadableByteChannel<ReadableByteChannel>, ReadableByteChannel, Builder> {
70+
71+
/**
72+
* Builds instances of {@link FilterByteChannel}.
73+
*/
74+
protected Builder() {
75+
// empty
76+
}
77+
78+
@Override
79+
public FilterReadableByteChannel<ReadableByteChannel> get() throws IOException {
80+
return new FilterReadableByteChannel<>(this);
81+
}
82+
}
83+
84+
/**
85+
* Creates a new {@link Builder}.
86+
*
87+
* @return a new {@link Builder}.
88+
*/
89+
public static Builder forReadableByteChannel() {
90+
return new Builder();
91+
}
92+
93+
FilterReadableByteChannel(final Builder builder) throws IOException {
94+
super(builder);
95+
}
96+
97+
@Override
98+
public int read(final ByteBuffer dst) throws IOException {
99+
return channel.read(dst);
100+
}
101+
}

0 commit comments

Comments
 (0)