File tree 2 files changed +17
-13
lines changed
main/java/org/springframework/core/io/buffer
test/java/org/springframework/core/io/buffer
2 files changed +17
-13
lines changed Original file line number Diff line number Diff line change 37
37
* @author Arjen Poutsma
38
38
* @author Juergen Hoeller
39
39
* @author Brian Clozel
40
- * @author Injae Kim
41
40
* @since 5.0
42
41
* @see DefaultDataBufferFactory
43
42
*/
@@ -81,14 +80,16 @@ static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBuffer
81
80
82
81
/**
83
82
* 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}.
86
87
* @return the wrapped byte buffer
87
88
*/
88
89
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 ) ;
92
93
}
93
94
94
95
private void setNativeBuffer (ByteBuffer byteBuffer ) {
Original file line number Diff line number Diff line change @@ -36,20 +36,23 @@ class DefaultDataBufferTests {
36
36
37
37
@ Test // gh-30967
38
38
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 );
41
41
42
42
byte [] result = new byte [7 ];
43
- buffer .read (result );
43
+ dataBuffer .read (result );
44
44
assertThat (result ).isEqualTo ("0123456" .getBytes (StandardCharsets .UTF_8 ));
45
45
46
- ByteBuffer nativeBuffer = buffer .getNativeBuffer ();
46
+ ByteBuffer nativeBuffer = dataBuffer .getNativeBuffer ();
47
47
assertThat (nativeBuffer .position ()).isEqualTo (7 );
48
- assertThat (buffer .readPosition ()).isEqualTo (7 );
48
+ assertThat (dataBuffer .readPosition ()).isEqualTo (7 );
49
49
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 );
51
53
52
- release (buffer );
54
+
55
+ release (dataBuffer );
53
56
}
54
57
55
58
}
You can’t perform that action at this time.
0 commit comments