|
25 | 25 | import java.nio.charset.Charset; |
26 | 26 | import java.nio.charset.CharsetEncoder; |
27 | 27 | import java.nio.charset.StandardCharsets; |
28 | | -import java.nio.file.Files; |
29 | 28 | import java.nio.file.Path; |
30 | 29 | import java.nio.file.StandardOpenOption; |
31 | 30 | import java.util.ArrayList; |
@@ -93,6 +92,7 @@ public static class Builder extends AbstractStreamBuilder<ReversedLinesFileReade |
93 | 92 | public Builder() { |
94 | 93 | setBufferSizeDefault(DEFAULT_BLOCK_SIZE); |
95 | 94 | setBufferSize(DEFAULT_BLOCK_SIZE); |
| 95 | + setOpenOptions(StandardOpenOption.READ); |
96 | 96 | } |
97 | 97 |
|
98 | 98 | /** |
@@ -311,42 +311,42 @@ private ReversedLinesFileReader(final Builder builder) throws IOException { |
311 | 311 | this.blockSize = builder.getBufferSize(); |
312 | 312 | this.charset = Charsets.toCharset(builder.getCharset()); |
313 | 313 | // check & prepare encoding |
314 | | - final CharsetEncoder charsetEncoder = this.charset.newEncoder(); |
| 314 | + final CharsetEncoder charsetEncoder = charset.newEncoder(); |
315 | 315 | final float maxBytesPerChar = charsetEncoder.maxBytesPerChar(); |
316 | | - if (maxBytesPerChar == 1f || this.charset == StandardCharsets.UTF_8) { |
| 316 | + if (maxBytesPerChar == 1f || charset == StandardCharsets.UTF_8) { |
317 | 317 | // all one byte encodings are partNumber problem |
318 | 318 | 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 |
320 | 320 | // 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) |
325 | 325 | 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) { |
327 | 327 | // UTF-16 new line sequences are not allowed as second tuple of four byte |
328 | 328 | // sequences, |
329 | 329 | // however byte order has to be specified |
330 | 330 | byteDecrement = 2; |
331 | | - } else if (this.charset == StandardCharsets.UTF_16) { |
| 331 | + } else if (charset == StandardCharsets.UTF_16) { |
332 | 332 | throw new UnsupportedEncodingException("For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)"); |
333 | 333 | } else { |
334 | 334 | throw new UnsupportedEncodingException("Encoding " + charset + " is not supported yet (feel free to submit a patch)"); |
335 | 335 | } |
336 | 336 | // NOTE: The new line sequences are matched in the order given, so it is |
337 | 337 | // 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) }; |
340 | 340 | this.avoidNewlineSplitBufferSize = newLineSequences[0].length; |
341 | 341 | // Open file |
342 | | - this.channel = Files.newByteChannel(builder.getPath(), StandardOpenOption.READ); |
| 342 | + this.channel = builder.getChannel(SeekableByteChannel.class); |
343 | 343 | this.totalByteLength = channel.size(); |
344 | | - int lastBlockLength = (int) (this.totalByteLength % blockSize); |
| 344 | + int lastBlockLength = (int) (totalByteLength % blockSize); |
345 | 345 | if (lastBlockLength > 0) { |
346 | | - this.totalBlockCount = this.totalByteLength / blockSize + 1; |
| 346 | + this.totalBlockCount = totalByteLength / blockSize + 1; |
347 | 347 | } else { |
348 | | - this.totalBlockCount = this.totalByteLength / blockSize; |
349 | | - if (this.totalByteLength > 0) { |
| 348 | + this.totalBlockCount = totalByteLength / blockSize; |
| 349 | + if (totalByteLength > 0) { |
350 | 350 | lastBlockLength = blockSize; |
351 | 351 | } |
352 | 352 | } |
|
0 commit comments