com.google.protobuf.CodedOutputStream# writeRawVarint32 ( ) 源码实例Demo

下面列出了com.google.protobuf.CodedOutputStream# writeRawVarint32 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: blueflood   文件: RawSerDes.java

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()));
    }
}
 
源代码2 项目: hadoop   文件: TestProtoUtil.java

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));
}
 
源代码3 项目: big-c   文件: TestProtoUtil.java

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));
}
 

@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);
}
 
源代码5 项目: trekarta   文件: TrackManager.java

@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();
}
 
源代码6 项目: LiquidDonkey   文件: ProtoBufArray.java

/**
 * 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();
}
 
源代码7 项目: blueflood   文件: CounterSerDes.java

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());
}
 
源代码8 项目: blueflood   文件: TimerRollupSerDes.java

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);
    }
}
 
源代码9 项目: blueflood   文件: SetSerDes.java

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);
    }
}
 
源代码10 项目: Bats   文件: RpcEncoder.java

@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();
  }
}
 
源代码11 项目: jprotobuf   文件: ReflectiveCodec.java

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() + "'");
	}

}
 
源代码12 项目: jprotobuf   文件: CodedConstant.java

/**
 * 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);
    }
}