下面列出了io.netty.buffer.ByteBufUtil#reserveAndWriteUtf8 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
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;
}
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;
}
/**
* 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);
}
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;
}
protected static void encodeString(ByteBuf byteBuf, String s) {
int length = NumberUtils.requireUnsignedByte(ByteBufUtil.utf8Bytes(s));
byteBuf.writeByte(length);
ByteBufUtil.reserveAndWriteUtf8(byteBuf, s, length);
}
@Override
public Buffer writeUtf8(CharSequence seq, int ensureWritable) {
ByteBufUtil.reserveAndWriteUtf8(buffer, seq, ensureWritable);
return this;
}