本文整理汇总了Java中com.google.protobuf.WireFormat类的典型用法代码示例。如果您正苦于以下问题:Java WireFormat类的具体用法?Java WireFormat怎么用?Java WireFormat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WireFormat类属于com.google.protobuf包,在下文中一共展示了WireFormat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFrom
import com.google.protobuf.WireFormat; //导入依赖的package包/类
public static BlockListAsLongs readFrom(InputStream is) throws IOException {
CodedInputStream cis = CodedInputStream.newInstance(is);
int numBlocks = -1;
ByteString blocksBuf = null;
while (!cis.isAtEnd()) {
int tag = cis.readTag();
int field = WireFormat.getTagFieldNumber(tag);
switch(field) {
case 0:
break;
case 1:
numBlocks = (int)cis.readInt32();
break;
case 2:
blocksBuf = cis.readBytes();
break;
default:
cis.skipField(tag);
break;
}
}
if (numBlocks != -1 && blocksBuf != null) {
return decodeBuffer(numBlocks, blocksBuf);
}
return null;
}
示例2: skipField
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Reads and discards a single field, given its tag value.
*
* @return {@code false} if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns
* {@code true}.
*/
public boolean skipField(final int tag) throws IOException {
switch (getTagWireType(tag)) {
case WireFormat.WIRETYPE_VARINT:
readInt32();
return true;
case WireFormat.WIRETYPE_FIXED64:
readRawLittleEndian64();
return true;
case WireFormat.WIRETYPE_LENGTH_DELIMITED:
skipRawBytes(readRawVarint32());
return true;
case WireFormat.WIRETYPE_START_GROUP:
skipMessage();
checkLastTagWas(makeTag(WireFormat.getTagFieldNumber(tag), WireFormat.WIRETYPE_END_GROUP));
return true;
case WireFormat.WIRETYPE_END_GROUP:
return false;
case WireFormat.WIRETYPE_FIXED32:
readRawLittleEndian32();
return true;
default:
throw new InvalidProtocolBufferException("Protocol message tag had invalid wire type.");
}
}
示例3: testWritingDouble
import com.google.protobuf.WireFormat; //导入依赖的package包/类
@Test
public void testWritingDouble() throws IOException {
ByteBuf buf = Unpooled.buffer();
buf.clear();
ByteBufCodedOutputStream outputStream = ByteBufCodedOutputStream.get(buf);
outputStream.writeDouble(12, 23d);
outputStream.writeDouble(15, 13.13d);
outputStream.writeDouble(1, -0.003d);
ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(buf);
assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 12);
assertEquals(inputStream.readDouble(), 23d);
assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 15);
assertEquals(inputStream.readDouble(), 13.13d);
assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 1);
assertEquals(inputStream.readDouble(), -0.003d);
}
示例4: writeToList
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* write list to {@link CodedOutputStream} object.
*
* @param out target output stream to write
* @param order field order
* @param type field type
* @param list target list object to be serialized
* @param packed the packed
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void writeToList(CodedOutputStream out, int order, FieldType type, List list, boolean packed)
throws IOException {
if (list == null || list.isEmpty()) {
return;
}
if (packed) {
out.writeUInt32NoTag(makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED));
out.writeUInt32NoTag(computeListSize(order, list, type, false, null, packed, true));
}
for (Object object : list) {
writeObject(out, order, type, object, true, !packed);
}
}
示例5: parseField
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Parses the field.
*
* @param <T> the generic type
* @param input the input
* @param extensionRegistry the extension registry
* @param type the type
* @param value the value
* @return the t
* @throws IOException Signals that an I/O exception has occurred.
*/
@SuppressWarnings("unchecked")
static <T> T parseField(CodedInputStream input, ExtensionRegistryLite extensionRegistry, WireFormat.FieldType type,
T value) throws IOException {
switch (type) {
case MESSAGE:
int length = input.readRawVarint32();
final int oldLimit = input.pushLimit(length);
Codec<? extends Object> codec = ProtobufProxy.create(value.getClass());
T ret = (T) codec.decode(input.readRawBytes(length));
input.popLimit(oldLimit);
return ret;
case ENUM:
return (T) (java.lang.Integer) input.readEnum();
case GROUP:
throw new RuntimeException("Groups are not allowed in maps.");
default:
return (T) CodedConstant.readPrimitiveField(input, type, true);
}
}
示例6: isWallet
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
*
* @param is
* input stream to test
* @return true if input stream is a wallet
*/
public static boolean isWallet(InputStream is) {
try {
final CodedInputStream cis = CodedInputStream.newInstance(is);
final int tag = cis.readTag();
final int field = WireFormat.getTagFieldNumber(tag);
if (field != 1) // network_identifier
return false;
final String network = cis.readString();
return NetworkParameters.fromID(network) != null;
} catch (IOException x) {
return false;
}
}
示例7: readTag
import com.google.protobuf.WireFormat; //导入依赖的package包/类
public int readTag() throws IOException {
if (isAtEnd()) {
lastTag = 0;
return 0;
}
lastTag = readRawVarint32();
if (WireFormat.getTagFieldNumber(lastTag) == 0) {
// If we actually read zero (or any tag number corresponding to field
// number zero), that's not a valid tag.
throw new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint.");
}
return lastTag;
}
示例8: isWallet
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
*
* @param is
* input stream to test
* @return true if input stream is a wallet
*/
public static boolean isWallet(InputStream is) {
try {
final CodedInputStream cis = CodedInputStream.newInstance(is);
final int tag = cis.readTag();
final int field = WireFormat.getTagFieldNumber(tag);
if (field != 1) // network_identifier
return false;
final String network = cis.readString();
return NetworkParameters.fromID(network) != null;
} catch (IOException x) {
return false;
}
}
示例9: saveData
import com.google.protobuf.WireFormat; //导入依赖的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();
}
示例10: computeElementSize
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Compute the number of bytes that would be needed to encode a single tag/value pair of arbitrary type.
*
* @param type The field's type.
* @param number The field's number.
* @param value Object representing the field's value. Must be of the exact type which would be returned by
* {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
* @return the int
*/
public static int computeElementSize(final WireFormat.FieldType type, final int number, final Object value) {
int tagSize = CodedOutputStream.computeTagSize(number);
if (type == WireFormat.FieldType.GROUP) {
// Only count the end group tag for proto2 messages as for proto1 the end
// group tag will be counted as a part of getSerializedSize().
tagSize *= 2;
}
return tagSize + computeElementSizeNoTag(type, value);
}
示例11: computeMapSize
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Compute map size.
*
* @param <K> the key type
* @param <V> the value type
* @param order the order
* @param map the map
* @param keyType the key type
* @param defaultKey the default key
* @param valueType the value type
* @param defalutValue the defalut value
* @return the int
*/
public static <K, V> int computeMapSize(int order, Map<K, V> map, com.google.protobuf.WireFormat.FieldType keyType,
K defaultKey, com.google.protobuf.WireFormat.FieldType valueType, V defalutValue) {
int size = 0;
for (java.util.Map.Entry<K, V> entry : map.entrySet()) {
com.baidu.bjf.remoting.protobuf.MapEntry<K, V> valuesDefaultEntry = com.baidu.bjf.remoting.protobuf.MapEntry
.<K, V> newDefaultInstance(null, keyType, defaultKey, valueType, defalutValue);
com.baidu.bjf.remoting.protobuf.MapEntry<K, V> values =
valuesDefaultEntry.newBuilderForType().setKey(entry.getKey()).setValue(entry.getValue()).build();
size += com.google.protobuf.CodedOutputStream.computeMessageSize(order, values);
}
return size;
}
示例12: writeElement
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Write a single tag-value pair to the stream.
*
* @param output The output stream.
* @param type The field's type.
* @param number The field's number.
* @param value Object representing the field's value. Must be of the exact type which would be returned by
* {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void writeElement(final CodedOutputStream output, final WireFormat.FieldType type, final int number,
final Object value) throws IOException {
// Special case for groups, which need a start and end tag; other fields
// can just use writeTag() and writeFieldNoTag().
if (type == WireFormat.FieldType.GROUP) {
output.writeGroup(number, (MessageLite) value);
} else {
output.writeTag(number, getWireFormatForFieldType(type, false));
writeElementNoTag(output, type, value);
}
}
示例13: Metadata
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/**
* Instantiates a new metadata.
*
* @param descriptor the descriptor
* @param defaultInstance the default instance
* @param keyType the key type
* @param valueType the value type
*/
public Metadata(Descriptor descriptor, MapEntry<K, V> defaultInstance, WireFormat.FieldType keyType,
WireFormat.FieldType valueType) {
super(keyType, defaultInstance.key, valueType, defaultInstance.value);
this.descriptor = descriptor;
this.parser = new AbstractParser<MapEntry<K, V>>() {
@Override
public MapEntry<K, V> parsePartialFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return new MapEntry<K, V>(Metadata.this, input, extensionRegistry);
}
};
}
示例14: writeInt32
import com.google.protobuf.WireFormat; //导入依赖的package包/类
/** Write an {@code int32} field, including tag, to the stream. */
public void writeInt32(final int fieldNumber, final int value) throws IOException {
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
writeInt32NoTag(value);
}
示例15: writeInt64
import com.google.protobuf.WireFormat; //导入依赖的package包/类
public void writeInt64(final int fieldNumber, final long value) throws IOException {
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
writeInt64NoTag(value);
}