Skip to content

Commit ceb7afd

Browse files
committed
Merge pull request #32009 from injae-kim:fix-defaultDataBuffer-getNativeBuffer
* gh-32009: Polishing external contribution Set correct limit in DefaultDataBuffer::getNativeBuffer
2 parents a8fb16b + 70004e9 commit ceb7afd

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBuffer
8080

8181
/**
8282
* Directly exposes the native {@code ByteBuffer} that this buffer is based
83-
* on also updating the {@code ByteBuffer's} position and limit to match
84-
* the current {@link #readPosition()} and {@link #readableByteCount()}.
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}.
8587
* @return the wrapped byte buffer
8688
*/
8789
public ByteBuffer getNativeBuffer() {
88-
this.byteBuffer.position(this.readPosition);
89-
this.byteBuffer.limit(readableByteCount());
90-
return this.byteBuffer;
90+
return this.byteBuffer.duplicate()
91+
.position(this.readPosition)
92+
.limit(this.writePosition);
9193
}
9294

9395
private void setNativeBuffer(ByteBuffer byteBuffer) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.io.buffer;
18+
19+
import java.nio.ByteBuffer;
20+
import java.nio.charset.StandardCharsets;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.springframework.core.io.buffer.DataBufferUtils.release;
26+
27+
/**
28+
* Tests for {@link DefaultDataBuffer}.
29+
*
30+
* @author Injae Kim
31+
* @since 6.2
32+
*/
33+
class DefaultDataBufferTests {
34+
35+
private final DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
36+
37+
@Test // gh-30967
38+
void getNativeBuffer() {
39+
DefaultDataBuffer dataBuffer = this.bufferFactory.allocateBuffer(256);
40+
dataBuffer.write("0123456789", StandardCharsets.UTF_8);
41+
42+
byte[] result = new byte[7];
43+
dataBuffer.read(result);
44+
assertThat(result).isEqualTo("0123456".getBytes(StandardCharsets.UTF_8));
45+
46+
ByteBuffer nativeBuffer = dataBuffer.getNativeBuffer();
47+
assertThat(nativeBuffer.position()).isEqualTo(7);
48+
assertThat(dataBuffer.readPosition()).isEqualTo(7);
49+
assertThat(nativeBuffer.limit()).isEqualTo(10);
50+
assertThat(dataBuffer.writePosition()).isEqualTo(10);
51+
assertThat(nativeBuffer.capacity()).isEqualTo(256);
52+
assertThat(dataBuffer.capacity()).isEqualTo(256);
53+
54+
55+
release(dataBuffer);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)