Skip to content

Commit 70004e9

Browse files
committed
Polishing external contribution
Change position and limit on duplicate, rather than source. See gh-30967 Closes gh-32009
1 parent 969d0bd commit 70004e9

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Diff for: spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
* @author Arjen Poutsma
3838
* @author Juergen Hoeller
3939
* @author Brian Clozel
40-
* @author Injae Kim
4140
* @since 5.0
4241
* @see DefaultDataBufferFactory
4342
*/
@@ -81,14 +80,16 @@ static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBuffer
8180

8281
/**
8382
* Directly exposes the native {@code ByteBuffer} that this buffer is based
84-
* on also updating the {@code ByteBuffer's} position and limit to match
85-
* the current {@link #readPosition()} and {@link #writePosition()}.
83+
* on. The {@linkplain ByteBuffer#position() position} of the returned
84+
* {@code ByteBuffer} is set to the {@linkplain #readPosition() read
85+
* position}, and the {@linkplain ByteBuffer#limit()} to the
86+
* {@linkplain #writePosition() write position}.
8687
* @return the wrapped byte buffer
8788
*/
8889
public ByteBuffer getNativeBuffer() {
89-
this.byteBuffer.position(this.readPosition);
90-
this.byteBuffer.limit(this.writePosition);
91-
return this.byteBuffer;
90+
return this.byteBuffer.duplicate()
91+
.position(this.readPosition)
92+
.limit(this.writePosition);
9293
}
9394

9495
private void setNativeBuffer(ByteBuffer byteBuffer) {

Diff for: spring-core/src/test/java/org/springframework/core/io/buffer/DefaultDataBufferTests.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,23 @@ class DefaultDataBufferTests {
3636

3737
@Test // gh-30967
3838
void getNativeBuffer() {
39-
DefaultDataBuffer buffer = bufferFactory.allocateBuffer(256);
40-
buffer.write("0123456789", StandardCharsets.UTF_8);
39+
DefaultDataBuffer dataBuffer = this.bufferFactory.allocateBuffer(256);
40+
dataBuffer.write("0123456789", StandardCharsets.UTF_8);
4141

4242
byte[] result = new byte[7];
43-
buffer.read(result);
43+
dataBuffer.read(result);
4444
assertThat(result).isEqualTo("0123456".getBytes(StandardCharsets.UTF_8));
4545

46-
ByteBuffer nativeBuffer = buffer.getNativeBuffer();
46+
ByteBuffer nativeBuffer = dataBuffer.getNativeBuffer();
4747
assertThat(nativeBuffer.position()).isEqualTo(7);
48-
assertThat(buffer.readPosition()).isEqualTo(7);
48+
assertThat(dataBuffer.readPosition()).isEqualTo(7);
4949
assertThat(nativeBuffer.limit()).isEqualTo(10);
50-
assertThat(buffer.writePosition()).isEqualTo(10);
50+
assertThat(dataBuffer.writePosition()).isEqualTo(10);
51+
assertThat(nativeBuffer.capacity()).isEqualTo(256);
52+
assertThat(dataBuffer.capacity()).isEqualTo(256);
5153

52-
release(buffer);
54+
55+
release(dataBuffer);
5356
}
5457

5558
}

0 commit comments

Comments
 (0)