io.rsocket.util.NumberUtils Java Examples

The following examples show how to use io.rsocket.util.NumberUtils. 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: Tracing.java    From rsocket-rpc-java with Apache License 2.0 6 votes vote down vote up
public static ByteBuf mapToByteBuf(ByteBufAllocator allocator, Map<String, String> map) {
  if (map == null || map.isEmpty()) {
    return Unpooled.EMPTY_BUFFER;
  }

  ByteBuf byteBuf = allocator.buffer();

  for (Map.Entry<String, String> entry : map.entrySet()) {
    String key = entry.getKey();
    int keyLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(key));
    byteBuf.writeShort(keyLength);
    ByteBufUtil.reserveAndWriteUtf8(byteBuf, key, keyLength);

    String value = entry.getValue();
    int valueLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(value));
    byteBuf.writeShort(valueLength);
    ByteBufUtil.reserveAndWriteUtf8(byteBuf, value, keyLength);
  }

  return byteBuf;
}
 
Example #2
Source File: Metadata.java    From rsocket-rpc-java with Apache License 2.0 6 votes vote down vote up
public static ByteBuf encode(
    ByteBufAllocator allocator,
    String service,
    String method,
    ByteBuf tracing,
    ByteBuf metadata) {
  ByteBuf byteBuf = allocator.buffer().writeShort(VERSION);

  int serviceLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(service));
  byteBuf.writeShort(serviceLength);
  ByteBufUtil.reserveAndWriteUtf8(byteBuf, service, serviceLength);

  int methodLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(method));
  byteBuf.writeShort(methodLength);
  ByteBufUtil.reserveAndWriteUtf8(byteBuf, method, methodLength);

  byteBuf.writeShort(tracing.readableBytes());
  byteBuf.writeBytes(tracing, tracing.readerIndex(), tracing.readableBytes());

  byteBuf.writeBytes(metadata, metadata.readerIndex(), metadata.readableBytes());

  return byteBuf;
}
 
Example #3
Source File: Tracing.java    From rsocket-rpc-java with Apache License 2.0 6 votes vote down vote up
public static ByteBuf mapToByteBuf(ByteBufAllocator allocator, Map<String, String> map) {
  if (map == null || map.isEmpty()) {
    return Unpooled.EMPTY_BUFFER;
  }

  ByteBuf byteBuf = allocator.buffer();

  for (Map.Entry<String, String> entry : map.entrySet()) {
    String key = entry.getKey();
    int keyLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(key));
    byteBuf.writeShort(keyLength);
    ByteBufUtil.reserveAndWriteUtf8(byteBuf, key, keyLength);

    String value = entry.getValue();
    int valueLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(value));
    byteBuf.writeShort(valueLength);
    ByteBufUtil.reserveAndWriteUtf8(byteBuf, value, keyLength);
  }

  return byteBuf;
}
 
Example #4
Source File: Metadata.java    From rsocket-rpc-java with Apache License 2.0 6 votes vote down vote up
public static ByteBuf encode(
    ByteBufAllocator allocator,
    String service,
    String method,
    ByteBuf tracing,
    ByteBuf metadata) {
  ByteBuf byteBuf = allocator.buffer().writeShort(VERSION);

  int serviceLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(service));
  byteBuf.writeShort(serviceLength);
  ByteBufUtil.reserveAndWriteUtf8(byteBuf, service, serviceLength);

  int methodLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(method));
  byteBuf.writeShort(methodLength);
  ByteBufUtil.reserveAndWriteUtf8(byteBuf, method, methodLength);

  byteBuf.writeShort(tracing.readableBytes());
  byteBuf.writeBytes(tracing, tracing.readerIndex(), tracing.readableBytes());

  byteBuf.writeBytes(metadata, metadata.readerIndex(), metadata.readableBytes());

  return byteBuf;
}
 
Example #5
Source File: BinaryRoutingMetadata.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
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 #6
Source File: Tracing.java    From rsocket-rpc-java with Apache License 2.0 5 votes vote down vote up
public static ByteBuf mapToByteBuf(ByteBufAllocator allocator, SpanContext spanContext) {
  if (spanContext == null) {
    return Unpooled.EMPTY_BUFFER;
  }
  Iterator<Map.Entry<String, String>> iterator = spanContext.baggageItems().iterator();

  if (!iterator.hasNext()) {
    return Unpooled.EMPTY_BUFFER;
  }

  ByteBuf byteBuf = allocator.buffer();

  do {
    final Map.Entry<String, String> entry = iterator.next();
    String key = entry.getKey();
    int keyLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(key));
    byteBuf.writeShort(keyLength);
    ByteBufUtil.reserveAndWriteUtf8(byteBuf, key, keyLength);

    String value = entry.getValue();
    int valueLength = NumberUtils.requireUnsignedShort(ByteBufUtil.utf8Bytes(value));
    byteBuf.writeShort(valueLength);
    ByteBufUtil.reserveAndWriteUtf8(byteBuf, value, keyLength);
  } while (iterator.hasNext());

  return byteBuf;
}
 
Example #7
Source File: CompositeMetadataCodec.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #8
Source File: CompositeMetadataTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: CompositeMetadataFlyweightTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: TagsMetadata.java    From spring-cloud-rsocket with Apache License 2.0 4 votes vote down vote up
protected static void encodeString(ByteBuf byteBuf, String s) {
	int length = NumberUtils.requireUnsignedByte(ByteBufUtil.utf8Bytes(s));
	byteBuf.writeByte(length);
	ByteBufUtil.reserveAndWriteUtf8(byteBuf, s, length);
}
 
Example #11
Source File: CompositeMetadataCodec.java    From rsocket-java with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}