Skip to content

Commit

Permalink
Fix SimpleBinaryBufferedReaderFactory for longer endings
Browse files Browse the repository at this point in the history
Resolves #811
  • Loading branch information
hpoettker authored and fmbenhassine committed Apr 23, 2024
1 parent 5aa4deb commit 929a551
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,25 +114,24 @@ private boolean isEndOfLine(StringBuilder buffer, StringBuilder candidate, int n
}

char c = (char) next;
if (ending.charAt(0) == c || candidate.length() > 0) {
if (ending.charAt(0) == c || !candidate.isEmpty()) {
candidate.append(c);
}

if (candidate.length() == 0) {
else {
buffer.append(c);
return false;
}

boolean end = ending.equals(candidate.toString());
if (end) {
if (ending.contentEquals(candidate)) {
candidate.delete(0, candidate.length());
return true;
}
else if (candidate.length() >= ending.length()) {
buffer.append(candidate);
candidate.delete(0, candidate.length());
while (!ending.startsWith(candidate.toString())) {
buffer.append(candidate.charAt(0));
candidate.delete(0, 1);
}

return end;
return false;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,8 @@
import java.io.BufferedReader;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.core.io.ByteArrayResource;

/**
Expand Down Expand Up @@ -75,16 +77,27 @@ void testCreateWithLineEndingAtEnd() throws Exception {
assertNull(reader.readLine());
}

@Test
void testCreateWithFalseLineEnding() throws Exception {
@ParameterizedTest
@ValueSource(strings = { "||", "|||" })
void testCreateWithFalseLineEnding(String lineEnding) throws Exception {
SimpleBinaryBufferedReaderFactory factory = new SimpleBinaryBufferedReaderFactory();
factory.setLineEnding("||");
factory.setLineEnding(lineEnding);
@SuppressWarnings("resource")
BufferedReader reader = factory.create(new ByteArrayResource("a|b||".getBytes()), "UTF-8");
BufferedReader reader = factory.create(new ByteArrayResource(("a|b" + lineEnding).getBytes()), "UTF-8");
assertEquals("a|b", reader.readLine());
assertNull(reader.readLine());
}

@Test
void testCreateWithFalseMixedCharacterLineEnding() throws Exception {
SimpleBinaryBufferedReaderFactory factory = new SimpleBinaryBufferedReaderFactory();
factory.setLineEnding("#@");
@SuppressWarnings("resource")
BufferedReader reader = factory.create(new ByteArrayResource(("a##@").getBytes()), "UTF-8");
assertEquals("a#", reader.readLine());
assertNull(reader.readLine());
}

@Test
void testCreateWithIncompleteLineEnding() throws Exception {
SimpleBinaryBufferedReaderFactory factory = new SimpleBinaryBufferedReaderFactory();
Expand Down

0 comments on commit 929a551

Please sign in to comment.