Browse Source

NettyDataBufferFactory.join returns single-element buffer as-is

Issue: SPR-17560
pull/2038/head
Juergen Hoeller 7 years ago
parent
commit
d5dab12909
  1. 21
      spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java
  2. 12
      spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/HttpServerTests.java

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

@ -31,6 +31,7 @@ import org.springframework.util.Assert;
* Netty {@link ByteBufAllocator}. * Netty {@link ByteBufAllocator}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller
* @since 5.0 * @since 5.0
* @see io.netty.buffer.PooledByteBufAllocator * @see io.netty.buffer.PooledByteBufAllocator
* @see io.netty.buffer.UnpooledByteBufAllocator * @see io.netty.buffer.UnpooledByteBufAllocator
@ -47,7 +48,7 @@ public class NettyDataBufferFactory implements DataBufferFactory {
* @see io.netty.buffer.UnpooledByteBufAllocator * @see io.netty.buffer.UnpooledByteBufAllocator
*/ */
public NettyDataBufferFactory(ByteBufAllocator byteBufAllocator) { public NettyDataBufferFactory(ByteBufAllocator byteBufAllocator) {
Assert.notNull(byteBufAllocator, "'byteBufAllocator' must not be null"); Assert.notNull(byteBufAllocator, "ByteBufAllocator must not be null");
this.byteBufAllocator = byteBufAllocator; this.byteBufAllocator = byteBufAllocator;
} }
@ -97,8 +98,12 @@ public class NettyDataBufferFactory implements DataBufferFactory {
*/ */
@Override @Override
public DataBuffer join(List<? extends DataBuffer> dataBuffers) { public DataBuffer join(List<? extends DataBuffer> dataBuffers) {
Assert.notNull(dataBuffers, "'dataBuffers' must not be null"); Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
CompositeByteBuf composite = this.byteBufAllocator.compositeBuffer(dataBuffers.size()); int bufferCount = dataBuffers.size();
if (bufferCount == 1) {
return dataBuffers.get(0);
}
CompositeByteBuf composite = this.byteBufAllocator.compositeBuffer(bufferCount);
for (DataBuffer dataBuffer : dataBuffers) { for (DataBuffer dataBuffer : dataBuffers) {
Assert.isInstanceOf(NettyDataBuffer.class, dataBuffer); Assert.isInstanceOf(NettyDataBuffer.class, dataBuffer);
composite.addComponent(true, ((NettyDataBuffer) dataBuffer).getNativeBuffer()); composite.addComponent(true, ((NettyDataBuffer) dataBuffer).getNativeBuffer());
@ -107,11 +112,11 @@ public class NettyDataBufferFactory implements DataBufferFactory {
} }
/** /**
* Return the given Netty {@link DataBuffer} as a {@link ByteBuf}. Returns the * Return the given Netty {@link DataBuffer} as a {@link ByteBuf}.
* {@linkplain NettyDataBuffer#getNativeBuffer() native buffer} if {@code buffer} is * <p>Returns the {@linkplain NettyDataBuffer#getNativeBuffer() native buffer}
* a {@link NettyDataBuffer}; returns {@link Unpooled#wrappedBuffer(ByteBuffer)} * if {@code buffer} is a {@link NettyDataBuffer}; returns
* otherwise. * {@link Unpooled#wrappedBuffer(ByteBuffer)} otherwise.
* @param buffer the {@code DataBuffer} to return a {@code ByteBuf} for. * @param buffer the {@code DataBuffer} to return a {@code ByteBuf} for
* @return the netty {@code ByteBuf} * @return the netty {@code ByteBuf}
*/ */
public static ByteBuf toByteBuf(DataBuffer buffer) { public static ByteBuf toByteBuf(DataBuffer buffer) {

12
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/HttpServerTests.java

@ -26,8 +26,8 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.route; import static org.springframework.web.reactive.function.server.RouterFunctions.*;
/** /**
* Sample tests demonstrating live server integration tests. * Sample tests demonstrating live server integration tests.
@ -43,11 +43,9 @@ public class HttpServerTests {
@Before @Before
public void setUp() throws Exception { public void start() throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler( HttpHandler httpHandler = RouterFunctions.toHttpHandler(
route(GET("/test"), request -> route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!")));
ServerResponse.ok().syncBody("It works!")));
this.server = new ReactorHttpServer(); this.server = new ReactorHttpServer();
this.server.setHandler(httpHandler); this.server.setHandler(httpHandler);
@ -60,7 +58,7 @@ public class HttpServerTests {
} }
@After @After
public void tearDown() { public void stop() {
this.server.stop(); this.server.stop();
} }

Loading…
Cancel
Save