You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2022/06/20 13:21:00 UTC

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

    [ https://issues.apache.org/jira/browse/IO-639?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17556373#comment-17556373 ] 

Gary D. Gregory commented on IO-639:
------------------------------------

This example does not compile. It is best to provide a PR on GitHub.

> 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.7#820007)