Java Code Examples for io.netty.buffer.ByteBufUtil#reserveAndWriteUtf8()

The following examples show how to use io.netty.buffer.ByteBufUtil#reserveAndWriteUtf8() . 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: AuthMetadataCodec.java    From rsocket-java with Apache License 2.0 6 votes vote down vote up
/**
 * Encode a Authentication CompositeMetadata payload using custom authentication type
 *
 * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed.
 * @param customAuthType the custom mime type to encode.
 * @param metadata the metadata value to encode.
 * @throws IllegalArgumentException in case of {@code customAuthType} is non US_ASCII string or
 *     empty string or its length is greater than 128 bytes
 */
public static ByteBuf encodeMetadata(
    ByteBufAllocator allocator, String customAuthType, ByteBuf metadata) {

  int actualASCIILength = ByteBufUtil.utf8Bytes(customAuthType);
  if (actualASCIILength != customAuthType.length()) {
    throw new IllegalArgumentException("custom auth type must be US_ASCII characters only");
  }
  if (actualASCIILength < 1 || actualASCIILength > 128) {
    throw new IllegalArgumentException(
        "custom auth type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128");
  }

  int capacity = 1 + actualASCIILength;
  ByteBuf headerBuffer = allocator.buffer(capacity, capacity);
  // encoded length is one less than actual length, since 0 is never a valid length, which gives
  // wider representation range
  headerBuffer.writeByte(actualASCIILength - 1);

  ByteBufUtil.reserveAndWriteUtf8(headerBuffer, customAuthType, actualASCIILength);

  return allocator.compositeBuffer(2).addComponents(true, headerBuffer, metadata);
}
 
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: 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 8
Source File: NettyBuffer.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Override
public Buffer writeUtf8(CharSequence seq, int ensureWritable) {
    ByteBufUtil.reserveAndWriteUtf8(buffer, seq, ensureWritable);
    return this;
}