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

The following examples show how to use com.google.protobuf.CodedInputStream#readRawByte() . 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 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 2
Source File: XProtocolDecoder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeTime(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        boolean negative = inputStream.readRawByte() > 0;
        int hours = 0;
        int minutes = 0;
        int seconds = 0;

        int nanos = 0;

        if (!inputStream.isAtEnd()) {
            hours = (int) inputStream.readInt64();
            if (!inputStream.isAtEnd()) {
                minutes = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    seconds = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        nanos = 1000 * (int) inputStream.readInt64();
                    }
                }
            }
        }

        return vf.createFromTime(negative ? -1 * hours : hours, minutes, seconds, nanos);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 3
Source File: XProtocolDecoder.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public <T> T decodeTime(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        boolean negative = inputStream.readRawByte() > 0;
        int hours = 0;
        int minutes = 0;
        int seconds = 0;

        int nanos = 0;

        if (!inputStream.isAtEnd()) {
            hours = (int) inputStream.readInt64();
            if (!inputStream.isAtEnd()) {
                minutes = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    seconds = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        nanos = 1000 * (int) inputStream.readInt64();
                    }
                }
            }
        }

        return vf.createFromTime(negative ? -1 * hours : hours, minutes, seconds, nanos);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 4
Source File: StatsSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public void deserialize(T stat, CodedInputStream in) throws IOException {

        byte metricValueType = in.readRawByte();
        switch(metricValueType) {
            case Constants.I64:
                stat.setLongValue(in.readRawVarint64());
                break;
            case Constants.B_DOUBLE:
                stat.setDoubleValue(in.readDouble());
                break;
            default:
                throw new IOException("Unsupported stat value type " + (int) metricValueType);
        }
    }
 
