下面列出了com.google.protobuf.CodedInputStream# readRawVarint32 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static byte[][] loadINodeSection(InputStream in)
throws IOException {
FsImageProto.INodeSection s = FsImageProto.INodeSection
.parseDelimitedFrom(in);
LOG.info("Loading " + s.getNumInodes() + " inodes.");
final byte[][] inodes = new byte[(int) s.getNumInodes()][];
for (int i = 0; i < s.getNumInodes(); ++i) {
int size = CodedInputStream.readRawVarint32(in.read(), in);
byte[] bytes = new byte[size];
IOUtils.readFully(in, bytes, 0, size);
inodes[i] = bytes;
}
LOG.debug("Sorting inodes");
Arrays.sort(inodes, INODE_BYTES_COMPARATOR);
LOG.debug("Finished sorting inodes");
return inodes;
}
private static byte[][] loadINodeSection(InputStream in)
throws IOException {
FsImageProto.INodeSection s = FsImageProto.INodeSection
.parseDelimitedFrom(in);
LOG.info("Loading " + s.getNumInodes() + " inodes.");
final byte[][] inodes = new byte[(int) s.getNumInodes()][];
for (int i = 0; i < s.getNumInodes(); ++i) {
int size = CodedInputStream.readRawVarint32(in.read(), in);
byte[] bytes = new byte[size];
IOUtils.readFully(in, bytes, 0, size);
inodes[i] = bytes;
}
LOG.debug("Sorting inodes");
Arrays.sort(inodes, INODE_BYTES_COMPARATOR);
LOG.debug("Finished sorting inodes");
return inodes;
}
/**
* Parses the field.
*
* @param <T> the generic type
* @param input the input
* @param extensionRegistry the extension registry
* @param type the type
* @param value the value
* @return the t
* @throws IOException Signals that an I/O exception has occurred.
*/
@SuppressWarnings("unchecked")
static <T> T parseField(CodedInputStream input, ExtensionRegistryLite extensionRegistry, WireFormat.FieldType type,
T value) throws IOException {
switch (type) {
case MESSAGE:
int length = input.readRawVarint32();
final int oldLimit = input.pushLimit(length);
Codec<? extends Object> codec = ProtobufProxy.create(value.getClass());
T ret = (T) codec.decode(input.readRawBytes(length));
input.popLimit(oldLimit);
return ret;
case ENUM:
return (T) (java.lang.Integer) input.readEnum();
case GROUP:
throw new RuntimeException("Groups are not allowed in maps.");
default:
return (T) CodedConstant.readPrimitiveField(input, type, true);
}
}
private Object deserializeSimpleMetric(CodedInputStream in) throws IOException {
byte metricValueType = in.readRawByte() /* type field */;
switch (metricValueType) {
case Constants.I32:
return in.readRawVarint32();
case Constants.I64:
return in.readRawVarint64();
case Constants.DOUBLE:
return in.readDouble();
case Constants.STR:
throw new UnexpectedStringSerializationException("We don't rollup strings");
default:
throw new SerializationException(String.format("Unexpected raw metric type=%s for full res " +
"metric", (char)metricValueType));
}
}
List<T> apply(InputStream in) throws IOException {
List<T> list = new ArrayList<>();
CodedInputStream stream = CodedInputStream.newInstance(in);
int size;
while (!stream.isAtEnd() && (size = stream.readRawVarint32()) != 0) {
ByteArrayInputStream delimited = new ByteArrayInputStream(stream.readRawBytes(size));
list.add(parse.apply(delimited));
}
return list;
}
/**
* Decode custom protobuf variable length array.
*
* @param <T> the item type
* @param data the raw input, not null
* @param parser the parser to decode each message, not null
* @return the decoded list of items, not null
* @throws IOException
* @throws NullPointerException if any arguments are null
*/
public static <T> List<T> decode(InputStream data, Parser<T> parser) throws IOException {
CodedInputStream stream = CodedInputStream.newInstance(data);
List<T> list = new ArrayList<>();
while (!stream.isAtEnd()) {
int size = stream.readRawVarint32();
byte[] element = stream.readRawBytes(size);
T decoded = parser.parseFrom(element);
list.add(decoded);
}
return list;
}
@OnMessage(Demo.CDemoSendTables.class)
public void onSendTables(Context ctx, Demo.CDemoSendTables message) throws IOException {
CodedInputStream cis = CodedInputStream.newInstance(ZeroCopy.extract(message.getData()));
int size = cis.readRawVarint32();
S2NetMessages.CSVCMsg_FlattenedSerializer fs = Packet.parse(S2NetMessages.CSVCMsg_FlattenedSerializer.class, ZeroCopy.wrap(cis.readRawBytes(size)));
Set<String> baseTypes = new TreeSet<>();
for (S2NetMessages.ProtoFlattenedSerializer_t s : fs.getSerializersList()) {
for (int fi : s.getFieldsIndexList()) {
S2NetMessages.ProtoFlattenedSerializerField_t f = fs.getFields(fi);
FieldType ft = new FieldType(fs.getSymbols(f.getVarTypeSym()));
if (!f.hasFieldSerializerNameSym()) {
int l = 0;
do {
baseTypes.add(ft.getBaseType().toUpperCase());
if ("CUTLVECTOR".equals(ft.getBaseType().toUpperCase())) {
ft = ft.getGenericType();
} else {
ft = null;
}
l++;
} while (l <= 1 && ft != null);
}
}
}
dump(ctx, fs);
}
/**
* Parses an entry off of the input into the map. This helper avoids allocaton of a {@link MapEntryLite} by parsing
* directly into the provided {@link MapFieldLite}.
*
* @param map the map
* @param input the input
* @param extensionRegistry the extension registry
* @throws IOException Signals that an I/O exception has occurred.
*/
public void parseInto(MapFieldLite<K, V> map, CodedInputStream input, ExtensionRegistryLite extensionRegistry)
throws IOException {
int length = input.readRawVarint32();
final int oldLimit = input.pushLimit(length);
K key = metadata.defaultKey;
V value = metadata.defaultValue;
while (true) {
int tag = input.readTag();
if (tag == 0) {
break;
}
if (tag == CodedConstant.makeTag(KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
key = parseField(input, extensionRegistry, metadata.keyType, key);
} else if (tag == CodedConstant.makeTag(VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
value = parseField(input, extensionRegistry, metadata.valueType, value);
} else {
if (!input.skipField(tag)) {
break;
}
}
}
input.checkLastTagWas(0);
input.popLimit(oldLimit);
map.put(key, value);
}
private BluefloodSetRollup deserializeV1SetRollup(CodedInputStream in) throws IOException {
int count = in.readRawVarint32();
BluefloodSetRollup rollup = new BluefloodSetRollup();
while (count-- > 0) {
rollup = rollup.withObject(in.readRawVarint32());
}
return rollup;
}
@NonNull
@Override
public FileDataSource loadData(InputStream inputStream, String filePath) throws Exception {
long propertiesOffset = 0L;
Track track = new Track();
CodedInputStream input = CodedInputStream.newInstance(inputStream);
boolean done = false;
while (!done) {
long offset = input.getTotalBytesRead();
int tag = input.readTag();
int field = WireFormat.getTagFieldNumber(tag);
switch (field) {
case 0:
done = true;
break;
default: {
throw new com.google.protobuf.InvalidProtocolBufferException("Unsupported proto field: " + tag);
}
case FIELD_VERSION: {
// skip version
input.skipField(tag);
break;
}
case FIELD_POINT: {
int length = input.readRawVarint32();
int oldLimit = input.pushLimit(length);
readPoint(track, input);
input.popLimit(oldLimit);
input.checkLastTagWas(0);
break;
}
case FIELD_NAME: {
propertiesOffset = offset;
track.name = input.readBytes().toStringUtf8();
break;
}
case FIELD_COLOR: {
track.style.color = input.readUInt32();
break;
}
case FIELD_WIDTH: {
track.style.width = input.readFloat();
break;
}
}
}
inputStream.close();
track.id = 31 * filePath.hashCode() + 1;
FileDataSource dataSource = new FileDataSource();
dataSource.name = track.name;
dataSource.tracks.add(track);
track.source = dataSource;
dataSource.propertiesOffset = propertiesOffset;
return dataSource;
}
private BluefloodCounterRollup deserializeV1CounterRollup(CodedInputStream in) throws IOException {
Number value = getUnversionedDoubleOrLong(in);
double rate = in.readDouble();
int sampleCount = in.readRawVarint32();
return new BluefloodCounterRollup().withCount(value.longValue()).withRate(rate).withSampleCount(sampleCount);
}
private BluefloodTimerRollup deserializeTimer(CodedInputStream in, byte timerVersion) throws IOException {
// note: type and version have already been read.
final double sum;
if (timerVersion == VERSION_1_TIMER) {
sum = in.readRawVarint64();
} else if (timerVersion == VERSION_2_TIMER) {
sum = in.readDouble();
} else {
throw new SerializationException(String.format("Unexpected timer deserialization version: %d", (int)timerVersion));
}
final long count = in.readRawVarint64();
final double countPs = in.readDouble();
final int sampleCount = in.readRawVarint32();
// average
byte statType = in.readRawByte();
Average average = new Average();
averageStatDeSer.deserialize(average, in);
// max
statType = in.readRawByte();
MaxValue maxValue = new MaxValue();
maxStatDeSer.deserialize(maxValue, in);
// min
statType = in.readRawByte();
MinValue minValue = new MinValue();
minStatDeSer.deserialize(minValue, in);
// var
statType = in.readRawByte();
Variance variance = new Variance();
varianceStatDeSer.deserialize(variance, in);
BluefloodTimerRollup rollup = new BluefloodTimerRollup()
.withSum(sum)
.withCount(count)
.withCountPS(countPs)
.withSampleCount(sampleCount)
.withAverage(average)
.withMaxValue(maxValue)
.withMinValue(minValue)
.withVariance(variance);
int numPercentiles = in.readRawVarint32();
for (int i = 0; i < numPercentiles; i++) {
String name = in.readString();
Number mean = getUnversionedDoubleOrLong(in);
rollup.setPercentile(name, mean);
}
return rollup;
}