Skip to content

Commit 929a551

Browse files
hpoettkerfmbenhassine
authored andcommitted
Fix SimpleBinaryBufferedReaderFactory for longer endings
Resolves #811
1 parent 5aa4deb commit 929a551

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/SimpleBinaryBufferedReaderFactory.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -114,25 +114,24 @@ private boolean isEndOfLine(StringBuilder buffer, StringBuilder candidate, int n
114114
}
115115

116116
char c = (char) next;
117-
if (ending.charAt(0) == c || candidate.length() > 0) {
117+
if (ending.charAt(0) == c || !candidate.isEmpty()) {
118118
candidate.append(c);
119119
}
120-
121-
if (candidate.length() == 0) {
120+
else {
122121
buffer.append(c);
123122
return false;
124123
}
125124

126-
boolean end = ending.equals(candidate.toString());
127-
if (end) {
125+
if (ending.contentEquals(candidate)) {
128126
candidate.delete(0, candidate.length());
127+
return true;
129128
}
130-
else if (candidate.length() >= ending.length()) {
131-
buffer.append(candidate);
132-
candidate.delete(0, candidate.length());
129+
while (!ending.startsWith(candidate.toString())) {
130+
buffer.append(candidate.charAt(0));
131+
candidate.delete(0, 1);
133132
}
134133

135-
return end;
134+
return false;
136135

137136
}
138137

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/SimpleBinaryBufferedReaderFactoryTests.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121
import java.io.BufferedReader;
2222

2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
2426
import org.springframework.core.io.ByteArrayResource;
2527

2628
/**
@@ -75,16 +77,27 @@ void testCreateWithLineEndingAtEnd() throws Exception {
7577
assertNull(reader.readLine());
7678
}
7779

78-
@Test
79-
void testCreateWithFalseLineEnding() throws Exception {
80+
@ParameterizedTest
81+
@ValueSource(strings = { "||", "|||" })
82+
void testCreateWithFalseLineEnding(String lineEnding) throws Exception {
8083
SimpleBinaryBufferedReaderFactory factory = new SimpleBinaryBufferedReaderFactory();
81-
factory.setLineEnding("||");
84+
factory.setLineEnding(lineEnding);
8285
@SuppressWarnings("resource")
83-
BufferedReader reader = factory.create(new ByteArrayResource("a|b||".getBytes()), "UTF-8");
86+
BufferedReader reader = factory.create(new ByteArrayResource(("a|b" + lineEnding).getBytes()), "UTF-8");
8487
assertEquals("a|b", reader.readLine());
8588
assertNull(reader.readLine());
8689
}
8790

91+
@Test
92+
void testCreateWithFalseMixedCharacterLineEnding() throws Exception {
93+
SimpleBinaryBufferedReaderFactory factory = new SimpleBinaryBufferedReaderFactory();
94+
factory.setLineEnding("#@");
95+
@SuppressWarnings("resource")
96+
BufferedReader reader = factory.create(new ByteArrayResource(("a##@").getBytes()), "UTF-8");
97+
assertEquals("a#", reader.readLine());
98+
assertNull(reader.readLine());
99+
}
100+
88101
@Test
89102
void testCreateWithIncompleteLineEnding() throws Exception {
90103
SimpleBinaryBufferedReaderFactory factory = new SimpleBinaryBufferedReaderFactory();

0 commit comments

Comments
 (0)