Fix SimpleBinaryBufferedReaderFactory for longer endings

Resolves #811
This commit is contained in:
Henning Poettker
2023-12-28 02:12:11 +01:00
committed by Mahmoud Ben Hassine
parent 0640f9fef8
commit 2e9d96bc24
2 changed files with 27 additions and 15 deletions

View File

@@ -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.
@@ -123,25 +123,24 @@ public class SimpleBinaryBufferedReaderFactory implements BufferedReaderFactory
}
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;
}

View File

@@ -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.
@@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertNull;
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;
/**
@@ -75,16 +77,27 @@ class SimpleBinaryBufferedReaderFactoryTests {
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();