2121import io .netty .buffer .ByteBufUtil ;
2222import io .netty .buffer .Unpooled ;
2323import io .netty .util .AbstractReferenceCounted ;
24+ import io .netty .util .Recycler ;
2425import io .rsocket .Payload ;
2526
2627import javax .annotation .Nullable ;
2930import java .nio .charset .Charset ;
3031
3132public final class ByteBufPayload extends AbstractReferenceCounted implements Payload {
32- private final ByteBuf data ;
33- private final ByteBuf metadata ;
33+ private static final Recycler <ByteBufPayload > RECYCLER =
34+ new Recycler <ByteBufPayload >() {
35+ protected ByteBufPayload newObject (Handle <ByteBufPayload > handle ) {
36+ return new ByteBufPayload (handle );
37+ }
38+ };
3439
35- private ByteBufPayload (ByteBuf data ) {
36- this .data = data .asReadOnly ();
37- this .metadata = null ;
38- }
40+ private final Recycler .Handle <ByteBufPayload > handle ;
41+ private ByteBuf data ;
42+ private ByteBuf metadata ;
3943
40- private ByteBufPayload (ByteBuf data , @ Nullable ByteBuf metadata ) {
41- this .data = data .asReadOnly ();
42- this .metadata = metadata == null ? null : metadata .asReadOnly ();
44+ private ByteBufPayload (final Recycler .Handle <ByteBufPayload > handle ) {
45+ this .handle = handle ;
4346 }
4447
4548 @ Override
@@ -49,12 +52,12 @@ public boolean hasMetadata() {
4952
5053 @ Override
5154 public ByteBuf sliceMetadata () {
52- return metadata == null ? Unpooled .EMPTY_BUFFER : metadata . duplicate () ;
55+ return metadata == null ? Unpooled .EMPTY_BUFFER : metadata ;
5356 }
5457
5558 @ Override
5659 public ByteBuf sliceData () {
57- return data . duplicate () ;
60+ return data ;
5861 }
5962
6063 @ Override
@@ -90,9 +93,12 @@ public ByteBufPayload touch(Object hint) {
9093 @ Override
9194 protected void deallocate () {
9295 data .release ();
96+ data = null ;
9397 if (metadata != null ) {
9498 metadata .release ();
99+ metadata = null ;
95100 }
101+ handle .recycle (this );
96102 }
97103
98104 /**
@@ -102,7 +108,7 @@ protected void deallocate() {
102108 * @return a payload.
103109 */
104110 public static Payload create (String data ) {
105- return new ByteBufPayload (ByteBufUtil .writeUtf8 (ByteBufAllocator .DEFAULT , data ));
111+ return create (ByteBufUtil .writeUtf8 (ByteBufAllocator .DEFAULT , data ), null );
106112 }
107113
108114 /**
@@ -114,48 +120,51 @@ public static Payload create(String data) {
114120 * @return a payload.
115121 */
116122 public static Payload create (String data , @ Nullable String metadata ) {
117- return new ByteBufPayload (
123+ return create (
118124 ByteBufUtil .writeUtf8 (ByteBufAllocator .DEFAULT , data ),
119125 metadata == null ? null : ByteBufUtil .writeUtf8 (ByteBufAllocator .DEFAULT , metadata )
120126 );
121127 }
122128
123129 public static Payload create (CharSequence data , Charset dataCharset ) {
124- return new ByteBufPayload (ByteBufUtil .encodeString (ByteBufAllocator .DEFAULT , CharBuffer .wrap (data ), dataCharset ));
130+ return create (ByteBufUtil .encodeString (ByteBufAllocator .DEFAULT , CharBuffer .wrap (data ), dataCharset ), null );
125131 }
126132
127133 public static Payload create (CharSequence data , Charset dataCharset , @ Nullable CharSequence metadata , Charset metadataCharset ) {
128- return new ByteBufPayload (
134+ return create (
129135 ByteBufUtil .encodeString (ByteBufAllocator .DEFAULT , CharBuffer .wrap (data ), dataCharset ),
130136 metadata == null ? null : ByteBufUtil .encodeString (ByteBufAllocator .DEFAULT , CharBuffer .wrap (metadata ), metadataCharset )
131137 );
132138 }
133139
134140 public static Payload create (byte [] data ) {
135- return new ByteBufPayload (Unpooled .wrappedBuffer (data ));
141+ return create (Unpooled .wrappedBuffer (data ), null );
136142 }
137143
138144 public static Payload create (byte [] data , @ Nullable byte [] metadata ) {
139- return new ByteBufPayload (Unpooled .wrappedBuffer (data ), metadata == null ? null : Unpooled .wrappedBuffer (metadata ));
145+ return create (Unpooled .wrappedBuffer (data ), metadata == null ? null : Unpooled .wrappedBuffer (metadata ));
140146 }
141147
142148 public static Payload create (ByteBuffer data ) {
143- return new ByteBufPayload (Unpooled .wrappedBuffer (data ));
149+ return create (Unpooled .wrappedBuffer (data ), null );
144150 }
145151
146152 public static Payload create (ByteBuffer data , @ Nullable ByteBuffer metadata ) {
147- return new ByteBufPayload (Unpooled .wrappedBuffer (data ), metadata == null ? null : Unpooled .wrappedBuffer (metadata ));
153+ return create (Unpooled .wrappedBuffer (data ), metadata == null ? null : Unpooled .wrappedBuffer (metadata ));
148154 }
149155
150156 public static Payload create (ByteBuf data ) {
151- return new ByteBufPayload (data );
157+ return create (data , null );
152158 }
153159
154160 public static Payload create (ByteBuf data , @ Nullable ByteBuf metadata ) {
155- return new ByteBufPayload (data , metadata );
161+ ByteBufPayload payload = RECYCLER .get ();
162+ payload .data = data ;
163+ payload .metadata = metadata ;
164+ return payload ;
156165 }
157166
158167 public static Payload create (Payload payload ) {
159- return new ByteBufPayload (payload .sliceData (), payload .hasMetadata () ? payload .sliceMetadata () : null );
168+ return create (payload .sliceData (), payload .hasMetadata () ? payload .sliceMetadata () : null );
160169 }
161170}
0 commit comments