Skip to content

Commit 9121d65

Browse files
committed
[IO-639] ReversedLinesFileReader does not read first line if it's empty
- Add `@Disabled` and parameterized version of "testEmptyFile" from PR #590 by elharo - Add 8192 buffer size test fixture - Parameterize ReversedLinesFileReaderSimpleTest for block size - Internal renaming from from PR #590 by elharo
1 parent 30f8dc5 commit 9121d65

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public ReversedLinesFileReader get() throws IOException {
126126
}
127127

128128
private final class FilePart {
129-
private final long no;
129+
private final long partNumber;
130130

131131
private final byte[] data;
132132

@@ -137,19 +137,19 @@ private final class FilePart {
137137
/**
138138
* Constructs a new instance.
139139
*
140-
* @param no the part number
140+
* @param partNumber the part number
141141
* @param length its length
142142
* @param leftOverOfLastFilePart remainder
143143
* @throws IOException if there is a problem reading the file
144144
*/
145-
private FilePart(final long no, final int length, final byte[] leftOverOfLastFilePart) throws IOException {
146-
this.no = no;
145+
private FilePart(final long partNumber, final int length, final byte[] leftOverOfLastFilePart) throws IOException {
146+
this.partNumber = partNumber;
147147
final int dataLength = length + (leftOverOfLastFilePart != null ? leftOverOfLastFilePart.length : 0);
148148
this.data = new byte[dataLength];
149-
final long off = (no - 1) * blockSize;
149+
final long off = (partNumber - 1) * blockSize;
150150

151151
// read data
152-
if (no > 0 /* file not empty */) {
152+
if (partNumber > 0 /* file not empty */) {
153153
channel.position(off);
154154
final int countRead = channel.read(ByteBuffer.wrap(data, 0, length));
155155
if (countRead != length) {
@@ -209,7 +209,7 @@ private String readLine() { //NOPMD Bug in PMD
209209
String line = null;
210210
int newLineMatchByteCount;
211211

212-
final boolean isLastFilePart = no == 1;
212+
final boolean isLastFilePart = partNumber == 1;
213213

214214
int i = currentLastBytePos;
215215
while (i > -1) {
@@ -249,7 +249,7 @@ private String readLine() { //NOPMD Bug in PMD
249249

250250
// last file part handling
251251
if (isLastFilePart && leftOver != null) {
252-
// there will be no line break anymore, this is the first line of the file
252+
// there will be partNumber line break anymore, this is the first line of the file
253253
line = new String(leftOver, charset);
254254
leftOver = null;
255255
}
@@ -270,8 +270,8 @@ private FilePart rollOver() throws IOException {
270270
+ "last readLine() should have returned something! currentLastCharPos=" + currentLastBytePos);
271271
}
272272

273-
if (no > 1) {
274-
return new FilePart(no - 1, blockSize, leftOver);
273+
if (partNumber > 1) {
274+
return new FilePart(partNumber - 1, blockSize, leftOver);
275275
}
276276
// NO 1 was the last FilePart, we're finished
277277
if (leftOver != null) {
@@ -404,7 +404,7 @@ public ReversedLinesFileReader(final Path file, final int blockSize, final Chars
404404
final CharsetEncoder charsetEncoder = this.charset.newEncoder();
405405
final float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
406406
if (maxBytesPerChar == 1f || this.charset == StandardCharsets.UTF_8) {
407-
// all one byte encodings are no problem
407+
// all one byte encodings are partNumber problem
408408
byteDecrement = 1;
409409
} else if (this.charset == Charset.forName("Shift_JIS") || // Same as for UTF-8
410410
// http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
@@ -523,7 +523,7 @@ public String readLine() throws IOException {
523523
while (line == null) {
524524
currentFilePart = currentFilePart.rollOver();
525525
if (currentFilePart == null) {
526-
// no more FileParts: we're done, leave line set to null
526+
// partNumber more FileParts: we're done, leave line set to null
527527
break;
528528
}
529529
line = currentFilePart.readLine();

src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderParamBlockSizeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static void assertEqualsAndNoLineBreaks(final String expected, final String actu
8686
* Small and uneven block sizes are not used in reality but are good to show that the algorithm is solid.
8787
*/
8888
public static IntStream blockSizes() {
89-
return IntStream.of(1, 3, 8, 256, 4096);
89+
return IntStream.of(1, 3, 8, 10, 256, 4096, 8192);
9090
}
9191

9292
private ReversedLinesFileReader reversedLinesFileReader;

src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderSimpleTest.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,32 @@
3030

3131
import org.apache.commons.io.IOUtils;
3232
import org.apache.commons.io.TestResources;
33+
import org.junit.jupiter.api.Disabled;
3334
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.MethodSource;
3437

3538
public class ReversedLinesFileReaderSimpleTest {
3639

37-
@Test
38-
public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException, IOException {
39-
final int blockSize = 10;
40+
/*
41+
* Tests IO-639.
42+
*/
43+
@ParameterizedTest
44+
@MethodSource("org.apache.commons.io.input.ReversedLinesFileReaderParamBlockSizeTest#blockSizes")
45+
@Disabled
46+
public void testEmptyFirstLine(final int blockSize) throws Exception {
47+
final File testFileEmptyFirstLine = TestResources.getFile("/empty-first-line.bin");
48+
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFileEmptyFirstLine, 10, StandardCharsets.US_ASCII.name())) {
49+
assertEqualsAndNoLineBreaks("test2", reversedLinesFileReader.readLine());
50+
assertEqualsAndNoLineBreaks("", reversedLinesFileReader.readLine());
51+
assertEqualsAndNoLineBreaks("test1", reversedLinesFileReader.readLine());
52+
assertEqualsAndNoLineBreaks("", reversedLinesFileReader.readLine());
53+
}
54+
}
55+
56+
@ParameterizedTest
57+
@MethodSource("org.apache.commons.io.input.ReversedLinesFileReaderParamBlockSizeTest#blockSizes")
58+
public void testFileSizeIsExactMultipleOfBlockSize(final int blockSize) throws URISyntaxException, IOException {
4059
final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
4160
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
4261
StandardCharsets.ISO_8859_1.name())) {
@@ -45,9 +64,9 @@ public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException,
4564
}
4665
}
4766

48-
@Test
49-
public void testLineCount() throws URISyntaxException, IOException {
50-
final int blockSize = 10;
67+
@ParameterizedTest
68+
@MethodSource("org.apache.commons.io.input.ReversedLinesFileReaderParamBlockSizeTest#blockSizes")
69+
public void testLineCount(final int blockSize) throws URISyntaxException, IOException {
5170
final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
5271
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
5372
StandardCharsets.ISO_8859_1.name())) {
@@ -61,9 +80,9 @@ public void testLineCount() throws URISyntaxException, IOException {
6180
}
6281
}
6382

64-
@Test
65-
public void testToString() throws URISyntaxException, IOException {
66-
final int blockSize = 10;
83+
@ParameterizedTest
84+
@MethodSource("org.apache.commons.io.input.ReversedLinesFileReaderParamBlockSizeTest#blockSizes")
85+
public void testToString(final int blockSize) throws URISyntaxException, IOException {
6786
final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
6887
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
6988
StandardCharsets.ISO_8859_1.name())) {
@@ -89,5 +108,4 @@ public void testUnsupportedEncodingUTF16() throws URISyntaxException {
89108
assertThrows(UnsupportedEncodingException.class,
90109
() -> new ReversedLinesFileReader(testFileEmpty, IOUtils.DEFAULT_BUFFER_SIZE, StandardCharsets.UTF_16.name()).close());
91110
}
92-
93111
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
test1
3+
4+
test2

0 commit comments

Comments
 (0)