summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/pom.xml18
-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
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java227
7 files changed, 884 insertions, 407 deletions
diff --git a/java/pom.xml b/java/pom.xml
index 2f40b98..b74eaf4 100644
--- a/java/pom.xml
+++ b/java/pom.xml
@@ -154,6 +154,24 @@
<arg value="../src/google/protobuf/unittest_extension_nano.proto" />
</exec>
<exec executable="../src/protoc">
+ <arg value="--javanano_out=store_unknown_fields=true:target/generated-test-sources" />
+ <arg value="--proto_path=../src" />
+ <arg value="--proto_path=src/test/java" />
+ <arg value="../src/google/protobuf/unittest_extension_singular_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=store_unknown_fields=true:target/generated-test-sources" />
+ <arg value="--proto_path=../src" />
+ <arg value="--proto_path=src/test/java" />
+ <arg value="../src/google/protobuf/unittest_extension_repeated_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=store_unknown_fields=true:target/generated-test-sources" />
+ <arg value="--proto_path=../src" />
+ <arg value="--proto_path=src/test/java" />
+ <arg value="../src/google/protobuf/unittest_extension_packed_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
<arg value="--javanano_out=java_nano_generate_has=true,generate_equals=true:target/generated-test-sources" />
<arg value="--proto_path=../src" />
<arg value="--proto_path=src/test/java" />
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);
- }
- }
}
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 4934844..6a761f4 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -50,6 +50,9 @@ import com.google.protobuf.nano.NanoOuterClass;
import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano;
import com.google.protobuf.nano.NanoReferenceTypes;
import com.google.protobuf.nano.NanoRepeatedPackables;
+import com.google.protobuf.nano.PackedExtensions;
+import com.google.protobuf.nano.RepeatedExtensions;
+import com.google.protobuf.nano.SingularExtensions;
import com.google.protobuf.nano.TestRepeatedMergeNano;
import com.google.protobuf.nano.UnittestImportNano;
import com.google.protobuf.nano.UnittestMultipleNano;
@@ -59,10 +62,8 @@ import com.google.protobuf.nano.UnittestSingleNano.SingleMessageNano;
import junit.framework.TestCase;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
-import java.util.List;
/**
* Test nano runtime.
@@ -518,12 +519,12 @@ public class NanoTest extends TestCase {
byte [] serialized = MessageNano.toByteArray(msg);
MessageWithGroup parsed = MessageWithGroup.parseFrom(serialized);
- assertTrue(msg.group != null);
- assertEquals(1, msg.group.a);
+ assertEquals(1, parsed.group.a);
byte [] serialized2 = MessageNano.toByteArray(parsed);
- assertEquals(serialized2.length, serialized.length);
+ assertEquals(serialized.length, serialized2.length);
MessageWithGroup parsed2 = MessageWithGroup.parseFrom(serialized2);
+ assertEquals(1, parsed2.group.a);
}
public void testNanoOptionalNestedMessage() throws Exception {
@@ -2100,6 +2101,7 @@ public class NanoTest extends TestCase {
*/
public void testNanoSingle() throws Exception {
SingleMessageNano msg = new SingleMessageNano();
+ assertNotNull(msg);
}
/**
@@ -2470,12 +2472,14 @@ public class NanoTest extends TestCase {
msg.defaultFloatNan = 0;
byte[] result = MessageNano.toByteArray(msg);
int msgSerializedSize = msg.getSerializedSize();
+ assertTrue(result.length == msgSerializedSize);
assertTrue(msgSerializedSize > 3);
msg.defaultDoubleNan = Double.NaN;
msg.defaultFloatNan = Float.NaN;
result = MessageNano.toByteArray(msg);
msgSerializedSize = msg.getSerializedSize();
+ assertEquals(3, result.length);
assertEquals(3, msgSerializedSize);
}
@@ -2608,57 +2612,163 @@ public class NanoTest extends TestCase {
public void testExtensions() throws Exception {
Extensions.ExtendableMessage message = new Extensions.ExtendableMessage();
message.field = 5;
- message.setExtension(Extensions.someString, "Hello World!");
- message.setExtension(Extensions.someBool, true);
- message.setExtension(Extensions.someInt, 42);
- message.setExtension(Extensions.someLong, 124234234234L);
- message.setExtension(Extensions.someFloat, 42.0f);
- message.setExtension(Extensions.someDouble, 422222.0);
- message.setExtension(Extensions.someEnum, Extensions.FIRST_VALUE);
- AnotherMessage another = new AnotherMessage();
- another.string = "Foo";
- another.value = true;
- message.setExtension(Extensions.someMessage, another);
-
- message.setExtension(Extensions.someRepeatedString, list("a", "bee", "seeya"));
- message.setExtension(Extensions.someRepeatedBool, list(true, false, true));
- message.setExtension(Extensions.someRepeatedInt, list(4, 8, 15, 16, 23, 42));
- message.setExtension(Extensions.someRepeatedLong, list(4L, 8L, 15L, 16L, 23L, 42L));
- message.setExtension(Extensions.someRepeatedFloat, list(1.0f, 3.0f));
- message.setExtension(Extensions.someRepeatedDouble, list(55.133, 3.14159));
- message.setExtension(Extensions.someRepeatedEnum, list(Extensions.FIRST_VALUE,
- Extensions.SECOND_VALUE));
- AnotherMessage second = new AnotherMessage();
- second.string = "Whee";
- second.value = false;
- message.setExtension(Extensions.someRepeatedMessage, list(another, second));
+ int[] int32s = {1, 2};
+ int[] uint32s = {3, 4};
+ int[] sint32s = {-5, -6};
+ long[] int64s = {7, 8};
+ long[] uint64s = {9, 10};
+ long[] sint64s = {-11, -12};
+ int[] fixed32s = {13, 14};
+ int[] sfixed32s = {-15, -16};
+ long[] fixed64s = {17, 18};
+ long[] sfixed64s = {-19, -20};
+ boolean[] bools = {true, false};
+ float[] floats = {2.1f, 2.2f};
+ double[] doubles = {2.3, 2.4};
+ int[] enums = {Extensions.SECOND_VALUE, Extensions.FIRST_VALUE};
+ String[] strings = {"vijfentwintig", "twenty-six"};
+ byte[][] bytess = {{2, 7}, {2, 8}};
+ AnotherMessage another1 = new AnotherMessage();
+ another1.string = "er shi jiu";
+ another1.value = false;
+ AnotherMessage another2 = new AnotherMessage();
+ another2.string = "trente";
+ another2.value = true;
+ AnotherMessage[] messages = {another1, another2};
+ RepeatedExtensions.RepeatedGroup group1 = new RepeatedExtensions.RepeatedGroup();
+ group1.a = 31;
+ RepeatedExtensions.RepeatedGroup group2 = new RepeatedExtensions.RepeatedGroup();
+ group2.a = 32;
+ RepeatedExtensions.RepeatedGroup[] groups = {group1, group2};
+ message.setExtension(RepeatedExtensions.repeatedInt32, int32s);
+ message.setExtension(RepeatedExtensions.repeatedUint32, uint32s);
+ message.setExtension(RepeatedExtensions.repeatedSint32, sint32s);
+ message.setExtension(RepeatedExtensions.repeatedInt64, int64s);
+ message.setExtension(RepeatedExtensions.repeatedUint64, uint64s);
+ message.setExtension(RepeatedExtensions.repeatedSint64, sint64s);
+ message.setExtension(RepeatedExtensions.repeatedFixed32, fixed32s);
+ message.setExtension(RepeatedExtensions.repeatedSfixed32, sfixed32s);
+ message.setExtension(RepeatedExtensions.repeatedFixed64, fixed64s);
+ message.setExtension(RepeatedExtensions.repeatedSfixed64, sfixed64s);
+ message.setExtension(RepeatedExtensions.repeatedBool, bools);
+ message.setExtension(RepeatedExtensions.repeatedFloat, floats);
+ message.setExtension(RepeatedExtensions.repeatedDouble, doubles);
+ message.setExtension(RepeatedExtensions.repeatedEnum, enums);
+ message.setExtension(RepeatedExtensions.repeatedString, strings);
+ message.setExtension(RepeatedExtensions.repeatedBytes, bytess);
+ message.setExtension(RepeatedExtensions.repeatedMessage, messages);
+ message.setExtension(RepeatedExtensions.repeatedGroup, groups);
byte[] data = MessageNano.toByteArray(message);
-
- Extensions.ExtendableMessage deserialized = Extensions.ExtendableMessage.parseFrom(data);
- assertEquals(5, deserialized.field);
- assertEquals("Hello World!", deserialized.getExtension(Extensions.someString));
- assertEquals(Boolean.TRUE, deserialized.getExtension(Extensions.someBool));
- assertEquals(Integer.valueOf(42), deserialized.getExtension(Extensions.someInt));
- assertEquals(Long.valueOf(124234234234L), deserialized.getExtension(Extensions.someLong));
- assertEquals(Float.valueOf(42.0f), deserialized.getExtension(Extensions.someFloat));
- assertEquals(Double.valueOf(422222.0), deserialized.getExtension(Extensions.someDouble));
- assertEquals(Integer.valueOf(Extensions.FIRST_VALUE),
- deserialized.getExtension(Extensions.someEnum));
- assertEquals(another.string, deserialized.getExtension(Extensions.someMessage).string);
- assertEquals(another.value, deserialized.getExtension(Extensions.someMessage).value);
- assertEquals(list("a", "bee", "seeya"), deserialized.getExtension(Extensions.someRepeatedString));
- assertEquals(list(true, false, true), deserialized.getExtension(Extensions.someRepeatedBool));
- assertEquals(list(4, 8, 15, 16, 23, 42), deserialized.getExtension(Extensions.someRepeatedInt));
- assertEquals(list(4L, 8L, 15L, 16L, 23L, 42L), deserialized.getExtension(Extensions.someRepeatedLong));
- assertEquals(list(1.0f, 3.0f), deserialized.getExtension(Extensions.someRepeatedFloat));
- assertEquals(list(55.133, 3.14159), deserialized.getExtension(Extensions.someRepeatedDouble));
- assertEquals(list(Extensions.FIRST_VALUE,
- Extensions.SECOND_VALUE), deserialized.getExtension(Extensions.someRepeatedEnum));
- assertEquals("Foo", deserialized.getExtension(Extensions.someRepeatedMessage).get(0).string);
- assertEquals(true, deserialized.getExtension(Extensions.someRepeatedMessage).get(0).value);
- assertEquals("Whee", deserialized.getExtension(Extensions.someRepeatedMessage).get(1).string);
- assertEquals(false, deserialized.getExtension(Extensions.someRepeatedMessage).get(1).value);
+ message = Extensions.ExtendableMessage.parseFrom(data);
+ assertEquals(5, message.field);
+
+ // Test reading back using SingularExtensions: the retrieved value should equal the last
+ // in each array.
+ assertEquals(int32s[1], (int) message.getExtension(SingularExtensions.someInt32));
+ assertEquals(uint32s[1], (int) message.getExtension(SingularExtensions.someUint32));
+ assertEquals(sint32s[1], (int) message.getExtension(SingularExtensions.someSint32));
+ assertEquals(int64s[1], (long) message.getExtension(SingularExtensions.someInt64));
+ assertEquals(uint64s[1], (long) message.getExtension(SingularExtensions.someUint64));
+ assertEquals(sint64s[1], (long) message.getExtension(SingularExtensions.someSint64));
+ assertEquals(fixed32s[1], (int) message.getExtension(SingularExtensions.someFixed32));
+ assertEquals(sfixed32s[1], (int) message.getExtension(SingularExtensions.someSfixed32));
+ assertEquals(fixed64s[1], (long) message.getExtension(SingularExtensions.someFixed64));
+ assertEquals(sfixed64s[1], (long) message.getExtension(SingularExtensions.someSfixed64));
+ assertEquals(bools[1], (boolean) message.getExtension(SingularExtensions.someBool));
+ assertEquals(floats[1], (float) message.getExtension(SingularExtensions.someFloat));
+ assertEquals(doubles[1], (double) message.getExtension(SingularExtensions.someDouble));
+ assertEquals(enums[1], (int) message.getExtension(SingularExtensions.someEnum));
+ assertEquals(strings[1], message.getExtension(SingularExtensions.someString));
+ assertTrue(Arrays.equals(bytess[1], message.getExtension(SingularExtensions.someBytes)));
+ AnotherMessage deserializedMessage = message.getExtension(SingularExtensions.someMessage);
+ assertEquals(another2.string, deserializedMessage.string);
+ assertEquals(another2.value, deserializedMessage.value);
+ assertEquals(group2.a, message.getExtension(SingularExtensions.someGroup).a);
+
+ // Test reading back using RepeatedExtensions: the arrays should be equal.
+ assertTrue(Arrays.equals(int32s, message.getExtension(RepeatedExtensions.repeatedInt32)));
+ assertTrue(Arrays.equals(uint32s, message.getExtension(RepeatedExtensions.repeatedUint32)));
+ assertTrue(Arrays.equals(sint32s, message.getExtension(RepeatedExtensions.repeatedSint32)));
+ assertTrue(Arrays.equals(int64s, message.getExtension(RepeatedExtensions.repeatedInt64)));
+ assertTrue(Arrays.equals(uint64s, message.getExtension(RepeatedExtensions.repeatedUint64)));
+ assertTrue(Arrays.equals(sint64s, message.getExtension(RepeatedExtensions.repeatedSint64)));
+ assertTrue(Arrays.equals(fixed32s, message.getExtension(RepeatedExtensions.repeatedFixed32)));
+ assertTrue(Arrays.equals(sfixed32s, message.getExtension(RepeatedExtensions.repeatedSfixed32)));
+ assertTrue(Arrays.equals(fixed64s, message.getExtension(RepeatedExtensions.repeatedFixed64)));
+ assertTrue(Arrays.equals(sfixed64s, message.getExtension(RepeatedExtensions.repeatedSfixed64)));
+ assertTrue(Arrays.equals(bools, message.getExtension(RepeatedExtensions.repeatedBool)));
+ assertTrue(Arrays.equals(floats, message.getExtension(RepeatedExtensions.repeatedFloat)));
+ assertTrue(Arrays.equals(doubles, message.getExtension(RepeatedExtensions.repeatedDouble)));
+ assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum)));
+ assertTrue(Arrays.equals(strings, message.getExtension(RepeatedExtensions.repeatedString)));
+ byte[][] deserializedRepeatedBytes = message.getExtension(RepeatedExtensions.repeatedBytes);
+ assertEquals(2, deserializedRepeatedBytes.length);
+ assertTrue(Arrays.equals(bytess[0], deserializedRepeatedBytes[0]));
+ assertTrue(Arrays.equals(bytess[1], deserializedRepeatedBytes[1]));
+ AnotherMessage[] deserializedRepeatedMessage =
+ message.getExtension(RepeatedExtensions.repeatedMessage);
+ assertEquals(2, deserializedRepeatedMessage.length);
+ assertEquals(another1.string, deserializedRepeatedMessage[0].string);
+ assertEquals(another1.value, deserializedRepeatedMessage[0].value);
+ assertEquals(another2.string, deserializedRepeatedMessage[1].string);
+ assertEquals(another2.value, deserializedRepeatedMessage[1].value);
+ RepeatedExtensions.RepeatedGroup[] deserializedRepeatedGroup =
+ message.getExtension(RepeatedExtensions.repeatedGroup);
+ assertEquals(2, deserializedRepeatedGroup.length);
+ assertEquals(group1.a, deserializedRepeatedGroup[0].a);
+ assertEquals(group2.a, deserializedRepeatedGroup[1].a);
+
+ // Test reading back using PackedExtensions: the arrays should be equal, even the fields
+ // are non-packed.
+ assertTrue(Arrays.equals(int32s, message.getExtension(PackedExtensions.packedInt32)));
+ assertTrue(Arrays.equals(uint32s, message.getExtension(PackedExtensions.packedUint32)));
+ assertTrue(Arrays.equals(sint32s, message.getExtension(PackedExtensions.packedSint32)));
+ assertTrue(Arrays.equals(int64s, message.getExtension(PackedExtensions.packedInt64)));
+ assertTrue(Arrays.equals(uint64s, message.getExtension(PackedExtensions.packedUint64)));
+ assertTrue(Arrays.equals(sint64s, message.getExtension(PackedExtensions.packedSint64)));
+ assertTrue(Arrays.equals(fixed32s, message.getExtension(PackedExtensions.packedFixed32)));
+ assertTrue(Arrays.equals(sfixed32s, message.getExtension(PackedExtensions.packedSfixed32)));
+ assertTrue(Arrays.equals(fixed64s, message.getExtension(PackedExtensions.packedFixed64)));
+ assertTrue(Arrays.equals(sfixed64s, message.getExtension(PackedExtensions.packedSfixed64)));
+ assertTrue(Arrays.equals(bools, message.getExtension(PackedExtensions.packedBool)));
+ assertTrue(Arrays.equals(floats, message.getExtension(PackedExtensions.packedFloat)));
+ assertTrue(Arrays.equals(doubles, message.getExtension(PackedExtensions.packedDouble)));
+ assertTrue(Arrays.equals(enums, message.getExtension(PackedExtensions.packedEnum)));
+
+ // Now set the packable extension values using PackedExtensions so they're serialized packed.
+ message.setExtension(PackedExtensions.packedInt32, int32s);
+ message.setExtension(PackedExtensions.packedUint32, uint32s);
+ message.setExtension(PackedExtensions.packedSint32, sint32s);
+ message.setExtension(PackedExtensions.packedInt64, int64s);
+ message.setExtension(PackedExtensions.packedUint64, uint64s);
+ message.setExtension(PackedExtensions.packedSint64, sint64s);
+ message.setExtension(PackedExtensions.packedFixed32, fixed32s);
+ message.setExtension(PackedExtensions.packedSfixed32, sfixed32s);
+ message.setExtension(PackedExtensions.packedFixed64, fixed64s);
+ message.setExtension(PackedExtensions.packedSfixed64, sfixed64s);
+ message.setExtension(PackedExtensions.packedBool, bools);
+ message.setExtension(PackedExtensions.packedFloat, floats);
+ message.setExtension(PackedExtensions.packedDouble, doubles);
+ message.setExtension(PackedExtensions.packedEnum, enums);
+
+ // And read back using non-packed RepeatedExtensions.
+ byte[] data2 = MessageNano.toByteArray(message);
+ message = MessageNano.mergeFrom(new Extensions.ExtendableMessage(), data2);
+ assertTrue(Arrays.equals(int32s, message.getExtension(RepeatedExtensions.repeatedInt32)));
+ assertTrue(Arrays.equals(uint32s, message.getExtension(RepeatedExtensions.repeatedUint32)));
+ assertTrue(Arrays.equals(sint32s, message.getExtension(RepeatedExtensions.repeatedSint32)));
+ assertTrue(Arrays.equals(int64s, message.getExtension(RepeatedExtensions.repeatedInt64)));
+ assertTrue(Arrays.equals(uint64s, message.getExtension(RepeatedExtensions.repeatedUint64)));
+ assertTrue(Arrays.equals(sint64s, message.getExtension(RepeatedExtensions.repeatedSint64)));
+ assertTrue(Arrays.equals(fixed32s, message.getExtension(RepeatedExtensions.repeatedFixed32)));
+ assertTrue(Arrays.equals(sfixed32s, message.getExtension(RepeatedExtensions.repeatedSfixed32)));
+ assertTrue(Arrays.equals(fixed64s, message.getExtension(RepeatedExtensions.repeatedFixed64)));
+ assertTrue(Arrays.equals(sfixed64s, message.getExtension(RepeatedExtensions.repeatedSfixed64)));
+ assertTrue(Arrays.equals(bools, message.getExtension(RepeatedExtensions.repeatedBool)));
+ assertTrue(Arrays.equals(floats, message.getExtension(RepeatedExtensions.repeatedFloat)));
+ assertTrue(Arrays.equals(doubles, message.getExtension(RepeatedExtensions.repeatedDouble)));
+ assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum)));
}
public void testUnknownFields() throws Exception {
@@ -3220,13 +3330,4 @@ public class NanoTest extends TestCase {
}
return sb.toString();
}
-
- private <T> List<T> list(T first, T... remaining) {
- List<T> list = new ArrayList<T>();
- list.add(first);
- for (T item : remaining) {
- list.add(item);
- }
- return list;
- }
}