Skip to content

Commit c5428e6

Browse files
committed
NettyDataBufferFactory.join returns single-element buffer as-is
Issue: SPR-17560 (cherry picked from commit d5dab12)
1 parent 76fd179 commit c5428e6

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ext {
4747
junitVintageVersion = "4.12.3"
4848
kotlinVersion = "1.2.71"
4949
log4jVersion = "2.11.1"
50-
nettyVersion = "4.1.31.Final" // constrained by StringDecoderTests
50+
nettyVersion = "4.1.32.Final"
5151
reactorVersion = "Bismuth-SR14"
5252
rxjavaVersion = "1.3.8"
5353
rxjavaAdapterVersion = "1.2.1"

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

Lines changed: 23 additions & 19 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

@@ -82,37 +83,40 @@ public DataBuffer wrap(byte[] bytes) {
8283
return new NettyDataBuffer(byteBuf, this);
8384
}
8485

86+
/**
87+
* Wrap the given Netty {@link ByteBuf} in a {@code NettyDataBuffer}.
88+
* @param byteBuf the Netty byte buffer to wrap
89+
* @return the wrapped buffer
90+
*/
91+
public NettyDataBuffer wrap(ByteBuf byteBuf) {
92+
return new NettyDataBuffer(byteBuf, this);
93+
}
94+
8595
/**
8696
* {@inheritDoc}
8797
* <p>This implementation uses Netty's {@link CompositeByteBuf}.
8898
*/
8999
@Override
90100
public DataBuffer join(List<? extends DataBuffer> dataBuffers) {
91-
Assert.notNull(dataBuffers, "'dataBuffers' must not be null");
92-
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);
93107
for (DataBuffer dataBuffer : dataBuffers) {
94108
Assert.isInstanceOf(NettyDataBuffer.class, dataBuffer);
95-
NettyDataBuffer nettyDataBuffer = (NettyDataBuffer) dataBuffer;
96-
composite.addComponent(true, nettyDataBuffer.getNativeBuffer());
109+
composite.addComponent(true, ((NettyDataBuffer) dataBuffer).getNativeBuffer());
97110
}
98111
return new NettyDataBuffer(composite, this);
99112
}
100113

101114
/**
102-
* Wrap the given Netty {@link ByteBuf} in a {@code NettyDataBuffer}.
103-
* @param byteBuf the Netty byte buffer to wrap
104-
* @return the wrapped buffer
105-
*/
106-
public NettyDataBuffer wrap(ByteBuf byteBuf) {
107-
return new NettyDataBuffer(byteBuf, this);
108-
}
109-
110-
/**
111-
* Return the given Netty {@link DataBuffer} as a {@link ByteBuf}. Returns the
112-
* {@linkplain NettyDataBuffer#getNativeBuffer() native buffer} if {@code buffer} is
113-
* a {@link NettyDataBuffer}; returns {@link Unpooled#wrappedBuffer(ByteBuffer)}
114-
* otherwise.
115-
* @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
116120
* @return the netty {@code ByteBuf}
117121
*/
118122
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)