Skip to content

Commit 090aceb

Browse files
drewtulbclozel
authored andcommitted
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
1 parent 2137cc4 commit 090aceb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ default DataBuffer write(CharSequence charSequence, Charset charset) {
264264
break;
265265
}
266266
if (cr.isOverflow()) {
267-
writePosition(outBuffer.position());
267+
writePosition(writePosition() + outBuffer.position());
268268
int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());
269269
ensureCapacity(maximumSize);
270270
outBuffer = asByteBuffer(writePosition(), writableByteCount());
271271
}
272272
}
273-
writePosition(outBuffer.position());
273+
writePosition(writePosition() + outBuffer.position());
274274
}
275275
return this;
276276
}

spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,27 @@ public void writeIsoString() {
225225
release(buffer);
226226
}
227227

228+
@Test
229+
public void writeMultipleUtf8String() {
230+
231+
DataBuffer buffer = createDataBuffer(1);
232+
buffer.write("abc", StandardCharsets.UTF_8);
233+
assertEquals(3, buffer.readableByteCount());
234+
235+
buffer.write("def", StandardCharsets.UTF_8);
236+
assertEquals(6, buffer.readableByteCount());
237+
238+
buffer.write("ghi", StandardCharsets.UTF_8);
239+
assertEquals(9, buffer.readableByteCount());
240+
241+
byte[] result = new byte[9];
242+
buffer.read(result);
243+
244+
assertArrayEquals("abcdefghi".getBytes(), result);
245+
246+
release(buffer);
247+
}
248+
228249
@Test
229250
public void inputStream() throws IOException {
230251
DataBuffer buffer = createDataBuffer(4);

0 commit comments

Comments
 (0)