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


Java CodedInputStream.readRawByte方法代码示例

本文整理汇总了Java中com.google.protobuf.CodedInputStream.readRawByte方法的典型用法代码示例。如果您正苦于以下问题:Java CodedInputStream.readRawByte方法的具体用法?Java CodedInputStream.readRawByte怎么用?Java CodedInputStream.readRawByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.protobuf.CodedInputStream的用法示例。


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

示例1: deserialize

import com.google.protobuf.CodedInputStream; //导入方法依赖的package包/类
@Override
public String deserialize(CodedInputStream codedIn) throws IOException {
  int length = codedIn.readInt32();
  if (length == 0) {
    return EMPTY_STRING;
  }

  char[] maybeDecoded = new char[length];
  for (int i = 0; i < length; i++) {
    // Read one byte at a time to avoid creating a new ByteString/copy of the underlying array.
    byte b = codedIn.readRawByte();
    // Check highest order bit, if it's set we've crossed into extended ascii/utf8.
    if ((b & 0x80) == 0) {
      maybeDecoded[i] = (char) b;
    } else {
      // Fail, we encountered a non-ascii byte. Copy what we have so far plus and then the rest
      // of the data into a buffer and let String's constructor do the UTF-8 decoding work.
      byte[] decodeFrom = new byte[length];
      for (int j = 0; j < i; j++) {
        decodeFrom[j] = (byte) maybeDecoded[j];
      }
      decodeFrom[i] = b;
      for (int j = i + 1; j < length; j++) {
        decodeFrom[j] = codedIn.readRawByte();
      }
      return new String(decodeFrom, StandardCharsets.UTF_8);
    }
  }

  try {
    String result = (String) theUnsafe.allocateInstance(String.class);
    theUnsafe.putObject(result, STRING_VALUE_OFFSET, maybeDecoded);
    return result;
  } catch (Exception e) {
    // This should only catch InstantiationException, but that makes IntelliJ unhappy for
    // some reason; it insists that that exception cannot be thrown from here, even though it
    // is set to JDK 8
    throw new IllegalStateException("Could not create string", e);
  }
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:41,代码来源:FastStringCodec.java


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