Skip to content

Commit 500ecec

Browse files
committed
Add channel filters
1 parent cd3271f commit 500ecec

File tree

10 files changed

+1264
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)