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; }
/** * 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."); } }
@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); }
/** * 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); } }
/** * 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); } }
/** * 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; } }
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; }
@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(); }
/** * 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); }
/** * 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; }
/** * 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); } }
/** * 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); } }; }
/** 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); }
public void writeInt64(final int fieldNumber, final long value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeInt64NoTag(value); }
public void writeUInt64(final int fieldNumber, final long value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeUInt64NoTag(value); }
/** Write a {@code bool} field, including tag, to the stream. */ public void writeBool(final int fieldNumber, final boolean value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeBoolNoTag(value); }
/** Write a {@code bytes} field, including tag, to the stream. */ public void writeBytes(final int fieldNumber, final ByteString value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeBytesNoTag(value); }
public void writeEnum(final int fieldNumber, final int value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeEnumNoTag(value); }
/** Write a {@code uint32} field, including tag, to the stream. */ public void writeUInt32(final int fieldNumber, final int value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeUInt32NoTag(value); }
public void writeSFixed64(final int fieldNumber, long value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64); writeSFixed64NoTag(value); }
/** Write an embedded message field, including tag, to the stream. */ public void writeMessage(final int fieldNumber, final ByteBufGeneratedMessage value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeMessageNoTag(value); }
/** Write an double field, including tag, to the stream. */ public void writeDouble(final int fieldNumber, double value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64); buf.writeLongLE(Double.doubleToLongBits(value)); }
@NonNull @Override public FileDataSource loadData(InputStream inputStream, String filePath) throws Exception { long propertiesOffset = 0L; Track track = new Track(); CodedInputStream input = CodedInputStream.newInstance(inputStream); boolean done = false; while (!done) { long offset = input.getTotalBytesRead(); int tag = input.readTag(); int field = WireFormat.getTagFieldNumber(tag); switch (field) { case 0: done = true; break; default: { throw new com.google.protobuf.InvalidProtocolBufferException("Unsupported proto field: " + tag); } case FIELD_VERSION: { // skip version input.skipField(tag); break; } case FIELD_POINT: { int length = input.readRawVarint32(); int oldLimit = input.pushLimit(length); readPoint(track, input); input.popLimit(oldLimit); input.checkLastTagWas(0); break; } case FIELD_NAME: { propertiesOffset = offset; track.name = input.readBytes().toStringUtf8(); break; } case FIELD_COLOR: { track.style.color = input.readUInt32(); break; } case FIELD_WIDTH: { track.style.width = input.readFloat(); break; } } } inputStream.close(); track.id = 31 * filePath.hashCode() + 1; FileDataSource dataSource = new FileDataSource(); dataSource.name = track.name; dataSource.tracks.add(track); track.source = dataSource; dataSource.propertiesOffset = propertiesOffset; return dataSource; }
private void readPoint(Track track, CodedInputStream input) throws IOException { int latitudeE6 = 0; int longitudeE6 = 0; boolean continuous = true; float altitude = Float.NaN; float speed = Float.NaN; float bearing = Float.NaN; float accuracy = Float.NaN; long timestamp = 0L; boolean done = false; while (!done) { int tag = input.readTag(); int field = WireFormat.getTagFieldNumber(tag); switch (field) { case 0: done = true; break; default: { throw new com.google.protobuf.InvalidProtocolBufferException("Unsupported proto field: " + tag); } case FIELD_POINT_LATITUDE: { latitudeE6 = input.readInt32(); break; } case FIELD_POINT_LONGITUDE: { longitudeE6 = input.readInt32(); break; } case FIELD_POINT_ALTITUDE: { altitude = input.readFloat(); break; } case FIELD_POINT_SPEED: { speed = input.readFloat(); break; } case FIELD_POINT_BEARING: { bearing = input.readFloat(); break; } case FIELD_POINT_ACCURACY: { accuracy = input.readFloat(); break; } case FIELD_POINT_TIMESTAMP: { timestamp = input.readUInt64(); break; } case FIELD_POINT_CONTINUOUS: { continuous = input.readBool(); break; } } } track.addPointFast(continuous, latitudeE6, longitudeE6, altitude, speed, bearing, accuracy, timestamp); }
/** * Read a field of any primitive type for immutable messages from a CodedInputStream. Enums, groups, and embedded * messages are not handled by this method. * * @param input The stream from which to read. * @param type Declared type of the field. * @param checkUtf8 When true, check that the input is valid utf8. * @return An object representing the field's value, 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 Object readPrimitiveField(CodedInputStream input, final WireFormat.FieldType type, boolean checkUtf8) throws IOException { switch (type) { case DOUBLE: return input.readDouble(); case FLOAT: return input.readFloat(); case INT64: return input.readInt64(); case UINT64: return input.readUInt64(); case INT32: return input.readInt32(); case FIXED64: return input.readFixed64(); case FIXED32: return input.readFixed32(); case BOOL: return input.readBool(); case STRING: if (checkUtf8) { return input.readStringRequireUtf8(); } else { return input.readString(); } case BYTES: return input.readBytes(); case UINT32: return input.readUInt32(); case SFIXED32: return input.readSFixed32(); case SFIXED64: return input.readSFixed64(); case SINT32: return input.readSInt32(); case SINT64: return input.readSInt64(); case GROUP: throw new IllegalArgumentException("readPrimitiveField() cannot handle nested groups."); case MESSAGE: throw new IllegalArgumentException("readPrimitiveField() cannot handle embedded messages."); case ENUM: // We don't handle enums because we don't know what to do if the // value is not recognized. throw new IllegalArgumentException("readPrimitiveField() cannot handle enums."); } throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise."); }
/** * Compute the number of bytes that would be needed to encode a particular value of arbitrary type, excluding tag. * * @param type The field's type. * @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 computeElementSizeNoTag(final WireFormat.FieldType type, final Object value) { switch (type) { // Note: Minor violation of 80-char limit rule here because this would // actually be harder to read if we wrapped the lines. case DOUBLE: return CodedOutputStream.computeDoubleSizeNoTag((Double) value); case FLOAT: return CodedOutputStream.computeFloatSizeNoTag((Float) value); case INT64: return CodedOutputStream.computeInt64SizeNoTag((Long) value); case UINT64: return CodedOutputStream.computeUInt64SizeNoTag((Long) value); case INT32: return CodedOutputStream.computeInt32SizeNoTag((Integer) value); case FIXED64: return CodedOutputStream.computeFixed64SizeNoTag((Long) value); case FIXED32: return CodedOutputStream.computeFixed32SizeNoTag((Integer) value); case BOOL: return CodedOutputStream.computeBoolSizeNoTag((Boolean) value); case STRING: return CodedOutputStream.computeStringSizeNoTag((String) value); case GROUP: return CodedOutputStream.computeGroupSizeNoTag((MessageLite) value); case BYTES: if (value instanceof ByteString) { return CodedOutputStream.computeBytesSizeNoTag((ByteString) value); } else { return CodedOutputStream.computeByteArraySizeNoTag((byte[]) value); } case UINT32: return CodedOutputStream.computeUInt32SizeNoTag((Integer) value); case SFIXED32: return CodedOutputStream.computeSFixed32SizeNoTag((Integer) value); case SFIXED64: return CodedOutputStream.computeSFixed64SizeNoTag((Long) value); case SINT32: return CodedOutputStream.computeSInt32SizeNoTag((Integer) value); case SINT64: return CodedOutputStream.computeSInt64SizeNoTag((Long) value); case MESSAGE: if (value instanceof LazyField) { return CodedOutputStream.computeLazyFieldSizeNoTag((LazyField) value); } else { return computeObjectSizeNoTag(value); } case ENUM: if (value instanceof Internal.EnumLite) { return CodedOutputStream.computeEnumSizeNoTag(((Internal.EnumLite) value).getNumber()); } else { if (value instanceof EnumReadable) { return CodedOutputStream.computeEnumSizeNoTag(((EnumReadable) value).value()); } else if (value instanceof Enum) { return CodedOutputStream.computeEnumSizeNoTag(((Enum) value).ordinal()); } return CodedOutputStream.computeEnumSizeNoTag((Integer) value); } } throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise."); }
/** * 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); } }