Skip to content

Commit d357085

Browse files
committed
Fix DefaultDataBuffer#getNativeBuffer() to set correct limit
Closes gh-30967
1 parent 2593b60 commit d357085

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Arjen Poutsma
3838
* @author Juergen Hoeller
3939
* @author Brian Clozel
40+
* @author Injae Kim
4041
* @since 5.0
4142
* @see DefaultDataBufferFactory
4243
*/
@@ -81,12 +82,12 @@ static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBuffer
8182
/**
8283
* Directly exposes the native {@code ByteBuffer} that this buffer is based
8384
* on also updating the {@code ByteBuffer's} position and limit to match
84-
* the current {@link #readPosition()} and {@link #readableByteCount()}.
85+
* the current {@link #readPosition()} and {@link #writePosition()}.
8586
* @return the wrapped byte buffer
8687
*/
8788
public ByteBuffer getNativeBuffer() {
8889
this.byteBuffer.position(this.readPosition);
89-
this.byteBuffer.limit(readableByteCount());
90+
this.byteBuffer.limit(this.writePosition);
9091
return this.byteBuffer;
9192
}
9293

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 buffer = bufferFactory.allocateBuffer(256);
40+
buffer.write("0123456789", StandardCharsets.UTF_8);
41+
42+
byte[] result = new byte[7];
43+
buffer.read(result);
44+
assertThat(result).isEqualTo("0123456".getBytes(StandardCharsets.UTF_8));
45+
46+
ByteBuffer nativeBuffer = buffer.getNativeBuffer();
47+
assertThat(nativeBuffer.position()).isEqualTo(7);
48+
assertThat(buffer.readPosition()).isEqualTo(7);
49+
assertThat(nativeBuffer.limit()).isEqualTo(10);
50+
assertThat(buffer.writePosition()).isEqualTo(10);
51+
52+
release(buffer);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)