下面列出了com.google.protobuf.CodedInputStream# readRawByte ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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));
}
}
@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);
}
}
@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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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 );
}
}
}
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);
}
}
protected Number getUnversionedDoubleOrLong(CodedInputStream in) throws IOException {
byte type = in.readRawByte();
if (type == Constants.B_DOUBLE)
return in.readDouble();
else
return in.readRawVarint64();
}
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);
}
}
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);
}
}
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);
}
}
@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);
}
}
@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);
}
}
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;
}