/** * Creates the frame with a stream id. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param streamId the stream id * @param frame the frame to prepend the stream id to * @return the frame with a stream id * @throws NullPointerException if {@code byteBufAllocator} or {@code frame} is {@code null} */ public static StreamIdFrame createStreamIdFrame( ByteBufAllocator byteBufAllocator, int streamId, Frame frame) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(frame, "frame must not be null"); ByteBuf streamIdByteBuf = frame.mapFrame( frameByteBuf -> { ByteBuf byteBuf = byteBufAllocator.buffer(STREAM_ID_BYTES).writeInt(streamId); return Unpooled.wrappedBuffer(byteBuf, frameByteBuf.retain()); }); return RECYCLER.get().setByteBuf(streamIdByteBuf); }
/** * Creates the frame with a frame length. * * @param byteBufAllocator the {@link ByteBufAllocator} to use * @param frame the frame to prepend the frame length to * @return the frame with a frame length * @throws NullPointerException if {@code byteBufAllocator} or {@code frame} is {@code null} */ public static FrameLengthFrame createFrameLengthFrame( ByteBufAllocator byteBufAllocator, Frame frame) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(frame, "frame must not be null"); ByteBuf frameLengthByteBuf = frame.mapFrame( frameByteBuf -> { ByteBuf byteBuf = byteBufAllocator .buffer(FRAME_LENGTH_BYTES) .writeMedium(getLengthAsUnsignedMedium(frameByteBuf)); return Unpooled.wrappedBuffer(byteBuf, frameByteBuf.retain()); }); return RECYCLER.get().setByteBuf(frameLengthByteBuf); }
@DisplayName("maps frame") @Test default void mapFrame() { Tuple2<T, ByteBuf> tuple = getFrame(); T frame = tuple.getT1(); ByteBuf byteBuf = tuple.getT2(); assertThat(frame.mapFrame(Function.identity())).isEqualTo(byteBuf); }
@DisplayName("mapFrame throws NullPointerException with null function") @Test default void mapFrameNullFunction() { Tuple2<T, ByteBuf> tuple = getFrame(); T frame = tuple.getT1(); assertThatNullPointerException() .isThrownBy(() -> frame.mapFrame(null)) .withMessage("function must not be null"); } }
/** * Creates the frame with a stream id. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param streamId the stream id * @param frame the frame to prepend the stream id to * @return the frame with a stream id * @throws NullPointerException if {@code byteBufAllocator} or {@code frame} is {@code null} */ public static StreamIdFrame createStreamIdFrame( ByteBufAllocator byteBufAllocator, int streamId, Frame frame) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(frame, "frame must not be null"); ByteBuf streamIdByteBuf = frame.mapFrame( frameByteBuf -> { ByteBuf byteBuf = byteBufAllocator.buffer(STREAM_ID_BYTES).writeInt(streamId); return Unpooled.wrappedBuffer(byteBuf, frameByteBuf.retain()); }); return RECYCLER.get().setByteBuf(streamIdByteBuf); }
/** * Creates the frame with a frame length. * * @param byteBufAllocator the {@link ByteBufAllocator} to use * @param frame the frame to prepend the frame length to * @return the frame with a frame length * @throws NullPointerException if {@code byteBufAllocator} or {@code frame} is {@code null} */ public static FrameLengthFrame createFrameLengthFrame( ByteBufAllocator byteBufAllocator, Frame frame) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(frame, "frame must not be null"); ByteBuf frameLengthByteBuf = frame.mapFrame( frameByteBuf -> { ByteBuf byteBuf = byteBufAllocator .buffer(FRAME_LENGTH_BYTES) .writeMedium(getLengthAsUnsignedMedium(frameByteBuf)); return Unpooled.wrappedBuffer(byteBuf, frameByteBuf.retain()); }); return RECYCLER.get().setByteBuf(frameLengthByteBuf); }