Java Code Examples for com.google.protobuf.CodedInputStream#readRawVarint32()

The following examples show how to use com.google.protobuf.CodedInputStream#readRawVarint32() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FSImageLoader.java    From hadoop with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: FSImageLoader.java    From big-c with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: MapEntryLite.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 4
Source File: RawSerDes.java    From blueflood with Apache License 2.0 6 votes vote down vote up
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));
    }
}
 
Example 5
Source File: DelimitedProtobufHandler.java    From InflatableDonkey with MIT License 5 votes vote down vote up
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;
}
 
Example 6
Source File: ProtoBufArray.java    From LiquidDonkey with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: Main.java    From clarity-examples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 8
Source File: MapEntryLite.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: SetSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: TrackManager.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example 11
Source File: CounterSerDes.java    From blueflood with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 12
Source File: TimerRollupSerDes.java    From blueflood with Apache License 2.0 4 votes vote down vote up
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;
}