Skip to content

Commit c09b251

Browse files
committed
Ensure StringDecoder supports multiline delimiters
This commit makes sure the StringDecoder supports stripping off multi-line delimiters, such as \r\n. Specifically, we ensure that the delimiter is stripped from the joined buffer. Closes gh-26511
1 parent 6c22f7e commit c09b251

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ private Collection<DataBuffer> processDataBuffer(
157157
int startIndex = buffer.readPosition();
158158
int length = (endIndex - startIndex + 1);
159159
DataBuffer slice = buffer.retainedSlice(startIndex, length);
160-
if (this.stripDelimiter) {
161-
slice.writePosition(slice.writePosition() - matcher.delimiter().length);
162-
}
163160
result = (result != null ? result : new ArrayList<>());
164161
if (chunks.isEmpty()) {
162+
if (this.stripDelimiter) {
163+
slice.writePosition(slice.writePosition() - matcher.delimiter().length);
164+
}
165165
result.add(slice);
166166
}
167167
else {
168168
chunks.add(slice);
169-
result.add(buffer.factory().join(chunks));
169+
DataBuffer joined = buffer.factory().join(chunks);
170+
if (this.stripDelimiter) {
171+
joined.writePosition(joined.writePosition() - matcher.delimiter().length);
172+
}
173+
result.add(joined);
170174
chunks.clear();
171175
}
172176
buffer.readPosition(endIndex + 1);

spring-core/src/test/java/org/springframework/core/codec/StringDecoderTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,32 @@ void decodeNewLine() {
125125
);
126126

127127
testDecode(input, String.class, step -> step
128-
.expectNext("")
128+
.expectNext("").as("1st")
129129
.expectNext("abc")
130130
.expectNext("defghi")
131-
.expectNext("")
131+
.expectNext("").as("2nd")
132132
.expectNext("jklmno")
133133
.expectNext("pqr")
134134
.expectNext("stuvwxyz")
135135
.expectComplete()
136136
.verify());
137137
}
138138

139+
@Test
140+
void decodeNewlinesAcrossBuffers() {
141+
Flux<DataBuffer> input = Flux.just(
142+
stringBuffer("\r"),
143+
stringBuffer("\n"),
144+
stringBuffer("xyz")
145+
);
146+
147+
testDecode(input, String.class, step -> step
148+
.expectNext("")
149+
.expectNext("xyz")
150+
.expectComplete()
151+
.verify());
152+
}
153+
139154
@Test
140155
void maxInMemoryLimit() {
141156
Flux<DataBuffer> input = Flux.just(

0 commit comments

Comments
 (0)