/** * Appends metadata to the {@link ByteBuf}. * * @param byteBufAllocator the {@link ByteBufAllocator} to use * @param byteBuf the {@link ByteBuf} to append to * @param metadata the metadata to append * @return the {@link ByteBuf} with metadata appended to it * @throws NullPointerException if {@code byteBuf} is {@code null} */ static ByteBuf appendMetadata( ByteBufAllocator byteBufAllocator, ByteBuf byteBuf, @Nullable ByteBuf metadata) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(byteBuf, "byteBuf must not be null"); if (metadata == null) { return byteBuf.writeMedium(0); } ByteBuf frame = setFlag(byteBuf, FLAG_METADATA).writeMedium(getLengthAsUnsignedMedium(metadata)); frame = Unpooled.wrappedBuffer(frame, metadata.retain(), byteBufAllocator.buffer()); return frame; }
/** * 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("getLengthAsUnsignedMedium throws NullPointerException with null byteBuf") @Test void getLengthAsUnsignedMediumNullByteBuf() { assertThatNullPointerException() .isThrownBy(() -> LengthUtils.getLengthAsUnsignedMedium(null)) .withMessage("byteBuf must not be null"); }
@DisplayName( "getLengthAsUnsignedMedium throws IllegalArgumentException if larger than 16_777_215") @Test void getLengthAsUnsignedMediumOverFlow() { assertThatIllegalArgumentException() .isThrownBy(() -> LengthUtils.getLengthAsUnsignedMedium(getRandomByteBuf(1 << 24))) .withMessage("%d is larger than 24 bits", 1 << 24); }
/** * Appends metadata to the {@link ByteBuf}. * * @param byteBufAllocator the {@link ByteBufAllocator} to use * @param byteBuf the {@link ByteBuf} to append to * @param metadata the metadata to append * @return the {@link ByteBuf} with metadata appended to it * @throws NullPointerException if {@code byteBuf} is {@code null} */ static ByteBuf appendMetadata( ByteBufAllocator byteBufAllocator, ByteBuf byteBuf, @Nullable ByteBuf metadata) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(byteBuf, "byteBuf must not be null"); if (metadata == null) { return byteBuf.writeMedium(0); } ByteBuf frame = setFlag(byteBuf, FLAG_METADATA).writeMedium(getLengthAsUnsignedMedium(metadata)); frame = Unpooled.wrappedBuffer(frame, metadata.retain(), byteBufAllocator.buffer()); return frame; }
/** * 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); }