/** * Returns a test instance of {@link StreamIdFrame}. * * @return a test instance of {@link StreamIdFrame} */ public static StreamIdFrame createTestStreamIdFrame() { return createStreamIdFrame(DEFAULT, 1, createTestCancelFrame()); }
/** * Returns the stream id. * * @return the stream id */ public int getStreamId() { return getByteBuf().getInt(0); }
@Override public String toString() { return "StreamIdFrame{" + "streamId=" + getStreamId() + ", frameWithoutStreamId=" + mapFrameWithoutStreamId(ByteBufUtil::hexDump) + '}'; } }
/** * Creates the frame with a stream id. * * @param byteBuf the {@link ByteBuf} representing the frame * @return the frame with a stream id * @throws NullPointerException if {@code byteBuf} is {@code null} */ public static StreamIdFrame createStreamIdFrame(ByteBuf byteBuf) { Objects.requireNonNull(byteBuf, "byteBuf must not be null"); return RECYCLER.get().setByteBuf(byteBuf.retain()); }
/** * Exposes the {@link Frame} without the stream id as a {@link ByteBuf} for mapping to a different * type. * * @param function the function to transform the {@link Frame} without the stream id as a {@link * ByteBuf} to a different type * @param <T> the different type * @return the {@link Frame} without the stream id as a {@link ByteBuf} mapped to a different type * @throws NullPointerException if {@code function} is {@code null} */ public <T> T mapFrameWithoutStreamId(Function<ByteBuf, T> function) { Objects.requireNonNull(function, "function must not be null"); return function.apply(getUnsafeFrameWithoutStreamId()); }
/** * 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); }
/** * Exposes the {@link Frame} without the stream id as a {@link ByteBuf} for mapping to a different * type. * * @param function the function to transform the {@link Frame} without the stream id as a {@link * ByteBuf} to a different type * @param <T> the different type * @return the {@link Frame} without the stream id as a {@link ByteBuf} mapped to a different type * @throws NullPointerException if {@code function} is {@code null} */ public <T> T mapFrameWithoutStreamId(Function<ByteBuf, T> function) { Objects.requireNonNull(function, "function must not be null"); return function.apply(getUnsafeFrameWithoutStreamId()); }
public static StreamIdFrame createTestStreamIdFrame() { return createStreamIdFrame(DEFAULT, 1, createTestCancelFrame()); }
/** * Returns a {@link Tuple2} of the stream id, and the frame. This strips the frame length and * stream id header from the abstraction leaking frame. * * @param abstractionLeakingFrame the abstraction leaking frame * @return a {@link Tuple2} of the stream id, and the frame * @throws NullPointerException if {@code abstractionLeakingFrame} is {@code null} */ public static Tuple2<Integer, io.rsocket.framing.Frame> fromAbstractionLeakingFrame( Frame abstractionLeakingFrame) { Objects.requireNonNull(abstractionLeakingFrame, "abstractionLeakingFrame must not be null"); FrameLengthFrame frameLengthFrame = null; StreamIdFrame streamIdFrame = null; try { frameLengthFrame = createFrameLengthFrame(abstractionLeakingFrame.content()); streamIdFrame = frameLengthFrame.mapFrameWithoutFrameLength(StreamIdFrame::createStreamIdFrame); io.rsocket.framing.Frame frame = streamIdFrame.mapFrameWithoutStreamId(FrameFactory::createFrame); return Tuples.of(streamIdFrame.getStreamId(), frame); } finally { disposeQuietly(frameLengthFrame, streamIdFrame); release(abstractionLeakingFrame); } }
/** * Creates the frame with a stream id. * * @param byteBuf the {@link ByteBuf} representing the frame * @return the frame with a stream id * @throws NullPointerException if {@code byteBuf} is {@code null} */ public static StreamIdFrame createStreamIdFrame(ByteBuf byteBuf) { Objects.requireNonNull(byteBuf, "byteBuf must not be null"); return RECYCLER.get().setByteBuf(byteBuf.retain()); }
/** * Returns the frame without stream id directly. * * <p><b>Note:</b> this frame without stream id will be outside of the {@link Frame}'s lifecycle * and may be released at any time. It is highly recommended that you {@link ByteBuf#retain()} the * frame without stream id if you store it. * * @return the frame without stream id directly * @see #mapFrameWithoutStreamId(Function) */ public ByteBuf getUnsafeFrameWithoutStreamId() { ByteBuf byteBuf = getByteBuf(); return byteBuf.slice(STREAM_ID_BYTES, byteBuf.readableBytes() - STREAM_ID_BYTES).asReadOnly(); }
/** * Returns an abstraction leaking frame with the stream id and frame. This adds the frame length * and stream id header to the frame. * * @param byteBufAllocator the {@link ByteBufAllocator} to use * @param streamId the stream id * @param frame the frame * @return an abstraction leaking frame with the stream id and frame * @throws NullPointerException if {@code byteBufAllocator} or {@code frame} is {@code null} */ public static Frame toAbstractionLeakingFrame( ByteBufAllocator byteBufAllocator, int streamId, io.rsocket.framing.Frame frame) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(frame, "frame must not be null"); StreamIdFrame streamIdFrame = null; FrameLengthFrame frameLengthFrame = null; try { streamIdFrame = createStreamIdFrame(byteBufAllocator, streamId, frame); frameLengthFrame = createFrameLengthFrame(byteBufAllocator, streamIdFrame); return frameLengthFrame.mapFrame(byteBuf -> Frame.from(byteBuf.retain())); } finally { disposeQuietly(frame, streamIdFrame, frameLengthFrame); } } }
@Override public String toString() { return "StreamIdFrame{" + "streamId=" + getStreamId() + ", frameWithoutStreamId=" + mapFrameWithoutStreamId(ByteBufUtil::hexDump) + '}'; } }
/** * 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); }
/** * Returns the stream id. * * @return the stream id */ public int getStreamId() { return getByteBuf().getInt(0); }
@DisplayName("createStreamIdFrame throws NullPointerException with null frame") @Test void createStreamIdFrameNullFrame() { assertThatNullPointerException() .isThrownBy(() -> createStreamIdFrame(DEFAULT, 0, null)) .withMessage("frame must not be null"); }
/** * Returns a {@link Tuple2} of the stream id, and the frame. This strips the frame length and * stream id header from the abstraction leaking frame. * * @param abstractionLeakingFrame the abstraction leaking frame * @return a {@link Tuple2} of the stream id, and the frame * @throws NullPointerException if {@code abstractionLeakingFrame} is {@code null} */ public static Tuple2<Integer, io.rsocket.framing.Frame> fromAbstractionLeakingFrame( Frame abstractionLeakingFrame) { Objects.requireNonNull(abstractionLeakingFrame, "abstractionLeakingFrame must not be null"); FrameLengthFrame frameLengthFrame = null; StreamIdFrame streamIdFrame = null; try { frameLengthFrame = createFrameLengthFrame(abstractionLeakingFrame.content()); streamIdFrame = frameLengthFrame.mapFrameWithoutFrameLength(StreamIdFrame::createStreamIdFrame); io.rsocket.framing.Frame frame = streamIdFrame.mapFrameWithoutStreamId(FrameFactory::createFrame); return Tuples.of(streamIdFrame.getStreamId(), frame); } finally { disposeQuietly(frameLengthFrame, streamIdFrame); release(abstractionLeakingFrame); } }
/** * Returns the frame without stream id directly. * * <p><b>Note:</b> this frame without stream id will be outside of the {@link Frame}'s lifecycle * and may be released at any time. It is highly recommended that you {@link ByteBuf#retain()} the * frame without stream id if you store it. * * @return the frame without stream id directly * @see #mapFrameWithoutStreamId(Function) */ public ByteBuf getUnsafeFrameWithoutStreamId() { ByteBuf byteBuf = getByteBuf(); return byteBuf.slice(STREAM_ID_BYTES, byteBuf.readableBytes() - STREAM_ID_BYTES).asReadOnly(); }
@DisplayName("createStreamIdFrame throws NullPointerException with null byteBufAllocator") @Test void createStreamIdFrameNullByteBufAllocator() { assertThatNullPointerException() .isThrownBy(() -> createStreamIdFrame(null, 0, createTestCancelFrame())) .withMessage("byteBufAllocator must not be null"); }
/** * Returns an abstraction leaking frame with the stream id and frame. This adds the frame length * and stream id header to the frame. * * @param byteBufAllocator the {@link ByteBufAllocator} to use * @param streamId the stream id * @param frame the frame * @return an abstraction leaking frame with the stream id and frame * @throws NullPointerException if {@code byteBufAllocator} or {@code frame} is {@code null} */ public static Frame toAbstractionLeakingFrame( ByteBufAllocator byteBufAllocator, int streamId, io.rsocket.framing.Frame frame) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(frame, "frame must not be null"); StreamIdFrame streamIdFrame = null; FrameLengthFrame frameLengthFrame = null; try { streamIdFrame = createStreamIdFrame(byteBufAllocator, streamId, frame); frameLengthFrame = createFrameLengthFrame(byteBufAllocator, streamIdFrame); return frameLengthFrame.mapFrame(byteBuf -> Frame.from(byteBuf.retain())); } finally { disposeQuietly(frame, streamIdFrame, frameLengthFrame); } } }