Java Code Examples for com.google.protobuf.CodedOutputStream#writeDoubleNoTag()

The following examples show how to use com.google.protobuf.CodedOutputStream#writeDoubleNoTag() . 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: RawSerDes.java    From blueflood with Apache License 2.0 6 votes vote down vote up
private void serializeFullResMetric(Object obj, byte[] buf) throws IOException {
    CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);

    fullResSize.update(sizeOf(obj));

    protobufOut.writeRawByte(Constants.VERSION_1_FULL_RES);

    if ( obj instanceof Integer ) {
        protobufOut.writeRawByte(Constants.B_I32);
        protobufOut.writeRawVarint32((Integer) obj);
    } else if ( obj instanceof Long ) {
        protobufOut.writeRawByte(Constants.B_I64);
        protobufOut.writeRawVarint64((Long) obj);
    } else if ( obj instanceof Double ) {
        protobufOut.writeRawByte(Constants.B_DOUBLE);
        protobufOut.writeDoubleNoTag((Double) obj);
    } else if ( obj instanceof Float ) {
        protobufOut.writeRawByte(Constants.B_DOUBLE);
        protobufOut.writeDoubleNoTag(((Float) obj).doubleValue());
    } else {
        throw new SerializationException(String.format("Cannot serialize %s", obj.getClass().getName()));
    }
}
 
Example 2
Source File: CounterSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private void serializeCounterRollup(BluefloodCounterRollup rollup, byte[] buf) throws IOException {
    CodedOutputStream out = CodedOutputStream.newInstance(buf);
    counterRollupSize.update(buf.length);
    out.writeRawByte(Constants.VERSION_1_COUNTER_ROLLUP);
    putUnversionedDoubleOrLong(rollup.getCount(), out);
    out.writeDoubleNoTag(rollup.getRate());
    out.writeRawVarint32(rollup.getSampleCount());
}
 
Example 3
Source File: BasicRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
protected void serializeRollup(BasicRollup basicRollup, byte[] buf) throws IOException {
    rollupSize.update(buf.length);
    CodedOutputStream protobufOut = CodedOutputStream.newInstance(buf);
    protobufOut.writeRawByte(Constants.VERSION_2_ROLLUP);

    serializeBaseRollupHelper( basicRollup, protobufOut );

    if (basicRollup.getCount() > 0) {
        protobufOut.writeDoubleNoTag( basicRollup.getSum() );
    }
}
 
Example 4
Source File: TimerRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private void serializeTimer(BluefloodTimerRollup rollup, byte[] buf, byte timerVersion) throws IOException {
    CodedOutputStream out = CodedOutputStream.newInstance(buf);
    timerRollupSize.update(buf.length);
    out.writeRawByte(timerVersion);

    // sum, count, countps, avg, max, min, var
    if (timerVersion == VERSION_1_TIMER) {
        out.writeRawVarint64((long)rollup.getSum());
    } else if (timerVersion == VERSION_2_TIMER) {
        out.writeDoubleNoTag(rollup.getSum());
    } else {
        throw new SerializationException(String.format("Unexpected timer serialization version: %d", (int)timerVersion));
    }

    out.writeRawVarint64(rollup.getCount());
    out.writeDoubleNoTag(rollup.getRate());
    out.writeRawVarint32(rollup.getSampleCount());
    putRollupStat(rollup.getAverage(), out);
    putRollupStat(rollup.getMaxValue(), out);
    putRollupStat(rollup.getMinValue(), out);
    putRollupStat(rollup.getVariance(), out);

    // percentiles.
    Map<String, BluefloodTimerRollup.Percentile> percentiles = rollup.getPercentiles();
    out.writeRawVarint32(percentiles.size());
    for (Map.Entry<String, BluefloodTimerRollup.Percentile> entry : percentiles.entrySet()) {
        out.writeStringNoTag(entry.getKey());
        putUnversionedDoubleOrLong(entry.getValue().getMean(), out);
    }
}
 
Example 5
Source File: AbstractSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
protected void putRollupStat(AbstractRollupStat stat, CodedOutputStream protobufOut) throws IOException {
    protobufOut.writeRawByte(stat.getStatType());   // stat type
    protobufOut.writeRawByte(stat.isFloatingPoint() ? Constants.B_DOUBLE : Constants.B_I64);

    if (stat.isFloatingPoint()) {
        protobufOut.writeDoubleNoTag(stat.toDouble());
    } else {
        protobufOut.writeRawVarint64(stat.toLong());
    }
}
 
