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

The following examples show how to use com.google.protobuf.CodedInputStream#pushLimit() . 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: ApkFileListUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
private static String getCompiledFileMd5(File file) {
    String md5;
    try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
        CodedInputStream codedInputStream = CodedInputStream.newInstance(is);
        int numFiles = codedInputStream.readRawLittleEndian32();
        // Preconditions.checkState(numFiles == 1, "inline xml not implemented yet");
        long pbSize = codedInputStream.readRawLittleEndian64();
        codedInputStream.pushLimit((int)pbSize);
        CompiledFile compiledFile = CompiledFile.parser().parsePartialFrom(codedInputStream);
        md5 = MD5Util.getFileMD5(compiledFile.getSourcePath());
        // codedInputStream.popLimit((int)pbSize);
        // codedInputStream.pushLimit(0);
        return md5;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 2
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 3
Source File: ReplayLogTest.java    From unitime with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: SessionRestore.java    From unitime with Apache License 2.0 5 votes vote down vote up
public static TableData.Table readTable(CodedInputStream cin) throws IOException {
	if (cin.isAtEnd()) return null;
	int size = cin.readInt32();
	int limit = cin.pushLimit(size);
	TableData.Table ret = TableData.Table.parseFrom(cin);
	cin.popLimit(limit);
	cin.resetSizeCounter();
	return ret;
}
 
Example 5
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 6
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;
}