Skip to content

Commit 5331074

Browse files
committed
Reuse AbstractStreamBuilder.getChannel(Channel)
1 parent 6d3ce61 commit 5331074

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.nio.charset.Charset;
2626
import java.nio.charset.CharsetEncoder;
2727
import java.nio.charset.StandardCharsets;
28-
import java.nio.file.Files;
2928
import java.nio.file.Path;
3029
import java.nio.file.StandardOpenOption;
3130
import java.util.ArrayList;
@@ -93,6 +92,7 @@ public static class Builder extends AbstractStreamBuilder<ReversedLinesFileReade
9392
public Builder() {
9493
setBufferSizeDefault(DEFAULT_BLOCK_SIZE);
9594
setBufferSize(DEFAULT_BLOCK_SIZE);
95+
setOpenOptions(StandardOpenOption.READ);
9696
}
9797

9898
/**
@@ -311,42 +311,42 @@ private ReversedLinesFileReader(final Builder builder) throws IOException {
311311
this.blockSize = builder.getBufferSize();
312312
this.charset = Charsets.toCharset(builder.getCharset());
313313
// check & prepare encoding
314-
final CharsetEncoder charsetEncoder = this.charset.newEncoder();
314+
final CharsetEncoder charsetEncoder = charset.newEncoder();
315315
final float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
316-
if (maxBytesPerChar == 1f || this.charset == StandardCharsets.UTF_8) {
316+
if (maxBytesPerChar == 1f || charset == StandardCharsets.UTF_8) {
317317
// all one byte encodings are partNumber problem
318318
byteDecrement = 1;
319-
} else if (this.charset == Charset.forName("Shift_JIS") || // Same as for UTF-8
319+
} else if (charset == Charset.forName("Shift_JIS") || // Same as for UTF-8
320320
// http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
321-
this.charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
322-
this.charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
323-
this.charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
324-
this.charset == Charset.forName("x-windows-950")) { // Windows code page 950 (Traditional Chinese)
321+
charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
322+
charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
323+
charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
324+
charset == Charset.forName("x-windows-950")) { // Windows code page 950 (Traditional Chinese)
325325
byteDecrement = 1;
326-
} else if (this.charset == StandardCharsets.UTF_16BE || this.charset == StandardCharsets.UTF_16LE) {
326+
} else if (charset == StandardCharsets.UTF_16BE || charset == StandardCharsets.UTF_16LE) {
327327
// UTF-16 new line sequences are not allowed as second tuple of four byte
328328
// sequences,
329329
// however byte order has to be specified
330330
byteDecrement = 2;
331-
} else if (this.charset == StandardCharsets.UTF_16) {
331+
} else if (charset == StandardCharsets.UTF_16) {
332332
throw new UnsupportedEncodingException("For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)");
333333
} else {
334334
throw new UnsupportedEncodingException("Encoding " + charset + " is not supported yet (feel free to submit a patch)");
335335
}
336336
// NOTE: The new line sequences are matched in the order given, so it is
337337
// important that \r\n is BEFORE \n
338-
this.newLineSequences = new byte[][] { StandardLineSeparator.CRLF.getBytes(this.charset), StandardLineSeparator.LF.getBytes(this.charset),
339-
StandardLineSeparator.CR.getBytes(this.charset) };
338+
this.newLineSequences = new byte[][] { StandardLineSeparator.CRLF.getBytes(charset), StandardLineSeparator.LF.getBytes(charset),
339+
StandardLineSeparator.CR.getBytes(charset) };
340340
this.avoidNewlineSplitBufferSize = newLineSequences[0].length;
341341
// Open file
342-
this.channel = Files.newByteChannel(builder.getPath(), StandardOpenOption.READ);
342+
this.channel = builder.getChannel(SeekableByteChannel.class);
343343
this.totalByteLength = channel.size();
344-
int lastBlockLength = (int) (this.totalByteLength % blockSize);
344+
int lastBlockLength = (int) (totalByteLength % blockSize);
345345
if (lastBlockLength > 0) {
346-
this.totalBlockCount = this.totalByteLength / blockSize + 1;
346+
this.totalBlockCount = totalByteLength / blockSize + 1;
347347
} else {
348-
this.totalBlockCount = this.totalByteLength / blockSize;
349-
if (this.totalByteLength > 0) {
348+
this.totalBlockCount = totalByteLength / blockSize;
349+
if (totalByteLength > 0) {
350350
lastBlockLength = blockSize;
351351
}
352352
}

0 commit comments

Comments
 (0)