3030import io .rsocket .frame .RequestNFrameFlyweight ;
3131import io .rsocket .frame .SetupFrameFlyweight ;
3232import io .rsocket .frame .VersionFlyweight ;
33- import java .nio .ByteBuffer ;
3433import java .nio .charset .StandardCharsets ;
3534import javax .annotation .Nullable ;
3635import org .slf4j .Logger ;
4140 *
4241 * <p>This provides encoding, decoding and field accessors.
4342 */
44- public class Frame implements ByteBufHolder {
45- public static final ByteBuffer NULL_BYTEBUFFER = ByteBuffer .allocateDirect (0 );
46-
43+ public class Frame implements Payload , ByteBufHolder {
4744 private static final Recycler <Frame > RECYCLER =
4845 new Recycler <Frame >() {
4946 protected Frame newObject (Handle <Frame > handle ) {
@@ -52,7 +49,7 @@ protected Frame newObject(Handle<Frame> handle) {
5249 };
5350
5451 private final Handle <Frame > handle ;
55- private @ Nullable ByteBuf content ;
52+ private ByteBuf content ;
5653
5754 private Frame (final Handle <Frame > handle ) {
5855 this .handle = handle ;
@@ -183,43 +180,25 @@ public boolean release(int decrement) {
183180 }
184181
185182 /**
186- * Return {@link ByteBuffer } that is a {@link ByteBuffer #slice()} for the frame metadata
183+ * Return {@link ByteBuf } that is a {@link ByteBuf #slice()} for the frame metadata
187184 *
188- * <p>If no metadata is present, the ByteBuffer will have 0 capacity.
185+ * <p>If no metadata is present, the ByteBuf will have 0 capacity.
189186 *
190- * @return ByteBuffer containing the content
187+ * @return ByteBuf containing the content
191188 */
192- public ByteBuffer getMetadata () {
193- final ByteBuf metadata = FrameHeaderFlyweight .sliceFrameMetadata (content );
194- if (metadata == null ) {
195- return NULL_BYTEBUFFER ;
196- } else if (metadata .readableBytes () > 0 ) {
197- final ByteBuffer buffer = ByteBuffer .allocateDirect (metadata .readableBytes ());
198- metadata .readBytes (buffer );
199- buffer .flip ();
200- return buffer ;
201- } else {
202- return NULL_BYTEBUFFER ;
203- }
189+ public ByteBuf sliceMetadata () {
190+ return hasMetadata () ? FrameHeaderFlyweight .sliceFrameMetadata (content ) : Unpooled .EMPTY_BUFFER ;
204191 }
205192
206193 /**
207- * Return {@link ByteBuffer } that is a {@link ByteBuffer #slice()} for the frame data
194+ * Return {@link ByteBuf } that is a {@link ByteBuf #slice()} for the frame data
208195 *
209- * <p>If no data is present, the ByteBuffer will have 0 capacity.
196+ * <p>If no data is present, the ByteBuf will have 0 capacity.
210197 *
211- * @return ByteBuffer containing the data
198+ * @return ByteBuf containing the data
212199 */
213- public ByteBuffer getData () {
214- final ByteBuf data = FrameHeaderFlyweight .sliceFrameData (content );
215- if (data .readableBytes () > 0 ) {
216- final ByteBuffer buffer = ByteBuffer .allocateDirect (data .readableBytes ());
217- data .readBytes (buffer );
218- buffer .flip ();
219- return buffer ;
220- } else {
221- return NULL_BYTEBUFFER ;
222- }
200+ public ByteBuf sliceData () {
201+ return FrameHeaderFlyweight .sliceFrameData (content );
223202 }
224203
225204 /**
@@ -270,14 +249,11 @@ public static int setFlag(int current, int toSet) {
270249 return current | toSet ;
271250 }
272251
252+ @ Override
273253 public boolean hasMetadata () {
274254 return Frame .isFlagSet (this .flags (), FLAGS_M );
275255 }
276256
277- public String getDataUtf8 () {
278- return StandardCharsets .UTF_8 .decode (getData ()).toString ();
279- }
280-
281257 /* TODO:
282258 *
283259 * fromRequest(type, id, payload)
@@ -297,14 +273,8 @@ public static Frame from(
297273 String metadataMimeType ,
298274 String dataMimeType ,
299275 Payload payload ) {
300- final ByteBuf metadata =
301- payload .hasMetadata ()
302- ? Unpooled .wrappedBuffer (payload .getMetadata ())
303- : Unpooled .EMPTY_BUFFER ;
304- final ByteBuf data =
305- payload .getData () != null
306- ? Unpooled .wrappedBuffer (payload .getData ())
307- : Unpooled .EMPTY_BUFFER ;
276+ final ByteBuf metadata = payload .hasMetadata () ? payload .sliceMetadata () : Unpooled .EMPTY_BUFFER ;
277+ final ByteBuf data = payload .sliceData ();
308278
309279 final Frame frame = RECYCLER .get ();
310280 frame .content =
@@ -460,9 +430,8 @@ public static Frame from(int streamId, FrameType type, Payload payload, int init
460430 if (initialRequestN < 1 ) {
461431 throw new IllegalStateException ("initial request n must be greater than 0" );
462432 }
463- final @ Nullable ByteBuf metadata =
464- payload .hasMetadata () ? Unpooled .wrappedBuffer (payload .getMetadata ()) : null ;
465- final ByteBuf data = Unpooled .wrappedBuffer (payload .getData ());
433+ final @ Nullable ByteBuf metadata = payload .hasMetadata () ? payload .sliceMetadata () : null ;
434+ final ByteBuf data = payload .sliceData ();
466435
467436 final Frame frame = RECYCLER .get ();
468437 frame .content =
@@ -561,9 +530,8 @@ public static Frame from(int streamId, FrameType type, Payload payload) {
561530 }
562531
563532 public static Frame from (int streamId , FrameType type , Payload payload , int flags ) {
564- final ByteBuf metadata =
565- payload .hasMetadata () ? Unpooled .wrappedBuffer (payload .getMetadata ()) : null ;
566- final ByteBuf data = Unpooled .wrappedBuffer (payload .getData ());
533+ final ByteBuf metadata = payload .hasMetadata () ? payload .sliceMetadata () : null ;
534+ final ByteBuf data = payload .sliceData ();
567535 return from (streamId , type , metadata , data , flags );
568536 }
569537
0 commit comments