Java Code Examples for com.google.common.base.Utf8#encodedLength()
The following examples show how to use
com.google.common.base.Utf8#encodedLength() .
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: FDBRecordContext.java From fdb-record-layer with Apache License 2.0 | 6 votes |
@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; } }
Example 2
Source File: MoreStringUtil.java From vjtools with Apache License 2.0 | 5 votes |
/** * 计算字符串被UTF8编码后的字节数 via guava * * @see Utf8#encodedLength(CharSequence) */ public static int utf8EncodedLength(@Nullable CharSequence sequence) { if (StringUtils.isEmpty(sequence)) { return 0; } return Utf8.encodedLength(sequence); }
Example 3
Source File: MoreStringUtil.java From vjtools with Apache License 2.0 | 5 votes |
/** * 计算字符串被UTF8编码后的字节数 via guava * * @see Utf8#encodedLength(CharSequence) */ public static int utf8EncodedLength(@Nullable CharSequence sequence) { if (StringUtils.isEmpty(sequence)) { return 0; } return Utf8.encodedLength(sequence); }
Example 4
Source File: MoreStringUtil.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
/** * 计算字符串被UTF8编码后的字节数 via guava * * @see Utf8#encodedLength(CharSequence) */ public static int utf8EncodedLength(@Nullable CharSequence sequence) { if (StringUtils.isEmpty(sequence)) { return 0; } return Utf8.encodedLength(sequence); }
Example 5
Source File: Utils.java From ChatUI with MIT License | 5 votes |
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); } }
Example 6
Source File: UltimateFancy.java From UltimateChat with GNU General Public License v3.0 | 5 votes |
/** * 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; }
Example 7
Source File: UltimateFancy.java From UltimateChat with GNU General Public License v3.0 | 5 votes |
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; }
Example 8
Source File: UltimateFancy.java From RedProtect with GNU General Public License v3.0 | 5 votes |
/** * 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; }
Example 9
Source File: UltimateFancy.java From RedProtect with GNU General Public License v3.0 | 5 votes |
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; }
Example 10
Source File: XodusUtils.java From hivemq-community-edition with Apache License 2.0 | 4 votes |
public static int shortLengthStringSize(@Nullable final String string) { return Short.BYTES + ((string == null) ? 0 : Utf8.encodedLength(string)); }
Example 11
Source File: AsnCharStringOerSerializer.java From quilt with Apache License 2.0 | 4 votes |
@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); }