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

The following examples show how to use com.google.protobuf.CodedInputStream#readString() . 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: WalletProtobufSerializer.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
 
Example 2
Source File: WalletProtobufSerializer.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
 
Example 3
Source File: WalletProtobufSerializer.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
 
Example 4
Source File: StringMetadataSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public String deserialize(ByteBuffer byteBuffer) {
    CodedInputStream is = CodedInputStream.newInstance(byteBuffer.array());
    try {
        byte type = is.readRawByte();
        if (type == STRING) {
            return is.readString();
        } else {
            throw new IOException("Unexpected first byte. Expected '4' (string). Got '" + type + "'.");
        }
    } catch (IOException e) {
        throw new RuntimeException("IOException during deserialization", e);
    }
}
 
Example 5
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * 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.");
}
 
Example 6
Source File: StringCodec.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public String deserialize(DeserializationContext context, CodedInputStream codedIn)
    throws IOException {
  return codedIn.readString();
}
 
Example 7
Source File: SerializationContextTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public String deserialize(DeserializationContext context, CodedInputStream codedIn)
    throws IOException {
  Preconditions.checkState(!deserializationCalled.getAndSet(true));
  return codedIn.readString();
}
 
Example 8
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;
}