Java Code Examples for io.rsocket.util.NumberUtils#requireUnsignedShort()

The following examples show how to use io.rsocket.util.NumberUtils#requireUnsignedShort() . 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: 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;
}