Skip to content

Commit 3d1b9dc

Browse files
authored
Feature/channel filters (#837)
* [IO-856] Try test on all OSs for GitHub CI * Add channel filters * Rename test class * Javadoc * Add FilterFileChannel
1 parent 8b94709 commit 3d1b9dc

15 files changed

Lines changed: 1739 additions & 8 deletions
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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
* Constructs a new instance.
97+
*
98+
* @param builder The source builder.
99+
* @throws IOException if an I/O error occurs.
100+
*/
101+
@SuppressWarnings("unchecked")
102+
FilterChannel(final AbstractBuilder<?, ?, ?> builder) throws IOException {
103+
channel = (C) builder.getChannel(Channel.class);
104+
}
105+
106+
@Override
107+
public void close() throws IOException {
108+
channel.close();
109+
}
110+
111+
@Override
112+
public boolean isOpen() {
113+
return channel.isOpen();
114+
}
115+
116+
/**
117+
* Unwraps this instance by returning the underlying {@link Channel} of type {@code C}.
118+
* <p>
119+
* Use with caution.
120+
* </p>
121+
*
122+
* @return the underlying channel of type {@code C}.
123+
*/
124+
public C unwrap() {
125+
return channel;
126+
}
127+
}

src/test/java/org/apache/commons/io/channels/FileChannelProxy.java renamed to src/main/java/org/apache/commons/io/channels/FilterFileChannel.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@
2424
import java.nio.channels.FileLock;
2525
import java.nio.channels.ReadableByteChannel;
2626
import java.nio.channels.WritableByteChannel;
27+
import java.util.Objects;
2728

2829
/**
29-
* Proxies a FileChannel.
30+
* Filters a {@link FileChannel}.
31+
*
32+
* @since 2.22.0
3033
*/
31-
class FileChannelProxy extends FileChannel {
34+
public class FilterFileChannel extends FileChannel {
3235

33-
FileChannel fileChannel;
36+
final FileChannel fileChannel;
3437

35-
FileChannelProxy(final FileChannel fileChannel) {
36-
this.fileChannel = fileChannel;
38+
FilterFileChannel(final FileChannel fileChannel) {
39+
this.fileChannel = Objects.requireNonNull(fileChannel, "fileChannel");
3740
}
3841

3942
@Override
@@ -121,6 +124,18 @@ public FileLock tryLock(final long position, final long size, final boolean shar
121124
return fileChannel.tryLock(position, size, shared);
122125
}
123126

127+
/**
128+
* Unwraps this instance by returning the underlying {@link FileChannel}.
129+
* <p>
130+
* Use with caution.
131+
* </p>
132+
*
133+
* @return the underlying {@link FileChannel}.
134+
*/
135+
public FileChannel unwrap() {
136+
return fileChannel;
137+
}
138+
124139
@Override
125140
public int write(final ByteBuffer src) throws IOException {
126141
return fileChannel.write(src);
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)