Example 5
Source File: CounterSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public BluefloodCounterRollup deserialize(ByteBuffer byteBuffer) {
    CodedInputStream in = CodedInputStream.newInstance(byteBuffer.array());
    try {
        byte version = in.readRawByte();
        if (version != VERSION_1_COUNTER_ROLLUP)
            throw new SerializationException(String.format("Unexpected counter deserialization version: %d", (int)version));
        return deserializeV1CounterRollup(in);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 6
Source File: RawSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public Object deserialize(ByteBuffer byteBuffer) {
    CodedInputStream in = CodedInputStream.newInstance(byteBuffer.array());
    try {
        byte version = in.readRawByte();
        if (version != VERSION_1_FULL_RES && version != VERSION_1_ROLLUP) {
            throw new SerializationException(String.format("Unexpected serialization version: %d",
                                                            (int)version));
        }
        return deserializeSimpleMetric(in);
    } catch (Exception e) {
        throw new RuntimeException("Deserialization Failure", e);
    }
}
 
Example 7
Source File: BasicRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public BasicRollup deserialize(ByteBuffer byteBuffer) {
    CodedInputStream in = CodedInputStream.newInstance(byteBuffer.array());
    try {
        byte version = in.readRawByte();
        if (version != VERSION_1_FULL_RES && version != VERSION_1_ROLLUP && version != VERSION_2_ROLLUP) {
            throw new SerializationException(String.format("Unexpected serialization version: %d",
                    (int)version));
        }

        return deserializeRollup( in, version );

    } catch (Exception e) {
        throw new RuntimeException("Deserialization Failure", e);
    }
}
 
Example 8
Source File: BaseRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
protected void deserializeBaseRollup( BaseRollup baseRollup, CodedInputStream in, byte version ) throws IOException {

        final long count = in.readRawVarint64();
        baseRollup.setCount(count);

        if (count <= 0) {
            return;
        }

        for ( int i = 0; i < BaseRollup.NUM_STATS; i++ ) {
            byte statType = in.readRawByte();
            switch ( statType ) {
                case Constants.AVERAGE:
                    averageStatDeSer.deserialize( baseRollup.getAverage(), in );
                    break;
                case Constants.VARIANCE:
                    varianceStatDeSer.deserialize( baseRollup.getVariance(), in );
                    break;
                case Constants.MIN:
                    minStatDeSer.deserialize( baseRollup.getMinValue(), in );
                    break;
                case Constants.MAX:
                    maxStatDeSer.deserialize( baseRollup.getMaxValue(), in );
                    break;
                default:
                    throw new SerializationException( "invalid stat " + (int) version + " type: " + (int) statType );
            }

        }
    }
 
Example 9
Source File: TimerRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public BluefloodTimerRollup deserialize(ByteBuffer byteBuffer) {
    CodedInputStream in = CodedInputStream.newInstance(byteBuffer.array());
    try {
        byte version = in.readRawByte();
        return deserializeTimer(in, version);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 10
Source File: AbstractSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
protected Number getUnversionedDoubleOrLong(CodedInputStream in) throws IOException {
    byte type = in.readRawByte();
    if (type == Constants.B_DOUBLE)
        return in.readDouble();
    else
        return in.readRawVarint64();
}
 
Example 11
Source File: SetSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public BluefloodSetRollup deserialize(ByteBuffer byteBuffer) {
    CodedInputStream in = CodedInputStream.newInstance(byteBuffer.array());
    try {
        byte version = in.readRawByte();
        if (version != VERSION_1_SET_ROLLUP)
            throw new SerializationException(String.format("Unexpected set serialization version: %d", (int)version));
        return deserializeV1SetRollup(in);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 12
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 13
Source File: GaugeSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public BluefloodGaugeRollup deserialize(ByteBuffer byteBuffer) {
    CodedInputStream in = CodedInputStream.newInstance(byteBuffer.array());
    try {
        byte version = in.readRawByte();
        if (version != VERSION_1_ROLLUP)
            throw new SerializationException(String.format("Unexpected gauge deserialization version: %d", (int)version));
        return deserializeGauge( in, version );
    } catch (Exception e) {
        throw new RuntimeException("Gauge deserialization Failure", e);
    }
}
 
Example 14
Source File: XProtocolDecoder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public <T> T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // packed BCD format (c.f. wikipedia)
        // TODO: optimization possibilities include using int/long if the digits is < X and scale = 0
        byte scale = inputStream.readRawByte();
        // we allocate an extra char for the sign
        CharBuffer unscaledString = CharBuffer.allocate(2 * inputStream.getBytesUntilLimit());
        unscaledString.position(1);
        byte sign = 0;
        // read until we encounter the sign bit
        while (true) {
            int b = 0xFF & inputStream.readRawByte();
            if ((b >> 4) > 9) {
                sign = (byte) (b >> 4);
                break;
            }
            unscaledString.append((char) ((b >> 4) + '0'));
            if ((b & 0x0f) > 9) {
                sign = (byte) (b & 0x0f);
                break;
            }
            unscaledString.append((char) ((b & 0x0f) + '0'));
        }
        if (inputStream.getBytesUntilLimit() > 0) {
            throw AssertionFailedException
                    .shouldNotHappen("Did not read all bytes while decoding decimal. Bytes left: " + inputStream.getBytesUntilLimit());
        }
        switch (sign) {
            case 0xa:
            case 0xc:
            case 0xe:
            case 0xf:
                unscaledString.put(0, '+');
                break;
            case 0xb:
            case 0xd:
                unscaledString.put(0, '-');
                break;
        }
        // may have filled the CharBuffer or one remaining. need to remove it before toString()
        int characters = unscaledString.position();
        unscaledString.clear(); // reset position
        BigInteger unscaled = new BigInteger(unscaledString.subSequence(0, characters).toString());
        return vf.createFromBigDecimal(new BigDecimal(unscaled, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 15
Source File: XProtocolDecoder.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T> T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // packed BCD format (c.f. wikipedia)
        // TODO: optimization possibilities include using int/long if the digits is < X and scale = 0
        byte scale = inputStream.readRawByte();
        // we allocate an extra char for the sign
        CharBuffer unscaledString = CharBuffer.allocate(2 * inputStream.getBytesUntilLimit());
        unscaledString.position(1);
        byte sign = 0;
        // read until we encounter the sign bit
        while (true) {
            int b = 0xFF & inputStream.readRawByte();
            if ((b >> 4) > 9) {
                sign = (byte) (b >> 4);
                break;
            }
            unscaledString.append((char) ((b >> 4) + '0'));
            if ((b & 0x0f) > 9) {
                sign = (byte) (b & 0x0f);
                break;
            }
            unscaledString.append((char) ((b & 0x0f) + '0'));
        }
        if (inputStream.getBytesUntilLimit() > 0) {
            throw AssertionFailedException
                    .shouldNotHappen("Did not read all bytes while decoding decimal. Bytes left: " + inputStream.getBytesUntilLimit());
        }
        switch (sign) {
            case 0xa:
            case 0xc:
            case 0xe:
            case 0xf:
                unscaledString.put(0, '+');
                break;
            case 0xb:
            case 0xd:
                unscaledString.put(0, '-');
                break;
        }
        // may have filled the CharBuffer or one remaining. need to remove it before toString()
        int characters = unscaledString.position();
        unscaledString.clear(); // reset position
        BigInteger unscaled = new BigInteger(unscaledString.subSequence(0, characters).toString());
        return vf.createFromBigDecimal(new BigDecimal(unscaled, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 16
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;
}