Java Code Examples for com.google.protobuf.CodedOutputStream#writeRawVarint32()
The following examples show how to use
com.google.protobuf.CodedOutputStream#writeRawVarint32() .
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 |
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: TestProtoUtil.java From hadoop with Apache License 2.0 | 5 votes |
private void doVarIntTest(int value) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); CodedOutputStream cout = CodedOutputStream.newInstance(baos); cout.writeRawVarint32(value); cout.flush(); DataInputStream dis = new DataInputStream( new ByteArrayInputStream(baos.toByteArray())); assertEquals(value, ProtoUtil.readRawVarint32(dis)); }
Example 3
Source File: TestProtoUtil.java From big-c with Apache License 2.0 | 5 votes |
private void doVarIntTest(int value) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); CodedOutputStream cout = CodedOutputStream.newInstance(baos); cout.writeRawVarint32(value); cout.flush(); DataInputStream dis = new DataInputStream( new ByteArrayInputStream(baos.toByteArray())); assertEquals(value, ProtoUtil.readRawVarint32(dis)); }
Example 4
Source File: ProtobufVarint32LengthFieldPrepender.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Override protected void encode( ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { int bodyLen = msg.readableBytes(); int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen); out.ensureWritable(headerLen + bodyLen); CodedOutputStream headerOut = CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLen); headerOut.writeRawVarint32(bodyLen); headerOut.flush(); out.writeBytes(msg, msg.readerIndex(), bodyLen); }
Example 5
Source File: TrackManager.java From trekarta with GNU General Public License v3.0 | 5 votes |
@Override public void saveData(OutputStream outputStream, FileDataSource source, @Nullable ProgressListener progressListener) throws Exception { if (source.tracks.size() != 1) throw new Exception("Only single track can be saved in mtrack format"); Track track = source.tracks.get(0); if (progressListener != null) progressListener.onProgressStarted(track.points.size()); CodedOutputStream output = CodedOutputStream.newInstance(outputStream); output.writeUInt32(FIELD_VERSION, VERSION); int progress = 0; for (Track.TrackPoint point : track.points) { output.writeTag(FIELD_POINT, WireFormat.WIRETYPE_LENGTH_DELIMITED); output.writeRawVarint32(getSerializedPointSize(point)); output.writeInt32(FIELD_POINT_LATITUDE, point.latitudeE6); output.writeInt32(FIELD_POINT_LONGITUDE, point.longitudeE6); output.writeFloat(FIELD_POINT_ALTITUDE, point.elevation); output.writeFloat(FIELD_POINT_SPEED, point.speed); output.writeFloat(FIELD_POINT_BEARING, point.bearing); output.writeFloat(FIELD_POINT_ACCURACY, point.accuracy); output.writeUInt64(FIELD_POINT_TIMESTAMP, point.time); if (!point.continuous) //noinspection ConstantConditions output.writeBool(8, point.continuous); progress++; if (progressListener != null) progressListener.onProgressChanged(progress); } output.writeBytes(FIELD_NAME, ByteString.copyFromUtf8(track.name)); output.writeUInt32(FIELD_COLOR, track.style.color); output.writeFloat(FIELD_WIDTH, track.style.width); output.flush(); outputStream.close(); if (progressListener != null) progressListener.onProgressFinished(); }
Example 6
Source File: ProtoBufArray.java From LiquidDonkey with MIT License | 5 votes |
/** * Encode custom protobuf variable length array. * * @param <T> the item type * @param items the list of items, not null * @return the encoded list, not null * @throws IOException, not null * @throws NullPointerException if any arguments are null */ public static <T extends GeneratedMessage> byte[] encode(List<T> items) throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); CodedOutputStream stream = CodedOutputStream.newInstance(bytes); for (T item : items) { byte[] encoded = item.toByteArray(); stream.writeRawVarint32(encoded.length); stream.writeRawBytes(encoded); } stream.flush(); return bytes.toByteArray(); }
Example 7
Source File: CounterSerDes.java From blueflood with Apache License 2.0 | 5 votes |
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 8
Source File: TimerRollupSerDes.java From blueflood with Apache License 2.0 | 5 votes |
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 9
Source File: SetSerDes.java From blueflood with Apache License 2.0 | 5 votes |
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 10
Source File: RpcEncoder.java From Bats with Apache License 2.0 | 4 votes |
@Override protected void encode(ChannelHandlerContext ctx, OutboundRpcMessage msg, List<Object> out) throws Exception { if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Rpc Encoder called with msg {}", msg); } if (!ctx.channel().isOpen()) { //output.add(ctx.alloc().buffer(0)); logger.debug("Channel closed, skipping encode."); msg.release(); return; } try{ if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Encoding outbound message {}", msg); } // first we build the RpcHeader RpcHeader header = RpcHeader.newBuilder() // .setMode(msg.mode) // .setCoordinationId(msg.coordinationId) // .setRpcType(msg.rpcType).build(); // figure out the full length int headerLength = header.getSerializedSize(); int protoBodyLength = msg.pBody.getSerializedSize(); int rawBodyLength = msg.getRawBodySize(); int fullLength = // HEADER_TAG_LENGTH + getRawVarintSize(headerLength) + headerLength + // PROTOBUF_BODY_TAG_LENGTH + getRawVarintSize(protoBodyLength) + protoBodyLength; // if (rawBodyLength > 0) { fullLength += (RAW_BODY_TAG_LENGTH + getRawVarintSize(rawBodyLength) + rawBodyLength); } ByteBuf buf = ctx.alloc().buffer(); OutputStream os = new ByteBufOutputStream(buf); CodedOutputStream cos = CodedOutputStream.newInstance(os); // write full length first (this is length delimited stream). cos.writeRawVarint32(fullLength); // write header cos.writeRawVarint32(HEADER_TAG); cos.writeRawVarint32(headerLength); header.writeTo(cos); // write protobuf body length and body cos.writeRawVarint32(PROTOBUF_BODY_TAG); cos.writeRawVarint32(protoBodyLength); msg.pBody.writeTo(cos); // if exists, write data body and tag. if (msg.getRawBodySize() > 0) { if(RpcConstants.EXTRA_DEBUGGING) { logger.debug("Writing raw body of size {}", msg.getRawBodySize()); } cos.writeRawVarint32(RAW_BODY_TAG); cos.writeRawVarint32(rawBodyLength); cos.flush(); // need to flush so that dbody goes after if cos is caching. final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(msg.dBodies.length + 1); cbb.addComponent(buf); int bufLength = buf.readableBytes(); for (ByteBuf b : msg.dBodies) { cbb.addComponent(b); bufLength += b.readableBytes(); } cbb.writerIndex(bufLength); out.add(cbb); } else { cos.flush(); out.add(buf); } if (RpcConstants.SOME_DEBUGGING) { logger.debug("Wrote message length {}:{} bytes (head:body). Message: " + msg, getRawVarintSize(fullLength), fullLength); } if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Sent message. Ending writer index was {}.", buf.writerIndex()); } } finally { // make sure to release Rpc Messages underlying byte buffers. //msg.release(); } }
Example 11
Source File: ReflectiveCodec.java From jprotobuf with Apache License 2.0 | 4 votes |
private void writeTo(FieldInfo fieldInfo, Object value, CodedOutputStream out) throws IOException { FieldType fieldType = fieldInfo.getFieldType(); int order = fieldInfo.getOrder(); if (value instanceof List) { // if check list CodedConstant.writeToList(out, order, fieldType, (List) value); return; } switch (fieldType) { case DOUBLE: out.writeDouble(order, (Double) value); break; case BYTES: ByteString bytes = ByteString.copyFrom((byte[]) value); out.writeBytes(order, bytes); break; case STRING: ByteString string = ByteString.copyFromUtf8(value.toString()); out.writeBytes(order, string); break; case BOOL: out.writeBool(order, (Boolean) value); break; case FIXED32: out.writeFixed32(order, (Integer) value); break; case SFIXED32: out.writeSFixed32(order, (Integer) value); break; case SINT32: out.writeSInt32(order, (Integer) value); break; case INT32: out.writeInt32(order, (Integer) value); break; case UINT32: out.writeUInt32(order, (Integer) value); break; case FIXED64: out.writeFixed64(order, (Long) value); break; case SFIXED64: out.writeSFixed64(order, (Long) value); break; case SINT64: out.writeSInt64(order, (Long) value); break; case INT64: out.writeInt64(order, (Long) value); break; case UINT64: out.writeUInt64(order, (Long) value); break; case ENUM: int i; i = getEnumValue(value); out.writeEnum(order, i); break; case FLOAT: out.writeFloat(order, (Float) value); break; case OBJECT: Class c = value.getClass(); ReflectiveCodec codec = new ReflectiveCodec(c); out.writeRawVarint32(CodedConstant.makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED)); out.writeRawVarint32(codec.size(value)); codec.writeTo(value, out); break; default: throw new IOException("Unknown field type on field '" + fieldInfo.getField().getName() + "'"); } }
Example 12
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 4 votes |
/** * Write object to byte array by {@link FieldType} * * @param out * @param order * @param type * @param o * @throws IOException */ public static void writeObject(CodedOutputStream out, int order, FieldType type, Object o, boolean list) throws IOException { if (o == null) { return; } if (type == FieldType.OBJECT) { Class cls = o.getClass(); Codec target = ProtobufProxy.create(cls); out.writeRawVarint32(makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED)); out.writeRawVarint32(target.size(o)); target.writeTo(o, out); return; } if (type == FieldType.BOOL) { out.writeBool(order, (Boolean) o); } else if (type == FieldType.BYTES) { byte[] bb = (byte[]) o; out.writeBytes(order, ByteString.copyFrom(bb)); } else if (type == FieldType.DOUBLE) { out.writeDouble(order, (Double) o); } else if (type == FieldType.FIXED32) { out.writeFixed32(order, (Integer) o); } else if (type == FieldType.FIXED64) { out.writeFixed64(order, (Long) o); } else if (type == FieldType.FLOAT) { out.writeFloat(order, (Float) o); } else if (type == FieldType.INT32) { out.writeInt32(order, (Integer) o); } else if (type == FieldType.INT64) { out.writeInt64(order, (Long) o); } else if (type == FieldType.SFIXED32) { out.writeSFixed32(order, (Integer) o); } else if (type == FieldType.SFIXED64) { out.writeSFixed64(order, (Long) o); } else if (type == FieldType.SINT32) { out.writeSInt32(order, (Integer) o); } else if (type == FieldType.SINT64) { out.writeSInt64(order, (Long) o); } else if (type == FieldType.STRING) { out.writeBytes(order, ByteString.copyFromUtf8(String.valueOf(o))); } else if (type == FieldType.UINT32) { out.writeUInt32(order, (Integer) o); } else if (type == FieldType.UINT64) { out.writeUInt64(order, (Long) o); } else if (type == FieldType.ENUM) { int value = 0; if (o instanceof EnumReadable) { value = ((EnumReadable) o).value(); } else if (o instanceof Enum) { value = ((Enum) o).ordinal(); } out.writeEnum(order, value); } }