Fix repeated calls to DataBuffer.write(CharSequence, Charset)

Prior to this commit, repeated calls to `DataBuffer.write(CharSequence,
Charset)` would not write the given chars to the expected position in
the original buffer.

This commit fixes that by writing to the `outBuffer.position()`, offset
by the current `DataBuffer.writePosition()`.

Fixes gh-22484
master
Andrew Tulloch 6 years ago committed by Brian Clozel
parent 2137cc4422
commit 090aceb3ad
  1. 4
      spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java
  2. 21
      spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

@ -264,13 +264,13 @@ public interface DataBuffer {
break;
}
if (cr.isOverflow()) {
writePosition(outBuffer.position());
writePosition(writePosition() + outBuffer.position());
int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());
ensureCapacity(maximumSize);
outBuffer = asByteBuffer(writePosition(), writableByteCount());
}
}
writePosition(outBuffer.position());
writePosition(writePosition() + outBuffer.position());
}
return this;
}

@ -225,6 +225,27 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void writeMultipleUtf8String() {
DataBuffer buffer = createDataBuffer(1);
buffer.write("abc", StandardCharsets.UTF_8);
assertEquals(3, buffer.readableByteCount());
buffer.write("def", StandardCharsets.UTF_8);
assertEquals(6, buffer.readableByteCount());
buffer.write("ghi", StandardCharsets.UTF_8);
assertEquals(9, buffer.readableByteCount());
byte[] result = new byte[9];
buffer.read(result);
assertArrayEquals("abcdefghi".getBytes(), result);
release(buffer);
}
@Test
public void inputStream() throws IOException {
DataBuffer buffer = createDataBuffer(4);

Loading…
Cancel
Save