public static Frame from(int streamId, FrameType type, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer(RequestFrameFlyweight.computeFrameLength(type, null, 0)); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER)); return frame; }
public static Frame from( int streamId, FrameType type, ByteBuf metadata, ByteBuf data, int initialRequestN, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata.readableBytes(), data.readableBytes())); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, initialRequestN, metadata, data)); return frame; }
public static Frame from(int streamId, FrameType type, Payload payload, int initialRequestN) { if (initialRequestN < 1) { throw new IllegalStateException("initial request n must be greater than 0"); } final @Nullable ByteBuf metadata = payload.hasMetadata() ? payload.sliceMetadata() : null; final ByteBuf data = payload.sliceData(); final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes())); if (type.hasInitialRequestN()) { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, initialRequestN, metadata, data)); } else { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, metadata, data)); } return frame; }
@Test public void testEncoding() { int encoded = RequestFrameFlyweight.encode( byteBuf, 1, FrameHeaderFlyweight.FLAGS_M, FrameType.REQUEST_STREAM, 1, Unpooled.copiedBuffer("md", StandardCharsets.UTF_8), Unpooled.copiedBuffer("d", StandardCharsets.UTF_8)); assertEquals( "000010000000011900000000010000026d6464", ByteBufUtil.hexDump(byteBuf, 0, encoded)); Payload payload = DefaultPayload.create(Frame.from(stringToBuf("000010000000011900000000010000026d6464"))); assertEquals("md", StandardCharsets.UTF_8.decode(payload.getMetadata()).toString()); }
@Test public void testEncodingWithEmptyMetadata() { int encoded = RequestFrameFlyweight.encode( byteBuf, 1, FrameHeaderFlyweight.FLAGS_M, FrameType.REQUEST_STREAM, 1, Unpooled.copiedBuffer("", StandardCharsets.UTF_8), Unpooled.copiedBuffer("d", StandardCharsets.UTF_8)); assertEquals("00000e0000000119000000000100000064", ByteBufUtil.hexDump(byteBuf, 0, encoded)); Payload payload = DefaultPayload.create(Frame.from(stringToBuf("00000e0000000119000000000100000064"))); assertEquals("", StandardCharsets.UTF_8.decode(payload.getMetadata()).toString()); }
@Test public void testEncodingWithNullMetadata() { int encoded = RequestFrameFlyweight.encode( byteBuf, 1, 0, FrameType.REQUEST_STREAM, 1, null, Unpooled.copiedBuffer("d", StandardCharsets.UTF_8)); assertEquals("00000b0000000118000000000164", ByteBufUtil.hexDump(byteBuf, 0, encoded)); Payload payload = DefaultPayload.create(Frame.from(stringToBuf("00000b0000000118000000000164"))); assertFalse(payload.hasMetadata()); }
public static Frame from(int streamId, FrameType type, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer(RequestFrameFlyweight.computeFrameLength(type, null, 0)); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER)); return frame; }
public static Frame from( int streamId, FrameType type, ByteBuf metadata, ByteBuf data, int initialRequestN, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata.readableBytes(), data.readableBytes())); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, initialRequestN, metadata, data)); return frame; }
public static Frame from(int streamId, FrameType type, Payload payload, int initialRequestN) { if (initialRequestN < 1) { throw new IllegalStateException("initial request n must be greater than 0"); } final @Nullable ByteBuf metadata = payload.hasMetadata() ? payload.sliceMetadata() : null; final ByteBuf data = payload.sliceData(); final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes())); if (type.hasInitialRequestN()) { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, initialRequestN, metadata, data)); } else { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, metadata, data)); } return frame; }