/** * Returns a test instance of {@link RequestStreamFrame}. * * @return a test instance of {@link RequestStreamFrame} */ public static RequestStreamFrame createTestRequestStreamFrame() { return createRequestStreamFrame(DEFAULT, false, 1, (ByteBuf) null, null); }
/** * Creates the {@code REQUEST_STREAM} frame. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param follows whether to set the Follows flag * @param initialRequestN the initial requestN * @param metadata the metadata * @param data the data * @return the {@code REQUEST_STREAM} frame * @throws NullPointerException if {@code byteBufAllocator} is {@code null} * @throws IllegalArgumentException if {@code initialRequestN} is not positive */ public static RequestStreamFrame createRequestStreamFrame( ByteBufAllocator byteBufAllocator, boolean follows, int initialRequestN, @Nullable ByteBuf metadata, @Nullable ByteBuf data) { NumberUtils.requirePositive(initialRequestN, "initialRequestN must be positive"); ByteBuf byteBuf = createFrameTypeAndFlags(byteBufAllocator, FrameType.REQUEST_STREAM); if (follows) { byteBuf = setFollowsFlag(byteBuf); } byteBuf = byteBuf.writeInt(initialRequestN); byteBuf = appendMetadata(byteBufAllocator, byteBuf, metadata); byteBuf = appendData(byteBuf, data); return RECYCLER.get().setByteBuf(byteBuf); }
/** * Returns the initial requestN. * * @return the initial requestN */ public int getInitialRequestN() { return getByteBuf().getInt(OFFSET_INITIAL_REQUEST_N); }
@Override public String toString() { return "RequestStreamFrame{" + "follows=" + isFollowsFlagSet() + ", initialRequestN=" + getInitialRequestN() + ", metadata=" + mapMetadata(ByteBufUtil::hexDump) + ", data=" + mapData(ByteBufUtil::hexDump) + '}'; } }
@Override public RequestStreamFrame createFragment( ByteBufAllocator byteBufAllocator, @Nullable ByteBuf metadata, @Nullable ByteBuf data) { return createRequestStreamFrame(byteBufAllocator, true, getInitialRequestN(), metadata, data); }
/** * Creates the {@code REQUEST_STREAM} frame. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param follows whether to set the Follows flag * @param initialRequestN the initial requestN * @param metadata the metadata * @param data the data * @return the {@code REQUEST_STREAM} frame * @throws NullPointerException if {@code byteBufAllocator} is {@code null} */ public static RequestStreamFrame createRequestStreamFrame( ByteBufAllocator byteBufAllocator, boolean follows, int initialRequestN, @Nullable String metadata, @Nullable String data) { ByteBuf metadataByteBuf = getUtf8AsByteBuf(metadata); ByteBuf dataByteBuf = getUtf8AsByteBuf(data); try { return createRequestStreamFrame( byteBufAllocator, follows, initialRequestN, metadataByteBuf, dataByteBuf); } finally { release(metadataByteBuf); release(dataByteBuf); } }
@Override public ByteBuf getUnsafeData() { return getData(OFFSET_METADATA_LENGTH); }
@Override public @Nullable ByteBuf getUnsafeMetadata() { return getMetadata(OFFSET_METADATA_LENGTH); }
@Override public RequestStreamFrame createNonFragment( ByteBufAllocator byteBufAllocator, @Nullable ByteBuf metadata, @Nullable ByteBuf data) { return createRequestStreamFrame(byteBufAllocator, false, getInitialRequestN(), metadata, data); }
@Override public String toString() { return "RequestStreamFrame{" + "follows=" + isFollowsFlagSet() + ", initialRequestN=" + getInitialRequestN() + ", metadata=" + mapMetadata(ByteBufUtil::hexDump) + ", data=" + mapData(ByteBufUtil::hexDump) + '}'; } }
/** * Creates the {@code REQUEST_STREAM} frame. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param follows whether to set the Follows flag * @param initialRequestN the initial requestN * @param metadata the metadata * @param data the data * @return the {@code REQUEST_STREAM} frame * @throws NullPointerException if {@code byteBufAllocator} is {@code null} */ public static RequestStreamFrame createRequestStreamFrame( ByteBufAllocator byteBufAllocator, boolean follows, int initialRequestN, @Nullable String metadata, @Nullable String data) { ByteBuf metadataByteBuf = getUtf8AsByteBuf(metadata); ByteBuf dataByteBuf = getUtf8AsByteBuf(data); try { return createRequestStreamFrame( byteBufAllocator, follows, initialRequestN, metadataByteBuf, dataByteBuf); } finally { release(metadataByteBuf); release(dataByteBuf); } }
@Override public ByteBuf getUnsafeData() { return getData(OFFSET_METADATA_LENGTH); }
@Override public @Nullable ByteBuf getUnsafeMetadata() { return getMetadata(OFFSET_METADATA_LENGTH); }
public static RequestStreamFrame createTestRequestStreamFrame() { return createRequestStreamFrame(DEFAULT, false, 1, (ByteBuf) null, null); }
/** * Creates the {@code REQUEST_STREAM} frame. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param follows whether to set the Follows flag * @param initialRequestN the initial requestN * @param metadata the metadata * @param data the data * @return the {@code REQUEST_STREAM} frame * @throws NullPointerException if {@code byteBufAllocator} is {@code null} * @throws IllegalArgumentException if {@code initialRequestN} is not positive */ public static RequestStreamFrame createRequestStreamFrame( ByteBufAllocator byteBufAllocator, boolean follows, int initialRequestN, @Nullable ByteBuf metadata, @Nullable ByteBuf data) { NumberUtils.requirePositive(initialRequestN, "initialRequestN must be positive"); ByteBuf byteBuf = createFrameTypeAndFlags(byteBufAllocator, FrameType.REQUEST_STREAM); if (follows) { byteBuf = setFollowsFlag(byteBuf); } byteBuf = byteBuf.writeInt(initialRequestN); byteBuf = appendMetadata(byteBufAllocator, byteBuf, metadata); byteBuf = appendData(byteBuf, data); return RECYCLER.get().setByteBuf(byteBuf); }
@Override public RequestStreamFrame createNonFragment( ByteBufAllocator byteBufAllocator, @Nullable ByteBuf metadata, @Nullable ByteBuf data) { return createRequestStreamFrame(byteBufAllocator, false, getInitialRequestN(), metadata, data); }
/** * Returns the initial requestN. * * @return the initial requestN */ public int getInitialRequestN() { return getByteBuf().getInt(OFFSET_INITIAL_REQUEST_N); }
@DisplayName("reassembles data") @Test void reassembleData() { ByteBuf data = getRandomByteBuf(6); RequestStreamFrame frame = createRequestStreamFrame(DEFAULT, false, 1, null, data); RequestStreamFrame fragment1 = createRequestStreamFrame(DEFAULT, true, 1, null, data.slice(0, 2)); PayloadFrame fragment2 = createPayloadFrame(DEFAULT, true, false, null, data.slice(2, 2)); PayloadFrame fragment3 = createPayloadFrame(DEFAULT, false, false, null, data.slice(4, 2)); FrameReassembler frameReassembler = createFrameReassembler(DEFAULT); assertThat(frameReassembler.reassemble(fragment1)).isNull(); assertThat(frameReassembler.reassemble(fragment2)).isNull(); assertThat(frameReassembler.reassemble(fragment3)).isEqualTo(frame); }
@Override public RequestStreamFrame createFragment( ByteBufAllocator byteBufAllocator, @Nullable ByteBuf metadata, @Nullable ByteBuf data) { return createRequestStreamFrame(byteBufAllocator, true, getInitialRequestN(), metadata, data); }
@DisplayName("reassembles metadata") @Test void reassembleMetadata() { ByteBuf metadata = getRandomByteBuf(6); RequestStreamFrame frame = createRequestStreamFrame(DEFAULT, false, 1, metadata, null); RequestStreamFrame fragment1 = createRequestStreamFrame(DEFAULT, true, 1, metadata.slice(0, 2), null); PayloadFrame fragment2 = createPayloadFrame(DEFAULT, true, true, metadata.slice(2, 2), null); PayloadFrame fragment3 = createPayloadFrame(DEFAULT, false, true, metadata.slice(4, 2), null); FrameReassembler frameReassembler = createFrameReassembler(DEFAULT); assertThat(frameReassembler.reassemble(fragment1)).isNull(); assertThat(frameReassembler.reassemble(fragment2)).isNull(); assertThat(frameReassembler.reassemble(fragment3)).isEqualTo(frame); }