本文整理匯總了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);
}
}