Polishing external contribution
Change position and limit on duplicate, rather than source. See gh-30967 Closes gh-32009
This commit is contained in:
		
							parent
							
								
									969d0bd08b
								
							
						
					
					
						commit
						70004e9ad0
					
				| 
						 | 
					@ -37,7 +37,6 @@ import org.springframework.util.ObjectUtils;
 | 
				
			||||||
 * @author Arjen Poutsma
 | 
					 * @author Arjen Poutsma
 | 
				
			||||||
 * @author Juergen Hoeller
 | 
					 * @author Juergen Hoeller
 | 
				
			||||||
 * @author Brian Clozel
 | 
					 * @author Brian Clozel
 | 
				
			||||||
 * @author Injae Kim
 | 
					 | 
				
			||||||
 * @since 5.0
 | 
					 * @since 5.0
 | 
				
			||||||
 * @see DefaultDataBufferFactory
 | 
					 * @see DefaultDataBufferFactory
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
| 
						 | 
					@ -81,14 +80,16 @@ public class DefaultDataBuffer implements DataBuffer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Directly exposes the native {@code ByteBuffer} that this buffer is based
 | 
						 * Directly exposes the native {@code ByteBuffer} that this buffer is based
 | 
				
			||||||
	 * on also updating the {@code ByteBuffer's} position and limit to match
 | 
						 * on. The {@linkplain ByteBuffer#position() position} of the returned
 | 
				
			||||||
	 * the current {@link #readPosition()} and {@link #writePosition()}.
 | 
						 * {@code ByteBuffer} is set to the {@linkplain #readPosition() read
 | 
				
			||||||
 | 
						 * position}, and the {@linkplain ByteBuffer#limit()} to the
 | 
				
			||||||
 | 
						 * {@linkplain #writePosition() write position}.
 | 
				
			||||||
	 * @return the wrapped byte buffer
 | 
						 * @return the wrapped byte buffer
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public ByteBuffer getNativeBuffer() {
 | 
						public ByteBuffer getNativeBuffer() {
 | 
				
			||||||
		this.byteBuffer.position(this.readPosition);
 | 
							return this.byteBuffer.duplicate()
 | 
				
			||||||
		this.byteBuffer.limit(this.writePosition);
 | 
									.position(this.readPosition)
 | 
				
			||||||
		return this.byteBuffer;
 | 
									.limit(this.writePosition);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private void setNativeBuffer(ByteBuffer byteBuffer) {
 | 
						private void setNativeBuffer(ByteBuffer byteBuffer) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,20 +36,23 @@ class DefaultDataBufferTests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Test // gh-30967
 | 
						@Test // gh-30967
 | 
				
			||||||
	void getNativeBuffer() {
 | 
						void getNativeBuffer() {
 | 
				
			||||||
		DefaultDataBuffer buffer = bufferFactory.allocateBuffer(256);
 | 
							DefaultDataBuffer dataBuffer = this.bufferFactory.allocateBuffer(256);
 | 
				
			||||||
		buffer.write("0123456789", StandardCharsets.UTF_8);
 | 
							dataBuffer.write("0123456789", StandardCharsets.UTF_8);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		byte[] result = new byte[7];
 | 
							byte[] result = new byte[7];
 | 
				
			||||||
		buffer.read(result);
 | 
							dataBuffer.read(result);
 | 
				
			||||||
		assertThat(result).isEqualTo("0123456".getBytes(StandardCharsets.UTF_8));
 | 
							assertThat(result).isEqualTo("0123456".getBytes(StandardCharsets.UTF_8));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		ByteBuffer nativeBuffer = buffer.getNativeBuffer();
 | 
							ByteBuffer nativeBuffer = dataBuffer.getNativeBuffer();
 | 
				
			||||||
		assertThat(nativeBuffer.position()).isEqualTo(7);
 | 
							assertThat(nativeBuffer.position()).isEqualTo(7);
 | 
				
			||||||
		assertThat(buffer.readPosition()).isEqualTo(7);
 | 
							assertThat(dataBuffer.readPosition()).isEqualTo(7);
 | 
				
			||||||
		assertThat(nativeBuffer.limit()).isEqualTo(10);
 | 
							assertThat(nativeBuffer.limit()).isEqualTo(10);
 | 
				
			||||||
		assertThat(buffer.writePosition()).isEqualTo(10);
 | 
							assertThat(dataBuffer.writePosition()).isEqualTo(10);
 | 
				
			||||||
 | 
							assertThat(nativeBuffer.capacity()).isEqualTo(256);
 | 
				
			||||||
 | 
							assertThat(dataBuffer.capacity()).isEqualTo(256);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		release(buffer);
 | 
					
 | 
				
			||||||
 | 
							release(dataBuffer);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue