Skip to content

Commit d5dab12

Browse files
committed
NettyDataBufferFactory.join returns single-element buffer as-is
Issue: SPR-17560
1 parent 7854b7a commit d5dab12

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Netty {@link ByteBufAllocator}.
3232
*
3333
* @author Arjen Poutsma
34+
* @author Juergen Hoeller
3435
* @since 5.0
3536
* @see io.netty.buffer.PooledByteBufAllocator
3637
* @see io.netty.buffer.UnpooledByteBufAllocator
@@ -47,7 +48,7 @@ public class NettyDataBufferFactory implements DataBufferFactory {
4748
* @see io.netty.buffer.UnpooledByteBufAllocator
4849
*/
4950
public NettyDataBufferFactory(ByteBufAllocator byteBufAllocator) {
50-
Assert.notNull(byteBufAllocator, "'byteBufAllocator' must not be null");
51+
Assert.notNull(byteBufAllocator, "ByteBufAllocator must not be null");
5152
this.byteBufAllocator = byteBufAllocator;
5253
}
5354

@@ -97,8 +98,12 @@ public NettyDataBuffer wrap(ByteBuf byteBuf) {
9798
*/
9899
@Override
99100
public DataBuffer join(List<? extends DataBuffer> dataBuffers) {
100-
Assert.notNull(dataBuffers, "'dataBuffers' must not be null");
101-
CompositeByteBuf composite = this.byteBufAllocator.compositeBuffer(dataBuffers.size());
101+
Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
102+
int bufferCount = dataBuffers.size();
103+
if (bufferCount == 1) {
104+
return dataBuffers.get(0);
105+
}
106+
CompositeByteBuf composite = this.byteBufAllocator.compositeBuffer(bufferCount);
102107
for (DataBuffer dataBuffer : dataBuffers) {
103108
Assert.isInstanceOf(NettyDataBuffer.class, dataBuffer);
104109
composite.addComponent(true, ((NettyDataBuffer) dataBuffer).getNativeBuffer());
@@ -107,11 +112,11 @@ public DataBuffer join(List<? extends DataBuffer> dataBuffers) {
107112
}
108113

109114
/**
110-
* Return the given Netty {@link DataBuffer} as a {@link ByteBuf}. Returns the
111-
* {@linkplain NettyDataBuffer#getNativeBuffer() native buffer} if {@code buffer} is
112-
* a {@link NettyDataBuffer}; returns {@link Unpooled#wrappedBuffer(ByteBuffer)}
113-
* otherwise.
114-
* @param buffer the {@code DataBuffer} to return a {@code ByteBuf} for.
115+
* Return the given Netty {@link DataBuffer} as a {@link ByteBuf}.
116+
* <p>Returns the {@linkplain NettyDataBuffer#getNativeBuffer() native buffer}
117+
* if {@code buffer} is a {@link NettyDataBuffer}; returns
118+
* {@link Unpooled#wrappedBuffer(ByteBuffer)} otherwise.
119+
* @param buffer the {@code DataBuffer} to return a {@code ByteBuf} for
115120
* @return the netty {@code ByteBuf}
116121
*/
117122
public static ByteBuf toByteBuf(DataBuffer buffer) {

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.springframework.web.reactive.function.server.RouterFunctions;
2727
import org.springframework.web.reactive.function.server.ServerResponse;
2828

29-
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
30-
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
29+
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
30+
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
3131

3232
/**
3333
* Sample tests demonstrating live server integration tests.
@@ -43,11 +43,9 @@ public class HttpServerTests {
4343

4444

4545
@Before
46-
public void setUp() throws Exception {
47-
46+
public void start() throws Exception {
4847
HttpHandler httpHandler = RouterFunctions.toHttpHandler(
49-
route(GET("/test"), request ->
50-
ServerResponse.ok().syncBody("It works!")));
48+
route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!")));
5149

5250
this.server = new ReactorHttpServer();
5351
this.server.setHandler(httpHandler);
@@ -60,7 +58,7 @@ public void setUp() throws Exception {
6058
}
6159

6260
@After
63-
public void tearDown() {
61+
public void stop() {
6462
this.server.stop();
6563
}
6664

0 commit comments

Comments
 (0)