当前位置: 首页>>代码示例>>Java>>正文


Java Utf8类代码示例

本文整理汇总了Java中com.google.common.base.Utf8的典型用法代码示例。如果您正苦于以下问题:Java Utf8类的具体用法?Java Utf8怎么用?Java Utf8使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Utf8类属于com.google.common.base包,在下文中一共展示了Utf8类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ReadString

import com.google.common.base.Utf8; //导入依赖的package包/类
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;
}
 
开发者ID:pfn,项目名称:keepassj,代码行数:17,代码来源:ProtectedString.java

示例2: constant

import com.google.common.base.Utf8; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:google,项目名称:closure-templates,代码行数:31,代码来源:BytecodeUtils.java

示例3: utf8EncodedLength

import com.google.common.base.Utf8; //导入依赖的package包/类
/**
 * 计算字符串被UTF8编码后的字节数 via guava
 * 
 * @see Utf8#encodedLength(CharSequence)
 */
public static int utf8EncodedLength(@Nullable CharSequence sequence) {
	if (StringUtils.isEmpty(sequence)) {
		return 0;
	}
	return Utf8.encodedLength(sequence);
}
 
开发者ID:zhangjunfang,项目名称:util,代码行数:12,代码来源:MoreStringUtil.java

示例4: sendMessageSplitLarge

import com.google.common.base.Utf8; //导入依赖的package包/类
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);
    }
}
 
开发者ID:simon816,项目名称:ChatUI,代码行数:11,代码来源:Utils.java

示例5: getEncodedElementByteSize

import com.google.common.base.Utf8; //导入依赖的package包/类
/**
 * {@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;
}
 
开发者ID:apache,项目名称:beam,代码行数:16,代码来源:StringUtf8Coder.java

示例6: representData

import com.google.common.base.Utf8; //导入依赖的package包/类
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, '|');
  }
}
 
开发者ID:bozaro,项目名称:git-lfs-java,代码行数:10,代码来源:YamlRepresenter.java

示例7: asString

import com.google.common.base.Utf8; //导入依赖的package包/类
@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);
  }
}
 
开发者ID:bozaro,项目名称:git-lfs-java,代码行数:9,代码来源:HttpRecord.java

示例8: Init

import com.google.common.base.Utf8; //导入依赖的package包/类
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);
}
 
开发者ID:pfn,项目名称:keepassj,代码行数:12,代码来源:ProtectedString.java

示例9: ReadUtf8

import com.google.common.base.Utf8; //导入依赖的package包/类
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;
}
 
开发者ID:pfn,项目名称:keepassj,代码行数:11,代码来源:ProtectedString.java

示例10: Insert

import com.google.common.base.Utf8; //导入依赖的package包/类
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;
		}
 
开发者ID:pfn,项目名称:keepassj,代码行数:55,代码来源:ProtectedString.java

示例11: Remove

import com.google.common.base.Utf8; //导入依赖的package包/类
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;
		}
 
开发者ID:pfn,项目名称:keepassj,代码行数:52,代码来源:ProtectedString.java

示例12: isValidTag

import com.google.common.base.Utf8; //导入依赖的package包/类
private boolean isValidTag(String tag) {
  return Utf8.encodedLength(tag) <= 23;
}
 
开发者ID:google,项目名称:error-prone,代码行数:4,代码来源:IsLoggableTagLength.java


注:本文中的com.google.common.base.Utf8类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。