下面列出了com.google.common.base.Utf8#encodedLength ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
private static String getSanitizedId(@Nonnull String id) {
try {
if (Utf8.encodedLength(id) > MAX_TR_ID_SIZE) {
if (CharMatcher.ascii().matchesAllOf(id)) {
// Most of the time, the string will be of ascii characters, so return a truncated ID based on length
return id.substring(0, MAX_TR_ID_SIZE - 3) + "...";
} else {
// In theory, we could try and split the UTF-16 string and find a string that fits, but that
// is fraught with peril, not the least of which because one might accidentally split a low
// surrogate/high surrogate pair.
return null;
}
} else {
return id;
}
} catch (IllegalArgumentException e) {
return null;
}
}
/**
* 计算字符串被UTF8编码后的字节数 via guava
*
* @see Utf8#encodedLength(CharSequence)
*/
public static int utf8EncodedLength(@Nullable CharSequence sequence) {
if (StringUtils.isEmpty(sequence)) {
return 0;
}
return Utf8.encodedLength(sequence);
}
/**
* 计算字符串被UTF8编码后的字节数 via guava
*
* @see Utf8#encodedLength(CharSequence)
*/
public static int utf8EncodedLength(@Nullable CharSequence sequence) {
if (StringUtils.isEmpty(sequence)) {
return 0;
}
return Utf8.encodedLength(sequence);
}
/**
* 计算字符串被UTF8编码后的字节数 via guava
*
* @see Utf8#encodedLength(CharSequence)
*/
public static int utf8EncodedLength(@Nullable CharSequence sequence) {
if (StringUtils.isEmpty(sequence)) {
return 0;
}
return Utf8.encodedLength(sequence);
}
public static void sendMessageSplitLarge(PlayerContext ctx, Text text) {
String json = TextSerializers.JSON.serialize(text);
int size = Utf8.encodedLength(json);
if (size > 32767) {
List<Text> lines = ctx.utils().splitLines(text, ctx.width);
ctx.getPlayer().sendMessages(lines);
} else {
ctx.getPlayer().sendMessage(text);
}
}
/**
* Item to show on chat message under this text.
*
* @param item {@link ItemStack}
* @return instance of same {@link UltimateFancy}.
*/
public UltimateFancy hoverShowItem(ItemStack item) {
JSONObject jItem = parseHoverItem(item);
if (Utf8.encodedLength(jItem.toJSONString()) > 32767)
pendentElements.add(new ExtraElement("hoverEvent", parseHoverItem(new ItemStack(item.getType()))));
pendentElements.add(new ExtraElement("hoverEvent", jItem));
return this;
}
private JSONObject parseHoverItem(ItemStack item) {
JSONObject obj = new JSONObject();
obj.put("action", "show_item");
String jItem = convertItemStackToJson(item);
if (Utf8.encodedLength(jItem) > 32767)
obj.put("value", convertItemStackToJson(new ItemStack(item.getType())));
obj.put("value", jItem);
return obj;
}
/**
* Item to show on chat message under this text.
*
* @param item {@link ItemStack}
* @return instance of same {@link UltimateFancy}.
*/
public UltimateFancy hoverShowItem(ItemStack item) {
JSONObject jItem = parseHoverItem(item);
if (Utf8.encodedLength(jItem.toJSONString()) > 32767)
pendentElements.add(new ExtraElement("hoverEvent", parseHoverItem(new ItemStack(item.getType()))));
pendentElements.add(new ExtraElement("hoverEvent", jItem));
return this;
}
private JSONObject parseHoverItem(ItemStack item) {
JSONObject obj = new JSONObject();
obj.put("action", "show_item");
String jItem = convertItemStackToJson(item);
if (Utf8.encodedLength(jItem) > 32767)
obj.put("value", convertItemStackToJson(new ItemStack(item.getType())));
obj.put("value", jItem);
return obj;
}
public static int shortLengthStringSize(@Nullable final String string) {
return Short.BYTES + ((string == null) ? 0 : Utf8.encodedLength(string));
}
@Override
public void read(
final AsnObjectSerializationContext context,
final AsnCharStringBasedObjectCodec instance,
final InputStream inputStream
) throws IOException {
Objects.requireNonNull(context);
Objects.requireNonNull(instance);
Objects.requireNonNull(inputStream);
// WARNING: This length can be maliciously specified by the packet creator, so be careful not to use it for unsafe
// operations, such as creating a new array of initial size `length`. This usage is safe because it merely caps the
// InputStream to the specified packet-length, whereas the InputStream is authoritative for when it actually ends,
// and this limit may be well smaller than `length`.
int lengthToRead;
final AsnSizeConstraint sizeConstraint = instance.getSizeConstraint();
if (sizeConstraint.isFixedSize()) {
lengthToRead = sizeConstraint.getMax();
} else {
// Read the lengthToRead of the encoded OctetString...
lengthToRead = OerLengthSerializer.readLength(inputStream);
}
final String result;
/* beware the 0-lengthToRead string */
if (lengthToRead == 0) {
result = "";
} else {
// Use a limited input stream so we don't read too many bytes.
final InputStream limitedInputStream = ByteStreams.limit(inputStream, lengthToRead);
// WARNING: Don't close the InputStreamReader so that the underlying inputStream is not closed.
result = CharStreams.toString(new InputStreamReader(limitedInputStream, instance.getCharacterSet().name()));
// For UTF-8 characters, result.length() will report the viewable length (e.g., 3) but for certain encoded
// characters, the actual byte-length will be larger (e.g., the String 元元元 is 3 viewable bytes, but 9 encoded
// UTF-8 bytes). Thus, when we write the length-prefix, the code will write 9, so when we read, we need to
// validate that 9 bytes were read, and not 3 (in this example).
if (Utf8.encodedLength(result) != lengthToRead) {
throw new IOException(
format("Unable to properly decode %s bytes (could only read %s bytes)", lengthToRead, result.length())
);
}
}
instance.setCharString(result);
}