當前位置: 首頁>>代碼示例>>Java>>正文


Java CodedOutputStream.writeRawVarint32方法代碼示例

本文整理匯總了Java中com.google.protobuf.CodedOutputStream.writeRawVarint32方法的典型用法代碼示例。如果您正苦於以下問題:Java CodedOutputStream.writeRawVarint32方法的具體用法?Java CodedOutputStream.writeRawVarint32怎麽用?Java CodedOutputStream.writeRawVarint32使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.google.protobuf.CodedOutputStream的用法示例。


在下文中一共展示了CodedOutputStream.writeRawVarint32方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: toChannelBuffer

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
/**
 * Serializes the given protobuf object into a Netty {@link ChannelBuffer}.
 * @param method The name of the method of the RPC we're going to send.
 * @param pb The protobuf to serialize.
 * @return A new channel buffer containing the serialized protobuf, with
 * enough free space at the beginning to tack on the RPC header.
 */
static final ChannelBuffer toChannelBuffer(final byte[] method,
                                           final AbstractMessageLite pb) {
  final int pblen = pb.getSerializedSize();
  final int vlen = CodedOutputStream.computeRawVarint32Size(pblen);
  final byte[] buf = new byte[4 + 19 + method.length + vlen + pblen];
  try {
    final CodedOutputStream out = CodedOutputStream.newInstance(buf, 4 + 19 + method.length,
                                                                vlen + pblen);
    out.writeRawVarint32(pblen);
    pb.writeTo(out);
    out.checkNoSpaceLeft();
  } catch (IOException e) {
    throw new RuntimeException("Should never happen", e);
  }
  return ChannelBuffers.wrappedBuffer(buf);
}
 
開發者ID:OpenTSDB,項目名稱:asynccassandra,代碼行數:24,代碼來源:HBaseRpc.java

示例2: setupResponseForProtobuf

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private byte[] setupResponseForProtobuf(
    RpcResponseHeaderProto header, Writable rv) throws IOException {
  Message payload = (rv != null)
      ? ((RpcWritable.ProtobufWrapper)rv).getMessage() : null;
  int length = getDelimitedLength(header);
  if (payload != null) {
    length += getDelimitedLength(payload);
  }
  byte[] buf = new byte[length + 4];
  CodedOutputStream cos = CodedOutputStream.newInstance(buf);
  // the stream only supports little endian ints
  cos.writeRawByte((byte)((length >>> 24) & 0xFF));
  cos.writeRawByte((byte)((length >>> 16) & 0xFF));
  cos.writeRawByte((byte)((length >>>  8) & 0xFF));
  cos.writeRawByte((byte)((length >>>  0) & 0xFF));
  cos.writeRawVarint32(header.getSerializedSize());
  header.writeTo(cos);
  if (payload != null) {
    cos.writeRawVarint32(payload.getSerializedSize());
    payload.writeTo(cos);
  }
  return buf;
}
 
開發者ID:hopshadoop,項目名稱:hops,代碼行數:24,代碼來源:Server.java

示例3: encodeWithLengthPrefix

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
public static void encodeWithLengthPrefix(MessageLite msg, ByteBuf out) throws Exception {
	ByteBuf msgBytes;
	if(msg==null){
		msgBytes=Unpooled.EMPTY_BUFFER;
	}
	else{
		msgBytes=encodeNoLengthPrefix(msg);
	}
    int encodedMessageLen = msgBytes.readableBytes();
    int headerLenLen = CodedOutputStream.computeRawVarint32Size(encodedMessageLen);
    out.ensureWritable(headerLenLen + encodedMessageLen);

    CodedOutputStream headerOut =
            CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLenLen);
    headerOut.writeRawVarint32(encodedMessageLen);
    headerOut.flush();

    out.writeBytes(msgBytes, msgBytes.readerIndex(), encodedMessageLen);

}
 
開發者ID:pmarches,項目名稱:peercentrum-core,代碼行數:21,代碼來源:ProtobufByteBufCodec.java

示例4: encode

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
@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);
}
 
開發者ID:ninelook,項目名稱:wecard-server,代碼行數:15,代碼來源:ProtobufVarint32LengthFieldPrepender.java

示例5: doVarIntTest

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
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));
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:11,代碼來源:TestProtoUtil.java

