You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Mashrur Mia (Jira)" <ji...@apache.org> on 2019/11/23 12:22:00 UTC

[jira] [Created] (IO-639) ReversedLinesFileReader does not read first line if it's empty

Mashrur Mia created IO-639:
------------------------------

             Summary: 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


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.3.4#803005)