Skip to content

Commit 25569a2

Browse files
committed
Internal refactoring
1 parent 369a347 commit 25569a2

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.nio.channels.FileChannel;
2424
import java.nio.file.Path;
2525
import java.nio.file.StandardOpenOption;
26-
import java.util.Objects;
2726

2827
import org.apache.commons.io.IOUtils;
2928
import org.apache.commons.io.build.AbstractStreamBuilder;
@@ -106,8 +105,7 @@ public Builder() {
106105
*/
107106
@Override
108107
public BufferedFileChannelInputStream get() throws IOException {
109-
return fileChannel != null ? new BufferedFileChannelInputStream(fileChannel, getBufferSize())
110-
: new BufferedFileChannelInputStream(getPath(), getBufferSize());
108+
return new BufferedFileChannelInputStream(this);
111109
}
112110

113111
/**
@@ -141,6 +139,13 @@ public static Builder builder() {
141139

142140
private final FileChannel fileChannel;
143141

142+
@SuppressWarnings("resource")
143+
private BufferedFileChannelInputStream(final Builder builder) throws IOException {
144+
this.fileChannel = builder.fileChannel != null ? builder.fileChannel : FileChannel.open(builder.getPath(), StandardOpenOption.READ);
145+
this.byteBuffer = ByteBuffer.allocateDirect(builder.getBufferSize());
146+
this.byteBuffer.flip();
147+
}
148+
144149
/**
145150
* Constructs a new instance for the given File.
146151
*
@@ -166,12 +171,6 @@ public BufferedFileChannelInputStream(final File file, final int bufferSize) thr
166171
this(file.toPath(), bufferSize);
167172
}
168173

169-
private BufferedFileChannelInputStream(final FileChannel fileChannel, final int bufferSize) {
170-
this.fileChannel = Objects.requireNonNull(fileChannel, "path");
171-
byteBuffer = ByteBuffer.allocateDirect(bufferSize);
172-
byteBuffer.flip();
173-
}
174-
175174
/**
176175
* Constructs a new instance for the given Path.
177176
*
@@ -192,10 +191,9 @@ public BufferedFileChannelInputStream(final Path path) throws IOException {
192191
* @throws IOException If an I/O error occurs
193192
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
194193
*/
195-
@SuppressWarnings("resource")
196194
@Deprecated
197195
public BufferedFileChannelInputStream(final Path path, final int bufferSize) throws IOException {
198-
this(FileChannel.open(path, StandardOpenOption.READ), bufferSize);
196+
this(builder().setPath(path).setBufferSize(bufferSize));
199197
}
200198

201199
@Override

0 commit comments

Comments
 (0)