示例6: encode

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
protected void encode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf1, ByteBuf paramByteBuf2)
        throws Exception {
    int i = paramByteBuf1.readableBytes();
    int j = CodedOutputStream.computeRawVarint32Size(i);
    paramByteBuf2.ensureWritable(j + i);

    CodedOutputStream localCodedOutputStream = CodedOutputStream.newInstance(new ByteBufOutputStream(paramByteBuf2), j);

    localCodedOutputStream.writeRawVarint32(i);
    localCodedOutputStream.flush();

    paramByteBuf2.writeBytes(paramByteBuf1, paramByteBuf1.readerIndex(), i);
}
 
開發者ID:Superioz,項目名稱:MooProject,代碼行數:14,代碼來源:Varint32LengthFieldPrepender.java

示例7: encode

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
protected void encode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf1, ByteBuf paramByteBuf2)
        throws Exception {
    int i = paramByteBuf1.readableBytes();
    int j = CodedOutputStream.computeRawVarint32Size(i);
    paramByteBuf2.ensureWritable(j + i);

    CodedOutputStream localCodedOutputStream = CodedOutputStream.newInstance(new ByteBufOutputStream(paramByteBuf2), j);

    localCodedOutputStream.writeRawVarint32(i);
    localCodedOutputStream.flush();

    paramByteBuf2.writeBytes(paramByteBuf1, paramByteBuf1.readerIndex(), i);
    paramByteBuf2.release();
}
 
開發者ID:Superioz,項目名稱:MooProject,代碼行數:15,代碼來源:ProtobufVarint32LengthFieldPrepender.java

示例8: sendResponse

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private void sendResponse(com.google.protobuf.GeneratedMessage response) {
    try {
        CodedOutputStream outputStream = CodedOutputStream.newInstance(socket.getOutputStream());
        outputStream.writeRawVarint32(response.getSerializedSize());
        outputStream.flush();
        response.writeTo(outputStream);
        outputStream.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:cscenter,項目名稱:hpcourse,代碼行數:12,代碼來源:SocketConnectionThread.java

示例9: sendRequest

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
private static void sendRequest(Protocol.ServerRequest request, Socket socket) throws IOException {
    CodedOutputStream outputStream = CodedOutputStream.newInstance(socket.getOutputStream());
    outputStream.writeRawVarint32(request.getSerializedSize());
    outputStream.flush();
    request.writeTo(outputStream);
    outputStream.flush();
}
 
開發者ID:cscenter,項目名稱:hpcourse,代碼行數:8,代碼來源:RandomClient.java

示例10: saveData

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
@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();
}
 
開發者ID:andreynovikov,項目名稱:trekarta,代碼行數:36,代碼來源:TrackManager.java

示例11: encode

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
/**
 * 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();
}
 
開發者ID:horrorho,項目名稱:LiquidDonkey,代碼行數:21,代碼來源:ProtoBufArray.java

示例12: encode

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
@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));
    headerOut.writeRawVarint32(bodyLen);
    headerOut.flush();

    out.writeBytes(msg, msg.readerIndex(), bodyLen);
}
 
開發者ID:kyle-liu,項目名稱:netty4study,代碼行數:15,代碼來源:ProtobufVarint32LengthFieldPrepender.java

示例13: writeObject

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
/**
 * Write object to byte array by {@link FieldType}.
 *
 * @param out the out
 * @param order the order
 * @param type the type
 * @param o the o
 * @param list the list
 * @throws IOException Signals that an I/O exception has occurred.
 */
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);
    }
}
 
開發者ID:jhunters,項目名稱:jprotobuf,代碼行數:70,代碼來源:CodedConstant.java

示例14: writeTo

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
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() + "'");
	}

}
 
開發者ID:jhunters,項目名稱:jprotobuf,代碼行數:76,代碼來源:ReflectiveCodec.java

示例15: writeObject

import com.google.protobuf.CodedOutputStream; //導入方法依賴的package包/類
/**
 * 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);
    }
}
 
開發者ID:jhunters,項目名稱:jprotobuf,代碼行數:69,代碼來源:CodedConstant.java


注:本文中的com.google.protobuf.CodedOutputStream.writeRawVarint32方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。