Java Code Examples for io.rsocket.util.NumberUtils#encodeUnsignedMedium()
The following examples show how to use
io.rsocket.util.NumberUtils#encodeUnsignedMedium() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryRoutingMetadata.java From alibaba-rsocket-broker with Apache License 2.0 | 5 votes |
public ByteBuf getHeaderAndContent() { int capacity = 12; if (this.routingText != null) { capacity = 12 + this.routingText.length; } ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer(capacity, capacity); byteBuf.writeByte(WellKnownMimeType.MESSAGE_RSOCKET_BINARY_ROUTING.getIdentifier() | 0x80); NumberUtils.encodeUnsignedMedium(byteBuf, capacity - 4); byteBuf.writeInt(this.serviceId); byteBuf.writeInt(this.handlerId); if (this.routingText != null) { byteBuf.writeBytes(this.routingText); } return byteBuf; }
Example 2
Source File: CompositeMetadataCodec.java From rsocket-java with Apache License 2.0 | 5 votes |
/** * Encode a custom mime type and a metadata value length into a newly allocated {@link ByteBuf}. * * <p>This larger representation encodes the mime type representation's length on a single byte, * then the representation itself, then the unsigned metadata value length on 3 additional bytes. * * @param allocator the {@link ByteBufAllocator} to use to create the buffer. * @param customMime a custom mime type to encode. * @param metadataLength the metadata length to append to the buffer as an unsigned 24 bits * integer. * @return the encoded mime and metadata length information */ static ByteBuf encodeMetadataHeader( ByteBufAllocator allocator, String customMime, int metadataLength) { ByteBuf metadataHeader = allocator.buffer(4 + customMime.length()); // reserve 1 byte for the customMime length // /!\ careful not to read that first byte, which is random at this point int writerIndexInitial = metadataHeader.writerIndex(); metadataHeader.writerIndex(writerIndexInitial + 1); // write the custom mime in UTF8 but validate it is all ASCII-compatible // (which produces the right result since ASCII chars are still encoded on 1 byte in UTF8) int customMimeLength = ByteBufUtil.writeUtf8(metadataHeader, customMime); if (!ByteBufUtil.isText( metadataHeader, metadataHeader.readerIndex() + 1, customMimeLength, CharsetUtil.US_ASCII)) { metadataHeader.release(); throw new IllegalArgumentException("custom mime type must be US_ASCII characters only"); } if (customMimeLength < 1 || customMimeLength > 128) { metadataHeader.release(); throw new IllegalArgumentException( "custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); } metadataHeader.markWriterIndex(); // go back to beginning and write the length // encoded length is one less than actual length, since 0 is never a valid length, which gives // wider representation range metadataHeader.writerIndex(writerIndexInitial); metadataHeader.writeByte(customMimeLength - 1); // go back to post-mime type and write the metadata content length metadataHeader.resetWriterIndex(); NumberUtils.encodeUnsignedMedium(metadataHeader, metadataLength); return metadataHeader; }
Example 3
Source File: CompositeMetadataTest.java From rsocket-java with Apache License 2.0 | 5 votes |
@Test void decodeEntryTooShortForContentLength() { ByteBuf fakeEntry = Unpooled.buffer(); fakeEntry.writeByte(1); fakeEntry.writeCharSequence("w", CharsetUtil.US_ASCII); NumberUtils.encodeUnsignedMedium(fakeEntry, 456); fakeEntry.writeChar('w'); CompositeMetadata compositeMetadata = new CompositeMetadata(fakeEntry, false); assertThatIllegalStateException() .isThrownBy(() -> compositeMetadata.iterator().next()) .withMessage("metadata is malformed"); }
Example 4
Source File: CompositeMetadataFlyweightTest.java From rsocket-java with Apache License 2.0 | 5 votes |
@Test void decodeEntryTooShortForContentLength() { ByteBuf fakeEntry = Unpooled.buffer(); fakeEntry.writeByte(1); fakeEntry.writeCharSequence("w", CharsetUtil.US_ASCII); NumberUtils.encodeUnsignedMedium(fakeEntry, 456); fakeEntry.writeChar('w'); assertThatIllegalStateException() .isThrownBy(() -> decodeMimeAndContentBuffersSlices(fakeEntry, 0, false)); }
Example 5
Source File: CompositeMetadataCodec.java From rsocket-java with Apache License 2.0 | 3 votes |
/** * Encode a {@link WellKnownMimeType well known mime type} and a metadata value length into a * newly allocated {@link ByteBuf}. * * <p>This compact representation encodes the mime type via its ID on a single byte, and the * unsigned value length on 3 additional bytes. * * @param allocator the {@link ByteBufAllocator} to use to create the buffer. * @param mimeType a byte identifier of a {@link WellKnownMimeType} to encode. * @param metadataLength the metadata length to append to the buffer as an unsigned 24 bits * integer. * @return the encoded mime and metadata length information */ static ByteBuf encodeMetadataHeader( ByteBufAllocator allocator, byte mimeType, int metadataLength) { ByteBuf buffer = allocator.buffer(4, 4).writeByte(mimeType | STREAM_METADATA_KNOWN_MASK); NumberUtils.encodeUnsignedMedium(buffer, metadataLength); return buffer; }