31
31
* Netty {@link ByteBufAllocator}.
32
32
*
33
33
* @author Arjen Poutsma
34
+ * @author Juergen Hoeller
34
35
* @since 5.0
35
36
* @see io.netty.buffer.PooledByteBufAllocator
36
37
* @see io.netty.buffer.UnpooledByteBufAllocator
@@ -47,7 +48,7 @@ public class NettyDataBufferFactory implements DataBufferFactory {
47
48
* @see io.netty.buffer.UnpooledByteBufAllocator
48
49
*/
49
50
public NettyDataBufferFactory (ByteBufAllocator byteBufAllocator ) {
50
- Assert .notNull (byteBufAllocator , "'byteBufAllocator' must not be null" );
51
+ Assert .notNull (byteBufAllocator , "ByteBufAllocator must not be null" );
51
52
this .byteBufAllocator = byteBufAllocator ;
52
53
}
53
54
@@ -82,37 +83,40 @@ public DataBuffer wrap(byte[] bytes) {
82
83
return new NettyDataBuffer (byteBuf , this );
83
84
}
84
85
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
+
85
95
/**
86
96
* {@inheritDoc}
87
97
* <p>This implementation uses Netty's {@link CompositeByteBuf}.
88
98
*/
89
99
@ Override
90
100
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 );
93
107
for (DataBuffer dataBuffer : dataBuffers ) {
94
108
Assert .isInstanceOf (NettyDataBuffer .class , dataBuffer );
95
- NettyDataBuffer nettyDataBuffer = (NettyDataBuffer ) dataBuffer ;
96
- composite .addComponent (true , nettyDataBuffer .getNativeBuffer ());
109
+ composite .addComponent (true , ((NettyDataBuffer ) dataBuffer ).getNativeBuffer ());
97
110
}
98
111
return new NettyDataBuffer (composite , this );
99
112
}
100
113
101
114
/**
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
116
120
* @return the netty {@code ByteBuf}
117
121
*/
118
122
public static ByteBuf toByteBuf (DataBuffer buffer ) {
0 commit comments