Java Code Examples for com.google.protobuf.WireFormat#FieldType
The following examples show how to use
com.google.protobuf.WireFormat#FieldType .
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: MapEntryLite.java From jprotobuf with Apache License 2.0 | 6 votes |
/** * 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); } }
Example 2
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 5 votes |
/** * 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); }
Example 3
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 5 votes |
/** * 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); } }
Example 4
Source File: MapEntry.java From jprotobuf with Apache License 2.0 | 5 votes |
/** * 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); } }; }
Example 5
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 4 votes |
/** * 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.readByteArray(); 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."); }
Example 6
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 4 votes |
/** * Write a field of arbitrary type, without its tag, to the stream. * * @param output The output stream. * @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. * @throws IOException Signals that an I/O exception has occurred. */ public static void writeElementNoTag(final CodedOutputStream output, final WireFormat.FieldType type, final Object value) throws IOException { switch (type) { case DOUBLE: output.writeDoubleNoTag((Double) value); break; case FLOAT: output.writeFloatNoTag((Float) value); break; case INT64: output.writeInt64NoTag((Long) value); break; case UINT64: output.writeUInt64NoTag((Long) value); break; case INT32: output.writeInt32NoTag((Integer) value); break; case FIXED64: output.writeFixed64NoTag((Long) value); break; case FIXED32: output.writeFixed32NoTag((Integer) value); break; case BOOL: output.writeBoolNoTag((Boolean) value); break; case STRING: output.writeStringNoTag((String) value); break; // group not support yet // case GROUP : output.writeGroupNoTag ((MessageLite) value); break; case MESSAGE: writeObject(output, 0, FieldType.OBJECT, value, false, false); break; case BYTES: if (value instanceof ByteString) { output.writeBytesNoTag((ByteString) value); } else { byte[] v; if (value instanceof Byte[]) { v = toByteArray((Byte[]) value); } else { v = (byte[]) value; } output.writeByteArrayNoTag(v); } break; case UINT32: output.writeUInt32NoTag((Integer) value); break; case SFIXED32: output.writeSFixed32NoTag((Integer) value); break; case SFIXED64: output.writeSFixed64NoTag((Long) value); break; case SINT32: output.writeSInt32NoTag((Integer) value); break; case SINT64: output.writeSInt64NoTag((Long) value); break; case ENUM: if (value instanceof Internal.EnumLite) { output.writeEnumNoTag(((Internal.EnumLite) value).getNumber()); } else { if (value instanceof EnumReadable) { output.writeEnumNoTag(((EnumReadable) value).value()); } else if (value instanceof Enum) { output.writeEnumNoTag(((Enum) value).ordinal()); } else { output.writeEnumNoTag(((Integer) value).intValue()); } } break; } }
Example 7
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 4 votes |
/** * 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 { if (value instanceof Byte[]) { return computeLengthDelimitedFieldSize(((Byte[]) value).length); } 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."); }
Example 8
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 3 votes |
/** * Given a field type, return the wire type. * * @param type the type * @param isPacked the is packed * @return the wire format for field type * @returns One of the {@code WIRETYPE_} constants defined in {@link WireFormat}. */ static int getWireFormatForFieldType(final WireFormat.FieldType type, boolean isPacked) { if (isPacked) { return WireFormat.WIRETYPE_LENGTH_DELIMITED; } else { return type.getWireType(); } }
Example 9
Source File: MapEntryLite.java From jprotobuf with Apache License 2.0 | 3 votes |
/** * Instantiates a new metadata. * * @param keyType the key type * @param defaultKey the default key * @param valueType the value type * @param defaultValue the default value */ public Metadata(WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) { this.keyType = keyType; this.defaultKey = defaultKey; this.valueType = valueType; this.defaultValue = defaultValue; }
Example 10
Source File: MapEntry.java From jprotobuf with Apache License 2.0 | 3 votes |
/** * Create a default MapEntry instance. * * @param descriptor the descriptor * @param keyType the key type * @param defaultKey the default key * @param valueType the value type * @param defaultValue the default value */ private MapEntry(Descriptor descriptor, WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) { this.key = defaultKey; this.value = defaultValue; this.metadata = new Metadata<K, V>(descriptor, this, keyType, valueType); }
Example 11
Source File: MapEntryLite.java From jprotobuf with Apache License 2.0 | 2 votes |
/** * Creates a default MapEntryLite message instance. * * @param keyType the key type * @param defaultKey the default key * @param valueType the value type * @param defaultValue the default value */ private MapEntryLite(WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) { this.metadata = new Metadata<K, V>(keyType, defaultKey, valueType, defaultValue); this.key = defaultKey; this.value = defaultValue; }
Example 12
Source File: MapEntryLite.java From jprotobuf with Apache License 2.0 | 2 votes |
/** * Creates a default MapEntryLite message instance. * * This method is used by generated code to create the default instance for a map entry message. The created default * instance should be used to create new map entry messages of the same type. For each map entry message, only one * default instance should be created. * * @param <K> the key type * @param <V> the value type * @param keyType the key type * @param defaultKey the default key * @param valueType the value type * @param defaultValue the default value * @return the map entry lite */ public static <K, V> MapEntryLite<K, V> newDefaultInstance(WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) { return new MapEntryLite<K, V>(keyType, defaultKey, valueType, defaultValue); }
Example 13
Source File: MapEntry.java From jprotobuf with Apache License 2.0 | 2 votes |
/** * Create a default MapEntry instance. A default MapEntry instance should be created only once for each map entry * message type. Generated code should store the created default instance and use it later to create new MapEntry * messages of the same type. * * @param <K> the key type * @param <V> the value type * @param descriptor the descriptor * @param keyType the key type * @param defaultKey the default key * @param valueType the value type * @param defaultValue the default value * @return the map entry */ public static <K, V> MapEntry<K, V> newDefaultInstance(Descriptor descriptor, WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) { return new MapEntry<K, V>(descriptor, keyType, defaultKey, valueType, defaultValue); }