下面列出了com.google.protobuf.CodedInputStream# readInt32 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private <T> T deserializeMemoAfterContent(
DeserializationContext context, ObjectCodec<T> codec, CodedInputStream codedIn)
throws SerializationException, IOException {
T value = castedDeserialize(context, codec, codedIn);
int id = codedIn.readInt32();
// If deserializing the children caused the parent object itself to be deserialized due to
// a cycle, then there's now a memo entry for the parent. Reuse that object, discarding
// the one we were trying to construct here, so as to avoid creating duplicate objects in
// the object graph.
Object cyclicallyCreatedObject = memo.lookup(id);
if (cyclicallyCreatedObject != null) {
return safeCast(cyclicallyCreatedObject, codec);
} else {
memo.memoize(id, value);
return value;
}
}
@Override
public ImmutableBiMap<K, V> deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
int length = codedIn.readInt32();
if (length < 0) {
throw new SerializationException("Expected non-negative length: " + length);
}
ImmutableBiMap.Builder<K, V> builder =
ImmutableMapCodec.deserializeEntries(
ImmutableBiMap.builderWithExpectedSize(length), length, context, codedIn);
try {
return builder.build();
} catch (IllegalArgumentException e) {
throw new SerializationException(
"Duplicate keys during ImmutableBiMapCodec deserialization", e);
}
}
@Override
public Method deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
Class<?> clazz = context.deserialize(codedIn);
String name = context.deserialize(codedIn);
Class<?>[] parameters = new Class<?>[codedIn.readInt32()];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = context.deserialize(codedIn);
}
try {
return clazz.getDeclaredMethod(name, parameters);
} catch (NoSuchMethodException e) {
throw new SerializationException(
"Couldn't get method "
+ name
+ " in "
+ clazz
+ " with parameters "
+ Arrays.toString(parameters),
e);
}
}
@Override
public ImmutableMultimap<K, V> deserialize(
DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
ImmutableMultimap.Builder<K, V> result;
if (codedIn.readBool()) {
result = ImmutableListMultimap.builder();
} else {
result = ImmutableSetMultimap.builder();
}
int length = codedIn.readInt32();
for (int i = 0; i < length; i++) {
K key = context.deserialize(codedIn);
Collection<V> values = context.deserialize(codedIn);
result.putAll(key, values);
}
return result.build();
}
@Override
public ImmutableTable<R, C, V> deserialize(
DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
int length = codedIn.readInt32();
if (length < 0) {
throw new SerializationException("Expected non-negative length: " + length);
}
ImmutableTable.Builder<R, C, V> builder = ImmutableTable.builder();
for (int i = 0; i < length; i++) {
builder.put(
/*rowKey=*/ context.deserialize(codedIn),
/*columnKey=*/ context.deserialize(codedIn),
/*value=*/ context.deserialize(codedIn));
}
return builder.build();
}
/**
* Parses a {@link HyperLogLogPlusUniqueStateProto} message. Since the message is nested within an
* {@link AggregatorStateProto}, we limit ourselves to reading only the bytes of the specified
* message length.
*/
private void parseHll(CodedInputStream input, int size) throws IOException {
int limit = input.getTotalBytesRead() + size;
ByteBuffer buffer;
UnknownFieldSet.Builder ignoredFields = UnknownFieldSet.newBuilder();
while (input.getTotalBytesRead() < limit && !input.isAtEnd()) {
int tag = input.readTag();
switch (tag) {
case SPARSE_SIZE_TAG:
sparseSize = input.readInt32();
break;
case PRECISION_OR_NUM_BUCKETS_TAG:
precision = input.readInt32();
break;
case SPARSE_PRECISION_OR_NUM_BUCKETS_TAG:
sparsePrecision = input.readInt32();
break;
case DATA_TAG:
buffer = input.readByteBuffer();
data = ByteSlice.copyOnWrite(buffer);
break;
case SPARSE_DATA_TAG:
buffer = input.readByteBuffer();
sparseData = GrowingByteSlice.copyOnWrite(buffer);
break;
default:
ignoredFields.mergeFieldFrom(tag, input);
}
}
}
private OnlineSectioningLog.ExportedLog readLog(CodedInputStream cin) throws IOException {
if (cin.isAtEnd()) return null;
int size = cin.readInt32();
int limit = cin.pushLimit(size);
OnlineSectioningLog.ExportedLog ret = OnlineSectioningLog.ExportedLog.parseFrom(cin);
cin.popLimit(limit);
cin.resetSizeCounter();
return ret;
}
@Override
public ImmutableList<Object> deserialize(
DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
context = context.getMemoizingContext();
int size = codedIn.readInt32();
ImmutableList.Builder<Object> builder = ImmutableList.builder();
for (int i = 0; i < size; i++) {
builder.add(context.<Object>deserialize(codedIn));
}
return builder.build();
}
@Override
public StringLiteral deserialize(DeserializationContext context, CodedInputStream in)
throws SerializationException, IOException {
String value =
context.deserializeWithAdHocMemoizationStrategy(in, MemoizationStrategy.MEMOIZE_AFTER);
int startOffset = in.readInt32();
int endOffset = in.readInt32();
FileLocations locs = context.deserialize(in);
return new StringLiteral(locs, startOffset, value, endOffset);
}
@Override
public ImmutableList<T> deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
int length = codedIn.readInt32();
if (length < 0) {
throw new SerializationException("Expected non-negative length: " + length);
}
ImmutableList.Builder<T> builder = ImmutableList.builder();
for (int i = 0; i < length; i++) {
builder.add(codec.deserialize(context, codedIn));
}
return builder.build();
}
@Override
public LinkedHashMap<K, V> deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
int size = codedIn.readInt32();
// Load factor is 0.75, so we need an initial capacity of 4/3 actual size to avoid rehashing.
LinkedHashMap<K, V> result = new LinkedHashMap<>(4 * size / 3);
for (int i = 0; i < size; i++) {
result.put(context.deserialize(codedIn), context.deserialize(codedIn));
}
return result;
}
@Override
public ArrayList<?> deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
context = context.getMemoizingContext();
int size = codedIn.readInt32();
ArrayList<?> result = new ArrayList<>();
for (int i = 0; i < size; i++) {
result.add(context.deserialize(codedIn));
}
return result;
}
@Override
public EnumMap<E, V> deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
int size = codedIn.readInt32();
Class<E> clazz = context.deserialize(codedIn);
EnumMap<E, V> result = new EnumMap<>(clazz);
E[] enums = clazz.getEnumConstants();
for (int i = 0; i < size; i++) {
int ordinal = codedIn.readInt32();
V val = context.deserialize(codedIn);
result.put(enums[ordinal], val);
}
return result;
}
@Override
public List<T> deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
int length = codedIn.readInt32();
if (length < 0) {
throw new SerializationException("Expected non-negative length: " + length);
}
ArrayList<T> list = new ArrayList<>(length);
for (int i = 0; i < length; i++) {
list.add(context.deserialize(codedIn));
}
return maybeTransform(list);
}
@Override
public ImmutableList deserialize(DeserializationContext context, CodedInputStream codedIn)
throws IOException, SerializationException {
int size = codedIn.readInt32();
Object[] list = new Object[size];
for (int i = 0; i < size; ++i) {
list[i] = context.deserialize(codedIn);
}
return ImmutableList.copyOf(list);
}
@SuppressWarnings("unchecked") // Adding object to untyped builder.
@Override
public ImmutableSet deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
int size = codedIn.readInt32();
ImmutableSet.Builder builder = ImmutableSet.builderWithExpectedSize(size);
for (int i = 0; i < size; i++) {
// Don't inline so builder knows this is an object, not an array.
Object item = context.deserialize(codedIn);
builder.add(item);
}
return builder.build();
}
public static ArrayList<ByteString> extractWireFormatTxOut(Transaction tx)
throws ValidationException
{
try
{
CodedInputStream code_in = CodedInputStream.newInstance(tx.getInnerData().toByteArray());
ArrayList<ByteString> lst = new ArrayList<>();
while(true)
{
int tag = code_in.readTag();
if (tag == 0) break;
// The least signficiate 3 bits are the proto field type
// so shift to get out field number, which is 5 for TranasctionOutput
if (tag >> 3 == 5)
{
ByteArrayOutputStream b_out = new ByteArrayOutputStream();
CodedOutputStream c_out = CodedOutputStream.newInstance(b_out);
code_in.skipField(tag, c_out);
c_out.flush();
ByteString bs = ByteString.copyFrom(b_out.toByteArray());
// So funny story...when you get an inner message like this as opposed to just serializing
// the object, protobuf puts a tag and size on the coded input stream. So we need to figure
// out how many bytes that is an trim it off.
CodedInputStream read_again = CodedInputStream.newInstance(bs.toByteArray());
// Expected tag
int tag2 = read_again.readTag();
// Size of element
int size = read_again.readInt32();
// All we really care is how many bytes those two take. For shorter messages
// it will be 2, but could be higher if protobuf needs more bytes to encode the size
int offset = read_again.getTotalBytesRead();
bs = bs.substring(offset);
lst.add(bs);
}
else
{
code_in.skipField(tag);
}
}
return lst;
}
catch(java.io.IOException e)
{
throw new ValidationException(e);
}
}
@Override
public Integer deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
return codedIn.readInt32();
}
/**
* Read a field of any primitive type for immutable messages from a CodedInputStream. Enums, groups, and embedded
* messages are not handled by this method.
*
* @param input The stream from which to read.
* @param type Declared type of the field.
* @param checkUtf8 When true, check that the input is valid utf8.
* @return An object representing the field's value, of the exact type which would be returned by
* {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
* @throws IOException Signals that an I/O exception has occurred.
*/
public static Object readPrimitiveField(CodedInputStream input, final WireFormat.FieldType type, boolean checkUtf8)
throws IOException {
switch (type) {
case DOUBLE:
return input.readDouble();
case FLOAT:
return input.readFloat();
case INT64:
return input.readInt64();
case UINT64:
return input.readUInt64();
case INT32:
return input.readInt32();
case FIXED64:
return input.readFixed64();
case FIXED32:
return input.readFixed32();
case BOOL:
return input.readBool();
case STRING:
if (checkUtf8) {
return input.readStringRequireUtf8();
} else {
return input.readString();
}
case BYTES:
return input.readByteArray();
case UINT32:
return input.readUInt32();
case SFIXED32:
return input.readSFixed32();
case SFIXED64:
return input.readSFixed64();
case SINT32:
return input.readSInt32();
case SINT64:
return input.readSInt64();
case GROUP:
throw new IllegalArgumentException("readPrimitiveField() cannot handle nested groups.");
case MESSAGE:
throw new IllegalArgumentException("readPrimitiveField() cannot handle embedded messages.");
case ENUM:
// We don't handle enums because we don't know what to do if the
// value is not recognized.
throw new IllegalArgumentException("readPrimitiveField() cannot handle enums.");
}
throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise.");
}
@Override
public Integer deserialize(DeserializationContext context, CodedInputStream codedIn)
throws IOException {
return codedIn.readInt32();
}