@DisplayName("getLengthAsUnsignedByte returns length if 255") @Test void getLengthAsUnsignedByte() { assertThat(LengthUtils.getLengthAsUnsignedByte(getRandomByteBuf((1 << 8) - 1))).isEqualTo(255); }
/** * 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; }
.writeShort(requireUnsignedShort(minorVersion)); byteBuf = byteBuf.writeShort(getLengthAsUnsignedShort(resumeIdentificationToken)); byteBuf = Unpooled.wrappedBuffer(
byteBuf = setFlag(byteBuf, FLAG_RESUME_ENABLED) .writeShort(getLengthAsUnsignedShort(resumeIdentificationToken)); byteBuf = Unpooled.wrappedBuffer( byteBuf = byteBuf.writeByte(getLengthAsUnsignedByte(metadataMimeTypeByteBuf)); byteBuf = Unpooled.wrappedBuffer( byteBuf, metadataMimeTypeByteBuf.retain(), byteBufAllocator.buffer()); byteBuf = byteBuf.writeByte(getLengthAsUnsignedByte(dataMimeTypeByteBuf)); byteBuf = Unpooled.wrappedBuffer(byteBuf, dataMimeTypeByteBuf.retain(), byteBufAllocator.buffer());
byteBuf = setFlag(byteBuf, FLAG_RESUME_ENABLED) .writeShort(getLengthAsUnsignedShort(resumeIdentificationToken)); byteBuf = Unpooled.wrappedBuffer( byteBuf = byteBuf.writeByte(getLengthAsUnsignedByte(metadataMimeTypeByteBuf)); byteBuf = Unpooled.wrappedBuffer( byteBuf, metadataMimeTypeByteBuf.retain(), byteBufAllocator.buffer()); byteBuf = byteBuf.writeByte(getLengthAsUnsignedByte(dataMimeTypeByteBuf)); byteBuf = Unpooled.wrappedBuffer(byteBuf, dataMimeTypeByteBuf.retain(), byteBufAllocator.buffer());
/** * 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("getLengthAsUnsignedShort throws NullPointerException with null byteBuf") @Test void getLengthAsUnsignedIntegerNullByteBuf() { assertThatNullPointerException() .isThrownBy(() -> LengthUtils.getLengthAsUnsignedShort(null)) .withMessage("byteBuf must not be null"); }
@DisplayName("getLengthAsUnsignedByte throws NullPointerException with null byteBuf") @Test void getLengthAsUnsignedByteNullByteBuf() { assertThatNullPointerException() .isThrownBy(() -> LengthUtils.getLengthAsUnsignedByte(null)) .withMessage("byteBuf must not be null"); }
@DisplayName("getLengthAsUnsignedMedium throws NullPointerException with null byteBuf") @Test void getLengthAsUnsignedMediumNullByteBuf() { assertThatNullPointerException() .isThrownBy(() -> LengthUtils.getLengthAsUnsignedMedium(null)) .withMessage("byteBuf must not be null"); }
@DisplayName("getLengthAsUnsignedShort throws IllegalArgumentException if larger than 65_535") @Test void getLengthAsUnsignedShortOverFlow() { assertThatIllegalArgumentException() .isThrownBy(() -> LengthUtils.getLengthAsUnsignedShort(getRandomByteBuf(1 << 16))) .withMessage("%d is larger than 16 bits", 1 << 16); } }
@DisplayName("getLengthAsUnsignedByte throws IllegalArgumentException if larger than 255") @Test void getLengthAsUnsignedByteOverFlow() { assertThatIllegalArgumentException() .isThrownBy(() -> LengthUtils.getLengthAsUnsignedByte(getRandomByteBuf(1 << 8))) .withMessage("%d is larger than 8 bits", 1 << 8); }
@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); }
.writeShort(requireUnsignedShort(minorVersion)); byteBuf = byteBuf.writeShort(getLengthAsUnsignedShort(resumeIdentificationToken)); byteBuf = Unpooled.wrappedBuffer(
/** * 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); }