Skip to content

Commit 01de46e

Browse files
authored
Update Javadoc and constructors in the channels package (#843)
* [IO-856] Try test on all OSs for GitHub CI * Javadoc - Make some package-private constructors private - Provide "default" constructors for simple use cases
1 parent f96d650 commit 01de46e

14 files changed

Lines changed: 191 additions & 17 deletions

src/main/java/org/apache/commons/io/channels/ByteArraySeekableByteChannel.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@ public static Builder builder() {
104104
/**
105105
* Constructs a new channel backed directly by the given byte array.
106106
*
107-
* <p>The channel initially contains the full contents of the array, with its
108-
* size set to {@code bytes.length} and its position set to {@code 0}.</p>
107+
* <p>
108+
* The channel initially contains the full contents of the array, with its size set to {@code bytes.length} and its position set to {@code 0}.
109+
* </p>
109110
*
110-
* <p>Reads and writes operate on the shared array.
111-
* If a write operation extends beyond the current capacity, the channel will
112-
* automatically allocate a larger backing array and copy the existing contents.</p>
111+
* <p>
112+
* Reads and writes operate on the shared array. If a write operation extends beyond the current capacity, the channel will automatically allocate a larger
113+
* backing array and copy the existing contents.
114+
* </p>
113115
*
114116
* @param bytes The byte array to wrap, must not be {@code null}
115117
* @return A new channel that uses the given array as its initial backing store.

src/main/java/org/apache/commons/io/channels/CloseShieldChannel.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
/**
3737
* Creates a close-shielding proxy for a {@link Channel}.
3838
*
39-
* <p>The returned proxy implements all {@link Channel} sub-interfaces that are both supported by this implementation and actually implemented by the given
40-
* delegate.</p>
41-
*
42-
* <p>The following interfaces are supported:</p>
43-
*
39+
* <p>
40+
* The returned proxy implements all {@link Channel} sub-interfaces that are both supported by this implementation and actually implemented by the given
41+
* delegate.
42+
* </p>
43+
* <p>
44+
* The following interfaces are supported:
45+
* </p>
4446
* <ul>
4547
* <li>{@link AsynchronousChannel}</li>
4648
* <li>{@link ByteChannel}</li>

src/main/java/org/apache/commons/io/channels/CloseShieldChannelHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
import java.util.Objects;
3838
import java.util.Set;
3939

40+
/**
41+
* An {@link InvocationHandler} supporting the implementation of {@link CloseShieldChannel}.
42+
*/
4043
final class CloseShieldChannelHandler implements InvocationHandler {
4144

4245
private static final Set<Class<? extends Channel>> SUPPORTED_INTERFACES;
@@ -154,7 +157,7 @@ private Object invokeObjectMethod(final Object proxy, final Method method, final
154157
return false;
155158
}
156159
default:
157-
// Not possible, all non-final Object methods are handled above
160+
// Not possible, all non-final Object methods are handled above.
158161
return null;
159162
}
160163
}

src/main/java/org/apache/commons/io/channels/FilterByteChannel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
/**
3333
* A {@link ByteChannel} filter which delegates to the wrapped {@link ByteChannel}.
34+
* <p>
35+
* A {@code FilterByteChannel} wraps some other channel, which it uses as its basic source of data, possibly transforming the data along the way or providing
36+
* additional functionality. The class {@code FilterByteChannel} itself simply overrides methods of {@code ByteChannel} with versions that pass all requests to
37+
* the wrapped channel. Subclasses of {@code FilterByteChannel} may of course override any methods declared or inherited by {@code FilterByteChannel}, and may
38+
* also provide additional fields and methods.
39+
* </p>
40+
* <p>
41+
* You construct s simple instance with the {@link FilterByteChannel#FilterByteChannel(ByteChannel) channel constructor} and more advanced instances through the
42+
* {@link Builder}.
43+
* </p>
3444
*
3545
* @param <C> the {@link ByteChannel} type.
3646
* @see FilterInputStream
@@ -94,6 +104,15 @@ public static Builder forByteChannel() {
94104
super(builder);
95105
}
96106

107+
/**
108+
* Constructs a new instance.
109+
*
110+
* @param byteChannel The channel to wrap.
111+
*/
112+
public FilterByteChannel(final C byteChannel) {
113+
super(byteChannel);
114+
}
115+
97116
@Override
98117
public int read(final ByteBuffer dst) throws IOException {
99118
return channel.read(dst);

src/main/java/org/apache/commons/io/channels/FilterChannel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
/**
3333
* A {@link Channel} filter which delegates to the wrapped {@link Channel}.
34+
* <p>
35+
* A {@code FilterChannel} wraps some other channel, which it uses as its basic source of data, possibly transforming the data along the way or providing
36+
* additional functionality. The class {@code FilterChannel} itself simply overrides methods of {@code Channel} with versions that pass all requests to the
37+
* wrapped channel. Subclasses of {@code FilterChannel} may of course override any methods declared or inherited by {@code FilterChannel}, and may also provide
38+
* additional fields and methods.
39+
* </p>
40+
* <p>
41+
* You construct s simple instance with the {@link FilterChannel#FilterChannel(Channel) channel constructor} and more advanced instances through the
42+
* {@link Builder}.
43+
* </p>
3444
*
3545
* @param <C> the {@link Channel} type.
3646
* @see FilterInputStream
@@ -103,6 +113,15 @@ public static Builder forChannel() {
103113
channel = (C) builder.getChannel(Channel.class);
104114
}
105115

116+
/**
117+
* Constructs a new instance.
118+
*
119+
* @param channel The channel to wrap.
120+
*/
121+
public FilterChannel(final C channel) {
122+
this.channel = channel;
123+
}
124+
106125
@Override
107126
public void close() throws IOException {
108127
channel.close();

src/main/java/org/apache/commons/io/channels/FilterFileChannel.java

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,89 @@
2020
import java.io.IOException;
2121
import java.nio.ByteBuffer;
2222
import java.nio.MappedByteBuffer;
23+
import java.nio.channels.Channel;
2324
import java.nio.channels.FileChannel;
2425
import java.nio.channels.FileLock;
2526
import java.nio.channels.ReadableByteChannel;
2627
import java.nio.channels.WritableByteChannel;
2728
import java.util.Objects;
2829

30+
import org.apache.commons.io.build.AbstractStreamBuilder;
31+
2932
/**
3033
* Filters a {@link FileChannel}.
34+
* <p>
35+
* A {@code FilterFileChannel} wraps some other channel, which it uses as its basic source of data, possibly transforming the data along the way or providing
36+
* additional functionality. The class {@code FilterFileChannel} itself simply overrides methods of {@code FileChannel} with versions that pass all requests to
37+
* the wrapped channel. Subclasses of {@code FilterFileChannel} may of course override any methods declared or inherited by {@code FilterFileChannel}, and may
38+
* also provide additional fields and methods.
39+
* </p>
40+
* <p>
41+
* You construct s simple instance with the {@link FilterFileChannel#FilterFileChannel(FileChannel) channel constructor} and more advanced instances through the
42+
* {@link Builder}.
43+
* </p>
3144
*
3245
* @since 2.22.0
3346
*/
3447
public class FilterFileChannel extends FileChannel {
3548

49+
/**
50+
* Builds instances of {@link FilterFileChannel} for subclasses.
51+
*
52+
* @param <F> The {@link FilterFileChannel} type.
53+
* @param <C> The {@link Channel} type wrapped by the FilterChannel.
54+
* @param <B> The builder type.
55+
*/
56+
public abstract static class AbstractBuilder<F extends FilterFileChannel, C extends FileChannel, B extends AbstractBuilder<F, C, B>>
57+
extends AbstractStreamBuilder<F, AbstractBuilder<F, C, B>> {
58+
59+
/**
60+
* Constructs instance for subclasses.
61+
*/
62+
protected AbstractBuilder() {
63+
// empty
64+
}
65+
}
66+
67+
/**
68+
* Builds instances of {@link FilterFileChannel}.
69+
*/
70+
public static class Builder extends AbstractBuilder<FilterFileChannel, FileChannel, Builder> {
71+
72+
/**
73+
* Builds instances of {@link FilterChannel}.
74+
*/
75+
protected Builder() {
76+
// empty
77+
}
78+
79+
@Override
80+
public FilterFileChannel get() throws IOException {
81+
return new FilterFileChannel(this);
82+
}
83+
}
84+
85+
/**
86+
* Creates a new {@link Builder}.
87+
*
88+
* @return a new {@link Builder}.
89+
*/
90+
public static Builder forFilterFileChannel() {
91+
return new Builder();
92+
}
93+
3694
final FileChannel fileChannel;
3795

38-
FilterFileChannel(final FileChannel fileChannel) {
96+
private FilterFileChannel(final Builder builder) throws IOException {
97+
this.fileChannel = builder.getChannel(FileChannel.class);
98+
}
99+
100+
/**
101+
* Constructs a new instance.
102+
*
103+
* @param fileChannel the file channel to wrap.
104+
*/
105+
public FilterFileChannel(final FileChannel fileChannel) {
39106
this.fileChannel = Objects.requireNonNull(fileChannel, "fileChannel");
40107
}
41108

src/main/java/org/apache/commons/io/channels/FilterReadableByteChannel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
/**
3333
* A {@link ReadableByteChannel} filter which delegates to the wrapped {@link ReadableByteChannel}.
34+
* <p>
35+
* A {@code FilterReadableByteChannel} 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 FilterReadableByteChannel} itself simply overrides methods of {@code ReadableByteChannel} with versions
37+
* that pass all requests to the wrapped channel. Subclasses of {@code FilterReadableByteChannel} may of course override any methods declared or inherited by
38+
* {@code FilterReadableByteChannel}, and may also provide additional fields and methods.
39+
* </p>
40+
* <p>
41+
* You construct s simple instance with the {@link FilterReadableByteChannel#FilterReadableByteChannel(ReadableByteChannel) channel constructor} and more
42+
* advanced instances through the {@link Builder}.
43+
* </p>
3444
*
3545
* @param <C> the {@link ReadableByteChannel} type.
3646
* @see FilterInputStream
@@ -94,6 +104,15 @@ public static Builder forReadableByteChannel() {
94104
super(builder);
95105
}
96106

107+
/**
108+
* Constructs a new instance.
109+
*
110+
* @param channel The channel to wrap.
111+
*/
112+
public FilterReadableByteChannel(final C channel) {
113+
super(channel);
114+
}
115+
97116
@Override
98117
public int read(final ByteBuffer dst) throws IOException {
99118
return channel.read(dst);

src/main/java/org/apache/commons/io/channels/FilterSeekableByteChannel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030

3131
/**
3232
* 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>
3343
*
3444
* @param <C> the {@link SeekableByteChannel} type.
3545
* @see FilterInputStream
@@ -93,6 +103,15 @@ public static Builder forSeekableByteChannel() {
93103
super(builder);
94104
}
95105

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+
96115
@Override
97116
public long position() throws IOException {
98117
return channel.position();

src/main/java/org/apache/commons/io/channels/FilterWritableByteChannel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
/**
3333
* 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>
3444
*
3545
* @param <C> the {@link WritableByteChannel} type.
3646
* @see FilterInputStream
@@ -94,6 +104,15 @@ public static Builder forWritableByteChannel() {
94104
super(builder);
95105
}
96106

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+
97116
@Override
98117
public int write(final ByteBuffer src) throws IOException {
99118
return channel.write(src);

src/test/java/org/apache/commons/io/channels/CloseShieldChannelFilterByteChannelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CloseShieldChannelFilterByteChannelTest {
5555
@BeforeEach
5656
void setUp() throws IOException {
5757
mockChannel = mock(ByteChannel.class);
58-
filterChannel = FilterByteChannel.forByteChannel().setChannel(mockChannel).get();
58+
filterChannel = new FilterByteChannel<>(mockChannel);
5959
shield = CloseShieldChannel.wrap(filterChannel);
6060
}
6161

0 commit comments

Comments
 (0)