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

Elliotte Rusty Harold commented on IO-639:
------------------------------------------

slightly simpler case that repros this is a file that contains a line break and 
nothing else:


{noformat}
    @Test
    public void testLineBreakOnly() throws IOException, URISyntaxException {
        File file = File.createTempFile("foo", "txt");
        Files.write(file.toPath(), "\n".getBytes(StandardCharsets.ISO_8859_1));
        try (ReversedLinesFileReader reversedLinesFileReader = new 
ReversedLinesFileReader(file, BLOCK_SIZE,
                StandardCharsets.ISO_8859_1.name())) {
            assertEqualsAndNoLineBreaks("", reversedLinesFileReader.readLine());
        } finally {
            file.delete();
        }
    }
{noformat}




> 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)

Reply via email to