public String ReadString() { if(m_strPlainText != null) return m_strPlainText; byte[] pb = ReadUtf8(); String str = pb.length == 0 ? "" : StrUtil.Utf8.decode(ByteBuffer.wrap(pb)).toString(); // No need to clear pb // As the text is now visible in process memory anyway, // there's no need to protect it anymore m_strPlainText = str; m_pbUtf8 = null; // Thread-safe order return str; }
/** * Returns an {@link Expression} that can load the given String constant. * * <p>Unlike {@link #constant(String)} this can handle strings larger than 65K bytes. */ public static Expression constant(String value, ClassFieldManager manager) { int encodedLength = Utf8.encodedLength(value); if (encodedLength <= MAX_CONSTANT_STRING_LENGTH) { return stringConstant(value); } // else it is too big for a single constant pool entry so split it into a small number of // entries and generate a static final field to hold the cat'ed value. int startIndex = 0; Expression stringExpression = null; int length = value.length(); do { int endIndex = offsetOf65KUtf8Bytes(value, startIndex, length); // N.B. we may end up splitting the string at a surrogate pair, but the class format uses // modified utf8 which is forgiving about such things. Expression substringConstant = stringConstant(value.substring(startIndex, endIndex)); startIndex = endIndex; if (stringExpression == null) { stringExpression = substringConstant; } else { stringExpression = stringExpression.invoke(MethodRef.STRING_CONCAT, substringConstant); } } while (startIndex < length); FieldRef fieldRef = manager.addStaticField(LARGE_STRING_CONSTANT_NAME, stringExpression); return fieldRef.accessor(); }
/** * 计算字符串被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); } }
/** * {@inheritDoc} * * @return the byte size of the UTF-8 encoding of the a string or, in a nested context, * the byte size of the encoding plus the encoded length prefix. */ @Override public long getEncodedElementByteSize(String value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null String"); } int size = Utf8.encodedLength(value); return VarInt.getLength(size) + size; }
public Node representData(Object data) { byte[] value = (byte[]) data; if (Utf8.isWellFormed(value)) { return representScalar(new Tag("!text"), new String(value, StandardCharsets.UTF_8), DumperOptions.ScalarStyle.LITERAL.getChar()); } else { String binary = Base64Coder.encodeLines((byte[]) data); return representScalar(Tag.BINARY, binary, '|'); } }
@NotNull private static String asString(@NotNull byte[] data) { if (Utf8.isWellFormed(data)) { return new String(data, StandardCharsets.UTF_8); } else { return BaseEncoding.base16().encode(data); } }
private void Init(boolean bEnableProtection, byte[] pbUtf8) { if(pbUtf8 == null) throw new IllegalArgumentException("pbUtf8"); m_bIsProtected = bEnableProtection; if(bEnableProtection) m_pbUtf8 = new ProtectedBinary(true, pbUtf8); else m_strPlainText = new String(pbUtf8, 0, pbUtf8.length, StrUtil.Utf8); }
public byte[] ReadUtf8() { ProtectedBinary pBin = m_pbUtf8; // Local ref for thread-safety if(pBin != null) return pBin.ReadData(); ByteBuffer b = StrUtil.Utf8.encode(m_strPlainText); byte[] buf = new byte[b.remaining()]; b.get(buf); return buf; }
/** Returns an {@link Expression} that can load the given String constant. */ public static Expression constant(final String value) { checkNotNull(value); checkArgument( Utf8.encodedLength(value) <= MAX_CONSTANT_STRING_LENGTH, "String is too long when encoded in utf8"); return stringConstant(value); }
public ProtectedString Insert(int iStart, String strInsert) { if(iStart < 0) throw new ArrayIndexOutOfBoundsException("iStart"); if(strInsert == null) throw new IllegalArgumentException("strInsert"); if(strInsert.length() == 0) return this; // Only operate directly with strings when m_bIsProtected is // false, not in the case of non-null m_strPlainText, because // the operation creates a new sequence in memory if(!m_bIsProtected) return new ProtectedString(false, StrUtil.Insert(ReadString(), iStart, strInsert)); Charset utf8 = StrUtil.Utf8; byte[] pb = ReadUtf8(); CharBuffer cb = utf8.decode(ByteBuffer.wrap(pb)); char[] v = new char[cb.remaining()]; cb.get(v); char[] vNew; try { if(iStart > v.length) throw new ArrayIndexOutOfBoundsException("iStart"); char[] vIns = strInsert.toCharArray(); vNew = new char[v.length + vIns.length]; System.arraycopy(v, 0, vNew, 0, iStart); System.arraycopy(vIns, 0, vNew, iStart, vIns.length); System.arraycopy(v, iStart, vNew, iStart + vIns.length, v.length - iStart); } finally { Arrays.fill(v, (char)0); Arrays.fill(cb.array(), (char)0); MemUtil.ZeroByteArray(pb); } ByteBuffer bb = utf8.encode(CharBuffer.wrap(vNew)); byte[] pbNew = new byte[bb.remaining()]; bb.get(pbNew); ProtectedString ps = new ProtectedString(m_bIsProtected, pbNew); // assert utf8.GetString(pbNew, 0, pbNew.Length) == // ReadString().Insert(iStart, strInsert); Arrays.fill(vNew, (char)0); MemUtil.ZeroByteArray(pbNew); MemUtil.ZeroByteArray(bb.array()); return ps; }
public ProtectedString Remove(int iStart, int nCount) { if(iStart < 0) throw new ArrayIndexOutOfBoundsException("iStart"); if(nCount < 0) throw new ArrayIndexOutOfBoundsException("nCount"); if(nCount == 0) return this; // Only operate directly with strings when m_bIsProtected is // false, not in the case of non-null m_strPlainText, because // the operation creates a new sequence in memory if(!m_bIsProtected) return new ProtectedString(false, StrUtil.Remove(ReadString(), iStart, nCount)); Charset utf8 = StrUtil.Utf8; byte[] pb = ReadUtf8(); CharBuffer cb = utf8.decode(ByteBuffer.wrap(pb)); char[] v = new char[cb.remaining()]; cb.get(v); char[] vNew; try { if((iStart + nCount) > v.length) throw new IllegalArgumentException("iStart + nCount"); vNew = new char[v.length - nCount]; System.arraycopy(v, 0, vNew, 0, iStart); System.arraycopy(v, iStart + nCount, vNew, iStart, v.length - (iStart + nCount)); } finally { Arrays.fill(v, (char)0); Arrays.fill(cb.array(), (char)0); MemUtil.ZeroByteArray(pb); } ByteBuffer bb = utf8.encode(CharBuffer.wrap(vNew)); byte[] pbNew = new byte[bb.remaining()]; bb.get(pbNew); ProtectedString ps = new ProtectedString(m_bIsProtected, pbNew); // assert utf8.GetString(pbNew, 0, pbNew.Length) == // ReadString().Remove(iStart, nCount); Arrays.fill(vNew, (char)0); MemUtil.ZeroByteArray(pbNew); MemUtil.ZeroByteArray(bb.array()); return ps; }
private boolean isValidTag(String tag) { return Utf8.encodedLength(tag) <= 23; }