Example 6
Source File: AbstractSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
protected void putUnversionedDoubleOrLong(Number number, CodedOutputStream out) throws IOException {
    if (number instanceof Double) {
        out.writeRawByte(Constants.B_DOUBLE);
        out.writeDoubleNoTag(number.doubleValue());
    } else {
        out.writeRawByte(Constants.B_I64);
        out.writeRawVarint64(number.longValue());
    }
}
 
Example 7
Source File: ProtoBufSerialization.java    From eagle with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CodedOutputStream output = CodedOutputStream.newInstance(baos);
    output.writeBoolNoTag(obj == null);
    if (obj == null) {
        output.flush();
        return baos.toByteArray();
    }

    Class<?> clazz = obj.getClass();
    if (clazz == int.class || clazz == Integer.class) {
        output.writeSInt32NoTag((Integer) obj);
    } else if (clazz == long.class || clazz == Long.class) {
        output.writeSInt64NoTag((Long) obj);
    } else if (clazz == boolean.class || clazz == Boolean.class) {
        output.writeBoolNoTag((Boolean) obj);
    } else if (clazz == byte.class || clazz == Byte.class) {
        output.writeRawByte((Byte) obj);
    } else if (clazz == char.class || clazz == Character.class) {
        output.writeSInt32NoTag((Character) obj);
    } else if (clazz == short.class || clazz == Short.class) {
        output.writeSInt32NoTag((Short) obj);
    } else if (clazz == double.class || clazz == Double.class) {
        output.writeDoubleNoTag((Double) obj);
    } else if (clazz == float.class || clazz == Float.class) {
        output.writeFloatNoTag((Float) obj);
    } else if (clazz == String.class) {
        output.writeStringNoTag(obj.toString());
    } else if (MessageLite.class.isAssignableFrom(clazz)) {
        output.writeMessageNoTag((MessageLite) obj);
    } else if (Throwable.class.isAssignableFrom(clazz)) {
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
    } else {
        throw new IllegalArgumentException("can't serialization " + clazz);
    }

    output.flush();
    return baos.toByteArray();
}
 
Example 8
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Write a field of arbitrary type, without its tag, to the stream.
 *
 * @param output The output stream.
 * @param type The field's type.
 * @param value Object representing the field's value. Must be 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 void writeElementNoTag(final CodedOutputStream output, final WireFormat.FieldType type,
        final Object value) throws IOException {
    switch (type) {
        case DOUBLE:
            output.writeDoubleNoTag((Double) value);
            break;
        case FLOAT:
            output.writeFloatNoTag((Float) value);
            break;
        case INT64:
            output.writeInt64NoTag((Long) value);
            break;
        case UINT64:
            output.writeUInt64NoTag((Long) value);
            break;
        case INT32:
            output.writeInt32NoTag((Integer) value);
            break;
        case FIXED64:
            output.writeFixed64NoTag((Long) value);
            break;
        case FIXED32:
            output.writeFixed32NoTag((Integer) value);
            break;
        case BOOL:
            output.writeBoolNoTag((Boolean) value);
            break;
        case STRING:
            output.writeStringNoTag((String) value);
            break;
        // group not support yet
        // case GROUP : output.writeGroupNoTag ((MessageLite) value); break;
        case MESSAGE:
            writeObject(output, 0, FieldType.OBJECT, value, false, false);
            break;
        case BYTES:
            if (value instanceof ByteString) {
                output.writeBytesNoTag((ByteString) value);
            } else {
                byte[] v;
                if (value instanceof Byte[]) {
                    v = toByteArray((Byte[]) value);
                } else {
                    v = (byte[]) value;
                }
                output.writeByteArrayNoTag(v);
            }
            break;
        case UINT32:
            output.writeUInt32NoTag((Integer) value);
            break;
        case SFIXED32:
            output.writeSFixed32NoTag((Integer) value);
            break;
        case SFIXED64:
            output.writeSFixed64NoTag((Long) value);
            break;
        case SINT32:
            output.writeSInt32NoTag((Integer) value);
            break;
        case SINT64:
            output.writeSInt64NoTag((Long) value);
            break;

        case ENUM:
            if (value instanceof Internal.EnumLite) {
                output.writeEnumNoTag(((Internal.EnumLite) value).getNumber());
            } else {

                if (value instanceof EnumReadable) {
                    output.writeEnumNoTag(((EnumReadable) value).value());
                } else if (value instanceof Enum) {
                    output.writeEnumNoTag(((Enum) value).ordinal());
                } else {
                    output.writeEnumNoTag(((Integer) value).intValue());
                }

            }
            break;
    }
}