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

The following examples show how to use com.google.protobuf.CodedOutputStream#writeRawByte() . 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: SetSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private void serializeSetRollup(BluefloodSetRollup rollup, byte[] buf) throws IOException {
    CodedOutputStream out = CodedOutputStream.newInstance(buf);
    setRollupSize.update(buf.length);
    out.writeRawByte(Constants.VERSION_1_SET_ROLLUP);
    out.writeRawVarint32(rollup.getCount());
    for (Integer i : rollup.getHashes()) {
        out.writeRawVarint32(i);
    }
}
 
Example 8
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 9
Source File: StringMetadataSerDes.java    From blueflood with Apache License 2.0 4 votes vote down vote up
private static void writeToOutputStream(Object obj, CodedOutputStream out) throws IOException {
    out.writeRawByte(STRING);
    out.writeStringNoTag((String)obj);
}
 
Example 10
Source File: BaseRollupSerDes.java    From blueflood with Apache License 2.0 2 votes vote down vote up
/**
 * Serialize without sum attribute.  This is used by {@link GaugeSerDes}
 *
 * @param baseRollup
 * @param protobufOut
 * @throws IOException
 */
protected void serializeRollupV1( BaseRollup baseRollup, CodedOutputStream protobufOut ) throws IOException {
    protobufOut.writeRawByte( Constants.VERSION_1_ROLLUP );
    serializeBaseRollupHelper( baseRollup, protobufOut );
}