aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/main/java/com/google/protobuf
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/main/java/com/google/protobuf')
-rw-r--r--java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java31
-rw-r--r--java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java64
-rw-r--r--java/src/main/java/com/google/protobuf/nano/Extension.java700
-rw-r--r--java/src/main/java/com/google/protobuf/nano/MessageNano.java15
-rw-r--r--java/src/main/java/com/google/protobuf/nano/WireFormatNano.java236
5 files changed, 702 insertions, 344 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
index 769bb19..88df38d 100644
--- a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
@@ -168,14 +168,6 @@ public final class CodedOutputByteBufferNano {
writeBytesNoTag(value);
}
- /** Write a {@code byte} field, including tag, to the stream. */
- public void writeByteArray(final int fieldNumber, final byte[] value)
- throws IOException {
- writeTag(fieldNumber, WireFormatNano.WIRETYPE_LENGTH_DELIMITED);
- writeByteArrayNoTag(value);
- }
-
-
/** Write a {@code uint32} field, including tag, to the stream. */
public void writeUInt32(final int fieldNumber, final int value)
throws IOException {
@@ -321,12 +313,6 @@ public final class CodedOutputByteBufferNano {
writeRawBytes(value);
}
- /** Write a {@code byte[]} field to the stream. */
- public void writeByteArrayNoTag(final byte [] value) throws IOException {
- writeRawVarint32(value.length);
- writeRawBytes(value);
- }
-
/** Write a {@code uint32} field to the stream. */
public void writeUInt32NoTag(final int value) throws IOException {
writeRawVarint32(value);
@@ -468,15 +454,6 @@ public final class CodedOutputByteBufferNano {
/**
* Compute the number of bytes that would be needed to encode a
- * {@code byte[]} field, including tag.
- */
- public static int computeByteArraySize(final int fieldNumber,
- final byte[] value) {
- return computeTagSize(fieldNumber) + computeByteArraySizeNoTag(value);
- }
-
- /**
- * Compute the number of bytes that would be needed to encode a
* {@code uint32} field, including tag.
*/
public static int computeUInt32Size(final int fieldNumber, final int value) {
@@ -662,14 +639,6 @@ public final class CodedOutputByteBufferNano {
/**
* Compute the number of bytes that would be needed to encode a
- * {@code byte[]} field.
- */
- public static int computeByteArraySizeNoTag(final byte[] value) {
- return computeRawVarint32Size(value.length) + value.length;
- }
-
- /**
- * Compute the number of bytes that would be needed to encode a
* {@code uint32} field.
*/
public static int computeUInt32SizeNoTag(final int value) {
diff --git a/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java b/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java
index 066774d..839f21c 100644
--- a/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java
@@ -30,6 +30,7 @@
package com.google.protobuf.nano;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -37,34 +38,81 @@ import java.util.List;
* Base class of those Protocol Buffer messages that need to store unknown fields,
* such as extensions.
*/
-public abstract class ExtendableMessageNano extends MessageNano {
+public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>>
+ extends MessageNano {
/**
* A container for fields unknown to the message, including extensions. Extension fields can
- * can be accessed through the {@link getExtension()} and {@link setExtension()} methods.
+ * can be accessed through the {@link #getExtension} and {@link #setExtension} methods.
*/
protected List<UnknownFieldData> unknownFieldData;
@Override
public int getSerializedSize() {
- int size = WireFormatNano.computeWireSize(unknownFieldData);
+ int size = 0;
+ int unknownFieldCount = unknownFieldData == null ? 0 : unknownFieldData.size();
+ for (int i = 0; i < unknownFieldCount; i++) {
+ UnknownFieldData unknownField = unknownFieldData.get(i);
+ size += CodedOutputByteBufferNano.computeRawVarint32Size(unknownField.tag);
+ size += unknownField.bytes.length;
+ }
cachedSize = size;
return size;
}
+ @Override
+ public void writeTo(CodedOutputByteBufferNano output) throws IOException {
+ int unknownFieldCount = unknownFieldData == null ? 0 : unknownFieldData.size();
+ for (int i = 0; i < unknownFieldCount; i++) {
+ UnknownFieldData unknownField = unknownFieldData.get(i);
+ output.writeRawVarint32(unknownField.tag);
+ output.writeRawBytes(unknownField.bytes);
+ }
+ }
+
/**
* Gets the value stored in the specified extension of this message.
*/
- public <T> T getExtension(Extension<T> extension) {
- return WireFormatNano.getExtension(extension, unknownFieldData);
+ public final <T> T getExtension(Extension<M, T> extension) {
+ return extension.getValueFrom(unknownFieldData);
}
/**
* Sets the value of the specified extension of this message.
*/
- public <T> void setExtension(Extension<T> extension, T value) {
+ public final <T> M setExtension(Extension<M, T> extension, T value) {
+ unknownFieldData = extension.setValueTo(value, unknownFieldData);
+
+ @SuppressWarnings("unchecked") // Generated code should guarantee type safety
+ M typedThis = (M) this;
+ return typedThis;
+ }
+
+ /**
+ * Stores the binary data of an unknown field.
+ *
+ * <p>Generated messages will call this for unknown fields if the store_unknown_fields
+ * option is on.
+ *
+ * <p>Note that the tag might be a end-group tag (rather than the start of an unknown field) in
+ * which case we do not want to add an unknown field entry.
+ *
+ * @param input the input buffer.
+ * @param tag the tag of the field.
+
+ * @return {@literal true} unless the tag is an end-group tag.
+ */
+ protected final boolean storeUnknownField(CodedInputByteBufferNano input, int tag)
+ throws IOException {
+ int startPos = input.getPosition();
+ if (!input.skipField(tag)) {
+ return false; // This wasn't an unknown field, it's an end-group tag.
+ }
if (unknownFieldData == null) {
unknownFieldData = new ArrayList<UnknownFieldData>();
}
- WireFormatNano.setExtension(extension, value, unknownFieldData);
+ int endPos = input.getPosition();
+ byte[] bytes = input.getData(startPos, endPos - startPos);
+ unknownFieldData.add(new UnknownFieldData(tag, bytes));
+ return true;
}
-} \ No newline at end of file
+}
diff --git a/java/src/main/java/com/google/protobuf/nano/Extension.java b/java/src/main/java/com/google/protobuf/nano/Extension.java
index 4512b01..177a9cc 100644
--- a/java/src/main/java/com/google/protobuf/nano/Extension.java
+++ b/java/src/main/java/com/google/protobuf/nano/Extension.java
@@ -30,85 +30,659 @@
package com.google.protobuf.nano;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
import java.util.List;
/**
* Represents an extension.
*
* @author bduff@google.com (Brian Duff)
- * @param <T> the type of the extension.
+ * @author maxtroy@google.com (Max Cai)
+ * @param <M> the type of the extendable message this extension is for.
+ * @param <T> the Java type of the extension; see {@link #clazz}.
*/
-public class Extension<T> {
- public final int fieldNumber;
- public boolean isRepeatedField;
- public Class<T> fieldType;
- public Class<T> listType;
-
- private Extension(int fieldNumber, TypeLiteral<T> type) {
- this.fieldNumber = fieldNumber;
- isRepeatedField = type.isList();
- fieldType = type.getTargetClass();
- listType = isRepeatedField ? type.getListType() : null;
- }
-
- /**
- * Creates a new instance of {@code Extension} for the specified {@code fieldNumber} and
- * {@code type}.
- */
- public static <T> Extension<T> create(int fieldNumber, TypeLiteral<T> type) {
- return new Extension<T>(fieldNumber, type);
- }
-
- /**
- * Creates a new instance of {@code Extension} for the specified {@code fieldNumber} and
- * {@code type}. This version is used for repeated fields.
- */
- public static <T> Extension<List<T>> createRepeated(int fieldNumber, TypeLiteral<List<T>> type) {
- return new Extension<List<T>>(fieldNumber, type);
- }
-
- /**
- * Represents a generic type literal. We can't typesafely reference a
- * Class&lt;List&lt;Foo>>.class in Java, so we use this instead.
- * See: http://gafter.blogspot.com/2006/12/super-type-tokens.html
- *
- * <p>Somewhat specialized because we only ever have a Foo or a List&lt;Foo>.
- */
- public static abstract class TypeLiteral<T> {
- private final Type type;
-
- protected TypeLiteral() {
- Type superclass = getClass().getGenericSuperclass();
- if (superclass instanceof Class) {
- throw new RuntimeException("Missing type parameter");
- }
- this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0];
+public class Extension<M extends ExtendableMessageNano<M>, T> {
+
+ /*
+ * Because we typically only define message-typed extensions, the Extension class hierarchy is
+ * designed as follows, to allow a big amount of code in this file to be removed by ProGuard:
+ *
+ * Extension // ready to use for message/group typed extensions
+ * Δ
+ * |
+ * PrimitiveExtension // for primitive/enum typed extensions
+ */
+
+ public static final int TYPE_DOUBLE = 1;
+ public static final int TYPE_FLOAT = 2;
+ public static final int TYPE_INT64 = 3;
+ public static final int TYPE_UINT64 = 4;
+ public static final int TYPE_INT32 = 5;
+ public static final int TYPE_FIXED64 = 6;
+ public static final int TYPE_FIXED32 = 7;
+ public static final int TYPE_BOOL = 8;
+ public static final int TYPE_STRING = 9;
+ public static final int TYPE_GROUP = 10;
+ public static final int TYPE_MESSAGE = 11;
+ public static final int TYPE_BYTES = 12;
+ public static final int TYPE_UINT32 = 13;
+ public static final int TYPE_ENUM = 14;
+ public static final int TYPE_SFIXED32 = 15;
+ public static final int TYPE_SFIXED64 = 16;
+ public static final int TYPE_SINT32 = 17;
+ public static final int TYPE_SINT64 = 18;
+
+ /**
+ * Creates an {@code Extension} of the given message type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type {@link #TYPE_MESSAGE} or {@link #TYPE_GROUP}
+ */
+ public static <M extends ExtendableMessageNano<M>, T extends MessageNano>
+ Extension<M, T> createMessageTyped(int type, Class<T> clazz, int tag) {
+ return new Extension<M, T>(type, clazz, tag, false);
+ }
+
+ /**
+ * Creates a repeated {@code Extension} of the given message type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type {@link #TYPE_MESSAGE} or {@link #TYPE_GROUP}
+ */
+ public static <M extends ExtendableMessageNano<M>, T extends MessageNano>
+ Extension<M, T[]> createRepeatedMessageTyped(int type, Class<T[]> clazz, int tag) {
+ return new Extension<M, T[]>(type, clazz, tag, true);
+ }
+
+ /**
+ * Creates an {@code Extension} of the given primitive type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type one of {@code TYPE_*}, except {@link #TYPE_MESSAGE} and {@link #TYPE_GROUP}
+ * @param clazz the boxed Java type of this extension
+ */
+ public static <M extends ExtendableMessageNano<M>, T>
+ Extension<M, T> createPrimitiveTyped(int type, Class<T> clazz, int tag) {
+ return new PrimitiveExtension<M, T>(type, clazz, tag, false, 0, 0);
+ }
+
+ /**
+ * Creates a repeated {@code Extension} of the given primitive type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type one of {@code TYPE_*}, except {@link #TYPE_MESSAGE} and {@link #TYPE_GROUP}
+ * @param clazz the Java array type of this extension, with an unboxed component type
+ */
+ public static <M extends ExtendableMessageNano<M>, T>
+ Extension<M, T> createRepeatedPrimitiveTyped(
+ int type, Class<T> clazz, int tag, int nonPackedTag, int packedTag) {
+ return new PrimitiveExtension<M, T>(type, clazz, tag, true, nonPackedTag, packedTag);
+ }
+
+ /**
+ * Protocol Buffer type of this extension; one of the {@code TYPE_} constants.
+ */
+ protected final int type;
+
+ /**
+ * Java type of this extension. For a singular extension, this is the boxed Java type for the
+ * Protocol Buffer {@link #type}; for a repeated extension, this is an array type whose
+ * component type is the unboxed Java type for {@link #type}. For example, for a singular
+ * {@code int32}/{@link #TYPE_INT32} extension, this equals {@code Integer.class}; for a
+ * repeated {@code int32} extension, this equals {@code int[].class}.
+ */
+ protected final Class<T> clazz;
+
+ /**
+ * Tag number of this extension.
+ */
+ protected final int tag;
+
+ /**
+ * Whether this extension is repeated.
+ */
+ protected final boolean repeated;
+
+ private Extension(int type, Class<T> clazz, int tag, boolean repeated) {
+ this.type = type;
+ this.clazz = clazz;
+ this.tag = tag;
+ this.repeated = repeated;
+ }
+
+ protected boolean isMatch(int unknownDataTag) {
+ // This implementation is for message/group extensions.
+ return unknownDataTag == tag;
+ }
+
+ /**
+ * Returns the value of this extension stored in the given list of unknown fields, or
+ * {@code null} if no unknown fields matches this extension.
+ */
+ final T getValueFrom(List<UnknownFieldData> unknownFields) {
+ if (unknownFields == null) {
+ return null;
+ }
+
+ if (repeated) {
+ // For repeated extensions, read all matching unknown fields in their original order.
+ List<Object> resultList = new ArrayList<Object>();
+ for (int i = 0; i < unknownFields.size(); i++) {
+ UnknownFieldData data = unknownFields.get(i);
+ if (isMatch(data.tag) && data.bytes.length != 0) {
+ readDataInto(data, resultList);
+ }
+ }
+
+ int resultSize = resultList.size();
+ if (resultSize == 0) {
+ return null;
+ }
+
+ T result = clazz.cast(Array.newInstance(clazz.getComponentType(), resultSize));
+ for (int i = 0; i < resultSize; i++) {
+ Array.set(result, i, resultList.get(i));
+ }
+ return result;
+ } else {
+ // For singular extensions, get the last piece of data stored under this extension.
+ UnknownFieldData lastData = null;
+ for (int i = unknownFields.size() - 1; lastData == null && i >= 0; i--) {
+ UnknownFieldData data = unknownFields.get(i);
+ if (isMatch(data.tag) && data.bytes.length != 0) {
+ lastData = data;
+ }
+ }
+
+ if (lastData == null) {
+ return null;
+ }
+
+ return clazz.cast(readData(CodedInputByteBufferNano.newInstance(lastData.bytes)));
+ }
+ }
+
+ protected Object readData(CodedInputByteBufferNano input) {
+ // This implementation is for message/group extensions.
+ Class<?> messageType = repeated ? clazz.getComponentType() : clazz;
+ try {
+ switch (type) {
+ case TYPE_GROUP:
+ MessageNano group = (MessageNano) messageType.newInstance();
+ input.readGroup(group, WireFormatNano.getTagFieldNumber(tag));
+ return group;
+ case TYPE_MESSAGE:
+ MessageNano message = (MessageNano) messageType.newInstance();
+ input.readMessage(message);
+ return message;
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException(
+ "Error creating instance of class " + messageType, e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(
+ "Error creating instance of class " + messageType, e);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Error reading extension field", e);
+ }
+ }
+
+ protected void readDataInto(UnknownFieldData data, List<Object> resultList) {
+ // This implementation is for message/group extensions.
+ resultList.add(readData(CodedInputByteBufferNano.newInstance(data.bytes)));
}
/**
- * If the generic type is a list, returns {@code true}.
+ * Sets the value of this extension to the given list of unknown fields. This removes any
+ * previously stored data matching this extension.
+ *
+ * @param value The value of this extension, or {@code null} to clear this extension from the
+ * unknown fields.
+ * @return The same {@code unknownFields} list, or a new list storing the extension value if
+ * the argument was null.
*/
- private boolean isList() {
- return type instanceof ParameterizedType;
+ final List<UnknownFieldData> setValueTo(T value, List<UnknownFieldData> unknownFields) {
+ if (unknownFields != null) {
+ // Delete all data matching this extension
+ for (int i = unknownFields.size() - 1; i >= 0; i--) {
+ if (isMatch(unknownFields.get(i).tag)) {
+ unknownFields.remove(i);
+ }
+ }
+ }
+
+ if (value != null) {
+ if (unknownFields == null) {
+ unknownFields = new ArrayList<UnknownFieldData>();
+ }
+ if (repeated) {
+ writeDataInto(value, unknownFields);
+ } else {
+ unknownFields.add(writeData(value));
+ }
+ }
+
+ // After deletion or no-op addition (due to 'value' being an array of empty or
+ // null-only elements), unknownFields may be empty. Discard the ArrayList if so.
+ return (unknownFields.size() == 0) ? null : unknownFields;
}
- @SuppressWarnings("unchecked")
- private Class<T> getListType() {
- return (Class<T>) ((ParameterizedType) type).getRawType();
+ protected UnknownFieldData writeData(Object value) {
+ // This implementation is for message/group extensions.
+ byte[] data;
+ try {
+ switch (type) {
+ case TYPE_GROUP:
+ MessageNano groupValue = (MessageNano) value;
+ int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
+ data = new byte[CodedOutputByteBufferNano.computeGroupSizeNoTag(groupValue)
+ + CodedOutputByteBufferNano.computeTagSize(fieldNumber)];
+ CodedOutputByteBufferNano out = CodedOutputByteBufferNano.newInstance(data);
+ out.writeGroupNoTag(groupValue);
+ // The endgroup tag must be included in the data payload.
+ out.writeTag(fieldNumber, WireFormatNano.WIRETYPE_END_GROUP);
+ break;
+ case TYPE_MESSAGE:
+ MessageNano messageValue = (MessageNano) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeMessageSizeNoTag(messageValue)];
+ CodedOutputByteBufferNano.newInstance(data).writeMessageNoTag(messageValue);
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (IOException e) {
+ // Should not happen
+ throw new IllegalStateException(e);
+ }
+ return new UnknownFieldData(tag, data);
+ }
+
+ protected void writeDataInto(T array, List<UnknownFieldData> unknownFields) {
+ // This implementation is for non-packed extensions.
+ int arrayLength = Array.getLength(array);
+ for (int i = 0; i < arrayLength; i++) {
+ Object element = Array.get(array, i);
+ if (element != null) {
+ unknownFields.add(writeData(element));
+ }
+ }
}
/**
- * If the generic type is a list, returns the type of element in the list. Otherwise,
- * returns the actual type.
+ * Represents an extension of a primitive (including enum) type. If there is no primitive
+ * extensions, this subclass will be removable by ProGuard.
*/
- @SuppressWarnings("unchecked")
- private Class<T> getTargetClass() {
- if (isList()) {
- return (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[0];
- }
- return (Class<T>) type;
+ private static class PrimitiveExtension<M extends ExtendableMessageNano<M>, T>
+ extends Extension<M, T> {
+
+ /**
+ * Tag of a piece of non-packed data from the wire compatible with this extension.
+ */
+ private final int nonPackedTag;
+
+ /**
+ * Tag of a piece of packed data from the wire compatible with this extension.
+ * 0 if the type of this extension is not packable.
+ */
+ private final int packedTag;
+
+ public PrimitiveExtension(int type, Class<T> clazz, int tag, boolean repeated,
+ int nonPackedTag, int packedTag) {
+ super(type, clazz, tag, repeated);
+ this.nonPackedTag = nonPackedTag;
+ this.packedTag = packedTag;
+ }
+
+ @Override
+ protected boolean isMatch(int unknownDataTag) {
+ if (repeated) {
+ return unknownDataTag == nonPackedTag || unknownDataTag == packedTag;
+ } else {
+ return unknownDataTag == tag;
+ }
+ }
+
+ @Override
+ protected Object readData(CodedInputByteBufferNano input) {
+ try {
+ switch (type) {
+ case TYPE_DOUBLE:
+ return input.readDouble();
+ case TYPE_FLOAT:
+ return input.readFloat();
+ case TYPE_INT64:
+ return input.readInt64();
+ case TYPE_UINT64:
+ return input.readUInt64();
+ case TYPE_INT32:
+ return input.readInt32();
+ case TYPE_FIXED64:
+ return input.readFixed64();
+ case TYPE_FIXED32:
+ return input.readFixed32();
+ case TYPE_BOOL:
+ return input.readBool();
+ case TYPE_STRING:
+ return input.readString();
+ case TYPE_BYTES:
+ return input.readBytes();
+ case TYPE_UINT32:
+ return input.readUInt32();
+ case TYPE_ENUM:
+ return input.readEnum();
+ case TYPE_SFIXED32:
+ return input.readSFixed32();
+ case TYPE_SFIXED64:
+ return input.readSFixed64();
+ case TYPE_SINT32:
+ return input.readSInt32();
+ case TYPE_SINT64:
+ return input.readSInt64();
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Error reading extension field", e);
+ }
+ }
+
+ @Override
+ protected void readDataInto(UnknownFieldData data, List<Object> resultList) {
+ // This implementation is for primitive typed extensions,
+ // which can read both packed and non-packed data.
+ if (data.tag == nonPackedTag) {
+ resultList.add(readData(CodedInputByteBufferNano.newInstance(data.bytes)));
+ } else {
+ CodedInputByteBufferNano buffer = CodedInputByteBufferNano.newInstance(data.bytes);
+ try {
+ buffer.pushLimit(buffer.readRawVarint32()); // length limit
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Error reading extension field", e);
+ }
+ while (!buffer.isAtEnd()) {
+ resultList.add(readData(buffer));
+ }
+ }
+ }
+
+ @Override
+ protected final UnknownFieldData writeData(Object value) {
+ byte[] data;
+ try {
+ switch (type) {
+ case TYPE_DOUBLE:
+ Double doubleValue = (Double) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeDoubleSizeNoTag(doubleValue)];
+ CodedOutputByteBufferNano.newInstance(data).writeDoubleNoTag(doubleValue);
+ break;
+ case TYPE_FLOAT:
+ Float floatValue = (Float) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeFloatSizeNoTag(floatValue)];
+ CodedOutputByteBufferNano.newInstance(data).writeFloatNoTag(floatValue);
+ break;
+ case TYPE_INT64:
+ Long int64Value = (Long) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeInt64SizeNoTag(int64Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeInt64NoTag(int64Value);
+ break;
+ case TYPE_UINT64:
+ Long uint64Value = (Long) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeUInt64SizeNoTag(uint64Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeUInt64NoTag(uint64Value);
+ break;
+ case TYPE_INT32:
+ Integer int32Value = (Integer) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeInt32SizeNoTag(int32Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeInt32NoTag(int32Value);
+ break;
+ case TYPE_FIXED64:
+ Long fixed64Value = (Long) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeFixed64SizeNoTag(fixed64Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeFixed64NoTag(fixed64Value);
+ break;
+ case TYPE_FIXED32:
+ Integer fixed32Value = (Integer) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeFixed32SizeNoTag(fixed32Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeFixed32NoTag(fixed32Value);
+ break;
+ case TYPE_BOOL:
+ Boolean boolValue = (Boolean) value;
+ data = new byte[CodedOutputByteBufferNano.computeBoolSizeNoTag(boolValue)];
+ CodedOutputByteBufferNano.newInstance(data).writeBoolNoTag(boolValue);
+ break;
+ case TYPE_STRING:
+ String stringValue = (String) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeStringSizeNoTag(stringValue)];
+ CodedOutputByteBufferNano.newInstance(data).writeStringNoTag(stringValue);
+ break;
+ case TYPE_BYTES:
+ byte[] bytesValue = (byte[]) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeBytesSizeNoTag(bytesValue)];
+ CodedOutputByteBufferNano.newInstance(data).writeBytesNoTag(bytesValue);
+ break;
+ case TYPE_UINT32:
+ Integer uint32Value = (Integer) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeUInt32SizeNoTag(uint32Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeUInt32NoTag(uint32Value);
+ break;
+ case TYPE_ENUM:
+ Integer enumValue = (Integer) value;
+ data = new byte[CodedOutputByteBufferNano.computeEnumSizeNoTag(enumValue)];
+ CodedOutputByteBufferNano.newInstance(data).writeEnumNoTag(enumValue);
+ break;
+ case TYPE_SFIXED32:
+ Integer sfixed32Value = (Integer) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeSFixed32SizeNoTag(sfixed32Value)];
+ CodedOutputByteBufferNano.newInstance(data)
+ .writeSFixed32NoTag(sfixed32Value);
+ break;
+ case TYPE_SFIXED64:
+ Long sfixed64Value = (Long) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeSFixed64SizeNoTag(sfixed64Value)];
+ CodedOutputByteBufferNano.newInstance(data)
+ .writeSFixed64NoTag(sfixed64Value);
+ break;
+ case TYPE_SINT32:
+ Integer sint32Value = (Integer) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeSInt32SizeNoTag(sint32Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeSInt32NoTag(sint32Value);
+ break;
+ case TYPE_SINT64:
+ Long sint64Value = (Long) value;
+ data = new byte[
+ CodedOutputByteBufferNano.computeSInt64SizeNoTag(sint64Value)];
+ CodedOutputByteBufferNano.newInstance(data).writeSInt64NoTag(sint64Value);
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (IOException e) {
+ // Should not happen
+ throw new IllegalStateException(e);
+ }
+ return new UnknownFieldData(tag, data);
+ }
+
+ @Override
+ protected void writeDataInto(T array, List<UnknownFieldData> unknownFields) {
+ if (tag == nonPackedTag) {
+ // Use base implementation for non-packed data
+ super.writeDataInto(array, unknownFields);
+ } else if (tag == packedTag) {
+ // Packed. Note that the array element type is guaranteed to be primitive, so there
+ // won't be any null elements, so no null check in this block. First get data size.
+ int arrayLength = Array.getLength(array);
+ int dataSize = 0;
+ switch (type) {
+ case TYPE_BOOL:
+ // Bools are stored as int32 but just as 0 or 1, so 1 byte each.
+ dataSize = arrayLength;
+ break;
+ case TYPE_FIXED32:
+ case TYPE_SFIXED32:
+ case TYPE_FLOAT:
+ dataSize = arrayLength * CodedOutputByteBufferNano.LITTLE_ENDIAN_32_SIZE;
+ break;
+ case TYPE_FIXED64:
+ case TYPE_SFIXED64:
+ case TYPE_DOUBLE:
+ dataSize = arrayLength * CodedOutputByteBufferNano.LITTLE_ENDIAN_64_SIZE;
+ break;
+ case TYPE_INT32:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeInt32SizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ case TYPE_SINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeSInt32SizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ case TYPE_UINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeUInt32SizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ case TYPE_INT64:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeInt64SizeNoTag(
+ Array.getLong(array, i));
+ }
+ break;
+ case TYPE_SINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeSInt64SizeNoTag(
+ Array.getLong(array, i));
+ }
+ break;
+ case TYPE_UINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeUInt64SizeNoTag(
+ Array.getLong(array, i));
+ }
+ break;
+ case TYPE_ENUM:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeEnumSizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Unexpected non-packable type " + type);
+ }
+
+ // Then construct payload.
+ int payloadSize =
+ dataSize + CodedOutputByteBufferNano.computeRawVarint32Size(dataSize);
+ byte[] data = new byte[payloadSize];
+ CodedOutputByteBufferNano output = CodedOutputByteBufferNano.newInstance(data);
+ try {
+ output.writeRawVarint32(dataSize);
+ switch (type) {
+ case TYPE_BOOL:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeBoolNoTag(Array.getBoolean(array, i));
+ }
+ break;
+ case TYPE_FIXED32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeFixed32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_SFIXED32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSFixed32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_FLOAT:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeFloatNoTag(Array.getFloat(array, i));
+ }
+ break;
+ case TYPE_FIXED64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeFixed64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_SFIXED64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSFixed64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_DOUBLE:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeDoubleNoTag(Array.getDouble(array, i));
+ }
+ break;
+ case TYPE_INT32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeInt32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_SINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSInt32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_UINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeUInt32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_INT64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeInt64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_SINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSInt64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_UINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeUInt64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_ENUM:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeEnumNoTag(Array.getInt(array, i));
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Unpackable type " + type);
+ }
+ } catch (IOException e) {
+ // Should not happen.
+ throw new IllegalStateException(e);
+ }
+ unknownFields.add(new UnknownFieldData(tag, data));
+ } else {
+ throw new IllegalArgumentException("Unexpected repeated extension tag " + tag
+ + ", unequal to both non-packed variant " + nonPackedTag
+ + " and packed variant " + packedTag);
+ }
+ }
}
- }
}
diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNano.java b/java/src/main/java/com/google/protobuf/nano/MessageNano.java
index e95c514..82dc6cc 100644
--- a/java/src/main/java/com/google/protobuf/nano/MessageNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/MessageNano.java
@@ -67,16 +67,20 @@ public abstract class MessageNano {
}
/**
- * Serializes the message and writes it to {@code output}. This does not
- * flush or close the stream.
+ * Serializes the message and writes it to {@code output}.
+ *
+ * @param output the output to receive the serialized form.
+ * @throws IOException if an error occurred writing to {@code output}.
*/
- abstract public void writeTo(CodedOutputByteBufferNano output) throws java.io.IOException;
+ public void writeTo(CodedOutputByteBufferNano output) throws IOException {
+ // Does nothing by default. Overridden by subclasses which have data to write.
+ }
/**
* Parse {@code input} as a message of this type and merge it with the
* message being built.
*/
- abstract public MessageNano mergeFrom(final CodedInputByteBufferNano input) throws IOException;
+ public abstract MessageNano mergeFrom(CodedInputByteBufferNano input) throws IOException;
/**
* Serialize to a byte array.
@@ -95,9 +99,8 @@ public abstract class MessageNano {
* write more than length bytes OutOfSpaceException will be thrown
* and if length bytes are not written then IllegalStateException
* is thrown.
- * @return byte array with the serialized data.
*/
- public static final void toByteArray(MessageNano msg, byte [] data, int offset, int length) {
+ public static final void toByteArray(MessageNano msg, byte[] data, int offset, int length) {
try {
final CodedOutputByteBufferNano output =
CodedOutputByteBufferNano.newInstance(data, offset, length);
diff --git a/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java b/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java
index 301ff1d..1ff8f06 100644
--- a/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java
@@ -31,9 +31,6 @@
package com.google.protobuf.nano;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
/**
* This class is used internally by the Protocol Buffer library and generated
@@ -75,21 +72,6 @@ public final class WireFormatNano {
return (fieldNumber << TAG_TYPE_BITS) | wireType;
}
- // Field numbers for feilds in MessageSet wire format.
- static final int MESSAGE_SET_ITEM = 1;
- static final int MESSAGE_SET_TYPE_ID = 2;
- static final int MESSAGE_SET_MESSAGE = 3;
-
- // Tag numbers.
- static final int MESSAGE_SET_ITEM_TAG =
- makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP);
- static final int MESSAGE_SET_ITEM_END_TAG =
- makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP);
- static final int MESSAGE_SET_TYPE_ID_TAG =
- makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT);
- static final int MESSAGE_SET_MESSAGE_TAG =
- makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED);
-
public static final int EMPTY_INT_ARRAY[] = {};
public static final long EMPTY_LONG_ARRAY[] = {};
public static final float EMPTY_FLOAT_ARRAY[] = {};
@@ -114,35 +96,6 @@ public final class WireFormatNano {
}
/**
- * Stores the binary data of an unknown field.
- *
- * <p>Generated messages will call this for unknown fields if the store_unknown_fields
- * option is on.
- *
- * <p>Note that the tag might be a end-group tag (rather than the start of an unknown field) in
- * which case we do not want to add an unknown field entry.
- *
- * @param data a Collection in which to store the data.
- * @param input the input buffer.
- * @param tag the tag of the field.
-
- * @return {@literal true} unless the tag is an end-group tag.
- */
- public static boolean storeUnknownField(
- final List<UnknownFieldData> data,
- final CodedInputByteBufferNano input,
- final int tag) throws IOException {
- int startPos = input.getPosition();
- if (!input.skipField(tag)) {
- return false; // This wasn't an unknown field, it's an end-group tag.
- }
- int endPos = input.getPosition();
- byte[] bytes = input.getData(startPos, endPos - startPos);
- data.add(new UnknownFieldData(tag, bytes));
- return true;
- }
-
- /**
* Computes the array length of a repeated field. We assume that in the common case repeated
* fields are contiguously serialized but we still correctly handle interspersed values of a
* repeated field (but with extra allocations).
@@ -172,193 +125,4 @@ public final class WireFormatNano {
return arrayLength;
}
- /**
- * Decodes the value of an extension.
- */
- public static <T> T getExtension(Extension<T> extension, List<UnknownFieldData> unknownFields) {
- if (unknownFields == null) {
- return null;
- }
- List<UnknownFieldData> dataForField = new ArrayList<UnknownFieldData>();
- for (UnknownFieldData data : unknownFields) {
- if (getTagFieldNumber(data.tag) == extension.fieldNumber) {
- dataForField.add(data);
- }
- }
- if (dataForField.isEmpty()) {
- return null;
- }
-
- if (extension.isRepeatedField) {
- List<Object> result = new ArrayList<Object>(dataForField.size());
- for (UnknownFieldData data : dataForField) {
- result.add(readData(extension.fieldType, data.bytes));
- }
- return extension.listType.cast(result);
- }
-
- // Normal fields. Note that the protobuf docs require us to handle multiple instances
- // of the same field even for fields that are not repeated.
- UnknownFieldData lastData = dataForField.get(dataForField.size() - 1);
- return readData(extension.fieldType, lastData.bytes);
- }
-
- /**
- * Reads (extension) data of the specified type from the specified byte array.
- *
- * @throws IllegalArgumentException if an error occurs while reading the data.
- */
- private static <T> T readData(Class<T> clazz, byte[] data) {
- if (data.length == 0) {
- return null;
- }
- CodedInputByteBufferNano buffer = CodedInputByteBufferNano.newInstance(data);
- try {
- if (clazz == String.class) {
- return clazz.cast(buffer.readString());
- } else if (clazz == Integer.class) {
- return clazz.cast(buffer.readInt32());
- } else if (clazz == Long.class) {
- return clazz.cast(buffer.readInt64());
- } else if (clazz == Boolean.class) {
- return clazz.cast(buffer.readBool());
- } else if (clazz == Float.class) {
- return clazz.cast(buffer.readFloat());
- } else if (clazz == Double.class) {
- return clazz.cast(buffer.readDouble());
- } else if (clazz == byte[].class) {
- return clazz.cast(buffer.readBytes());
- } else if (MessageNano.class.isAssignableFrom(clazz)) {
- try {
- MessageNano message = (MessageNano) clazz.newInstance();
- buffer.readMessage(message);
- return clazz.cast(message);
- } catch (IllegalAccessException e) {
- throw new IllegalArgumentException("Error creating instance of class " + clazz, e);
- } catch (InstantiationException e) {
- throw new IllegalArgumentException("Error creating instance of class " + clazz, e);
- }
- } else {
- throw new IllegalArgumentException("Unhandled extension field type: " + clazz);
- }
- } catch (IOException e) {
- throw new IllegalArgumentException("Error reading extension field", e);
- }
- }
-
- public static <T> void setExtension(Extension<T> extension, T value,
- List<UnknownFieldData> unknownFields) {
- // First, remove all unknown fields with this tag.
- for (Iterator<UnknownFieldData> i = unknownFields.iterator(); i.hasNext();) {
- UnknownFieldData data = i.next();
- if (extension.fieldNumber == getTagFieldNumber(data.tag)) {
- i.remove();
- }
- }
- if (value == null) {
- return;
- }
- // Repeated field.
- if (value instanceof List) {
- for (Object item : (List<?>) value) {
- unknownFields.add(write(extension.fieldNumber, item));
- }
- } else {
- unknownFields.add(write(extension.fieldNumber, value));
- }
- }
-
- /**
- * Writes extension data and returns an {@link UnknownFieldData} containing
- * bytes and a tag.
- *
- * @throws IllegalArgumentException if an error occurs while writing.
- */
- private static UnknownFieldData write(int fieldNumber, Object object) {
- byte[] data;
- int tag;
- Class<?> clazz = object.getClass();
- try {
- if (clazz == String.class) {
- String str = (String) object;
- data = new byte[CodedOutputByteBufferNano.computeStringSizeNoTag(str)];
- CodedOutputByteBufferNano.newInstance(data).writeStringNoTag(str);
- tag = makeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);
- } else if (clazz == Integer.class) {
- Integer integer = (Integer) object;
- data = new byte[CodedOutputByteBufferNano.computeInt32SizeNoTag(integer)];
- CodedOutputByteBufferNano.newInstance(data).writeInt32NoTag(integer);
- tag = makeTag(fieldNumber, WIRETYPE_VARINT);
- } else if (clazz == Long.class) {
- Long longValue = (Long) object;
- data = new byte[CodedOutputByteBufferNano.computeInt64SizeNoTag(longValue)];
- CodedOutputByteBufferNano.newInstance(data).writeInt64NoTag(longValue);
- tag = makeTag(fieldNumber, WIRETYPE_VARINT);
- } else if (clazz == Boolean.class) {
- Boolean boolValue = (Boolean) object;
- data = new byte[CodedOutputByteBufferNano.computeBoolSizeNoTag(boolValue)];
- CodedOutputByteBufferNano.newInstance(data).writeBoolNoTag(boolValue);
- tag = makeTag(fieldNumber, WIRETYPE_VARINT);
- } else if (clazz == Float.class) {
- Float floatValue = (Float) object;
- data = new byte[CodedOutputByteBufferNano.computeFloatSizeNoTag(floatValue)];
- CodedOutputByteBufferNano.newInstance(data).writeFloatNoTag(floatValue);
- tag = makeTag(fieldNumber, WIRETYPE_FIXED32);
- } else if (clazz == Double.class) {
- Double doubleValue = (Double) object;
- data = new byte[CodedOutputByteBufferNano.computeDoubleSizeNoTag(doubleValue)];
- CodedOutputByteBufferNano.newInstance(data).writeDoubleNoTag(doubleValue);
- tag = makeTag(fieldNumber, WIRETYPE_FIXED64);
- } else if (clazz == byte[].class) {
- byte[] byteArrayValue = (byte[]) object;
- data = new byte[CodedOutputByteBufferNano.computeByteArraySizeNoTag(byteArrayValue)];
- CodedOutputByteBufferNano.newInstance(data).writeByteArrayNoTag(byteArrayValue);
- tag = makeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);
- } else if (MessageNano.class.isAssignableFrom(clazz)) {
- MessageNano messageValue = (MessageNano) object;
-
- int messageSize = messageValue.getSerializedSize();
- int delimiterSize = CodedOutputByteBufferNano.computeRawVarint32Size(messageSize);
- data = new byte[messageSize + delimiterSize];
- CodedOutputByteBufferNano buffer = CodedOutputByteBufferNano.newInstance(data);
- buffer.writeRawVarint32(messageSize);
- buffer.writeRawBytes(MessageNano.toByteArray(messageValue));
- tag = makeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);
- } else {
- throw new IllegalArgumentException("Unhandled extension field type: " + clazz);
- }
- } catch (IOException e) {
- throw new IllegalArgumentException(e);
- }
- return new UnknownFieldData(tag, data);
- }
-
- /**
- * Given a set of unknown field data, compute the wire size.
- */
- public static int computeWireSize(List<UnknownFieldData> unknownFields) {
- if (unknownFields == null) {
- return 0;
- }
- int size = 0;
- for (UnknownFieldData unknownField : unknownFields) {
- size += CodedOutputByteBufferNano.computeRawVarint32Size(unknownField.tag);
- size += unknownField.bytes.length;
- }
- return size;
- }
-
- /**
- * Write unknown fields.
- */
- public static void writeUnknownFields(List<UnknownFieldData> unknownFields,
- CodedOutputByteBufferNano outBuffer) throws IOException {
- if (unknownFields == null) {
- return;
- }
- for (UnknownFieldData data : unknownFields) {
- outBuffer.writeTag(getTagFieldNumber(data.tag), getTagWireType(data.tag));
- outBuffer.writeRawBytes(data.bytes);
- }
- }
}