Browse Source

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
pull/26558/head
Arjen Poutsma 5 years ago
parent
commit
c09b2513e0
  1. 12
      spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java
  2. 19
      spring-core/src/test/java/org/springframework/core/codec/StringDecoderTests.java

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

@ -157,16 +157,20 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> { @@ -157,16 +157,20 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {
int startIndex = buffer.readPosition();
int length = (endIndex - startIndex + 1);
DataBuffer slice = buffer.retainedSlice(startIndex, length);
if (this.stripDelimiter) {
slice.writePosition(slice.writePosition() - matcher.delimiter().length);
}
result = (result != null ? result : new ArrayList<>());
if (chunks.isEmpty()) {
if (this.stripDelimiter) {
slice.writePosition(slice.writePosition() - matcher.delimiter().length);
}
result.add(slice);
}
else {
chunks.add(slice);
result.add(buffer.factory().join(chunks));
DataBuffer joined = buffer.factory().join(chunks);
if (this.stripDelimiter) {
joined.writePosition(joined.writePosition() - matcher.delimiter().length);
}
result.add(joined);
chunks.clear();
}
buffer.readPosition(endIndex + 1);

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

@ -125,10 +125,10 @@ class StringDecoderTests extends AbstractDecoderTests<StringDecoder> { @@ -125,10 +125,10 @@ class StringDecoderTests extends AbstractDecoderTests<StringDecoder> {
);
testDecode(input, String.class, step -> step
.expectNext("")
.expectNext("").as("1st")
.expectNext("abc")
.expectNext("defghi")
.expectNext("")
.expectNext("").as("2nd")
.expectNext("jklmno")
.expectNext("pqr")
.expectNext("stuvwxyz")
@ -136,6 +136,21 @@ class StringDecoderTests extends AbstractDecoderTests<StringDecoder> { @@ -136,6 +136,21 @@ class StringDecoderTests extends AbstractDecoderTests<StringDecoder> {
.verify());
}
@Test
void decodeNewlinesAcrossBuffers() {
Flux<DataBuffer> input = Flux.just(
stringBuffer("\r"),
stringBuffer("\n"),
stringBuffer("xyz")
);
testDecode(input, String.class, step -> step
.expectNext("")
.expectNext("xyz")
.expectComplete()
.verify());
}
@Test
void maxInMemoryLimit() {
Flux<DataBuffer> input = Flux.just(

Loading…
Cancel
Save