[ https://issues.apache.org/jira/browse/IO-639?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17941414#comment-17941414 ]
Elliotte Rusty Harold commented on IO-639: ------------------------------------------ I'm not sure basing ReverseInputStream on RandomAccessFile will work, but as long as it's a package private implementation detail, whatever works and passes the tests is fine. > ReversedLinesFileReader does not read first line if it's empty > -------------------------------------------------------------- > > Key: IO-639 > URL: https://issues.apache.org/jira/browse/IO-639 > Project: Commons IO > Issue Type: Bug > Affects Versions: 2.6 > Reporter: Mashrur Mia > Priority: Minor > > ReversedLinesFileReader does not seem to read the first if the the line is > empty. Consider the content of a file: > {code} > \n > test\n > {code} > where the first line is simple a newline character. If > ReversedLinesFileReader is used to read the file in reverse, then only the > 2nd line is read - in the subsequent call, {{::readLine}} return null. > Here is a Java test that was tried: > {code:java} > class ReversedFileReaderTest { > @ParameterizedTest > @MethodSource("contentAndExpectedProvider") > void testReadLineInReverse_givenTwoLines(String content, List<String> > expected) > throws IOException { > File file = Files.newTemporaryFile(); > java.nio.file.Files.write(Path.of(file.getPath()), > content.getBytes()); > List<String> lines = new ArrayList<>(); > try (ReversedLinesFileReader fileReader = new > ReversedLinesFileReader(file, > StandardCharsets.UTF_8)) { > String line; > while ((line = fileReader.readLine()) != null) { > lines.add(line); > } > } > assertThat(lines).isEqualTo(expected); > } > static Stream<Arguments> contentAndExpectedProvider() { > return Stream.of( > arguments("the\ntest\n", Arrays.asList("test", "the")), > arguments("\ntest\n", Arrays.asList("test", "")), > arguments("\n\ntest\n", Arrays.asList("test", "", "")), > arguments("\n\n", Arrays.asList("", "")), > arguments("\n", Arrays.asList("")) > ); > } > } > {code} > Only the first test case runs. All the last four fails -- This message was sent by Atlassian Jira (v8.20.10#820010)