diff options
Diffstat (limited to 'java')
9 files changed, 4213 insertions, 0 deletions
diff --git a/java/README.txt b/java/README.txt index 8972792..82c1ed5 100644 --- a/java/README.txt +++ b/java/README.txt @@ -260,6 +260,57 @@ This could be compiled using: With the result will be com/example/TestMessages.java +Nano version +============================ + +Nano is even smaller than micro, especially in the number of generated +functions. It is like micro except: + +- No setter/getter/hazzer functions. +- Has state is not available. Outputs all fields not equal to their + default. (See important implications below.) +- CodedInputStreamMicro is renamed to CodedInputByteBufferNano and can + only take byte[] (not InputStream). +- Similar rename from CodedOutputStreamMicro to + CodedOutputByteBufferNano. +- Repeated fields are in arrays, not ArrayList or Vector. +- Unset messages/groups are null, not an immutable empty default + instance. +- Required fields are always serialized. +- toByteArray(...) and mergeFrom(...) are now static functions of + MessageNano. +- "bytes" are of java type byte[]. + +IMPORTANT: If you have fields with defaults + +How fields with defaults are serialized has changed. Because we don't +keep "has" state, any field equal to its default is assumed to be not +set and therefore is not serialized. Consider the situation where we +change the default value of a field. Senders compiled against an older +version of the proto continue to match against the old default, and +don't send values to the receiver even though the receiver assumes the +new default value. Therefore, think carefully about the implications +of changing the default value. + +IMPORTANT: If you have "bytes" fields with non-empty defaults + +Because the byte buffer is now of mutable type byte[], the default +static final cannot be exposed through a public field. Each time a +message's constructor or clear() function is called, the default value +(kept in a private byte[]) is cloned. This causes a small memory +penalty. This is not a problem if the field has no default or is an +empty default. + + +To use nano protobufs: + +- Link with the generated jar file + <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar. +- Invoke with --javanano_out, e.g.: + +../src/protoc '--javanano_out=java_package=src/test/proto/simple-data.proto|my_package,java_outer_classname=src/test/proto/simple-data.proto|OuterName:.' src/test/proto/simple-data.proto + + Usage ===== diff --git a/java/pom.xml b/java/pom.xml index 69d8ff7..0f9451f 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -127,6 +127,16 @@ <arg value="../src/google/protobuf/unittest_recursive_micro.proto" /> <arg value="../src/google/protobuf/unittest_import_micro.proto" /> </exec> + <exec executable="../src/protoc"> + <arg value="--javanano_out=java_package=google/protobuf/unittest_import_nano.proto|com.google.protobuf.nano,java_outer_classname=google/protobuf/unittest_import_nano.proto|UnittestImportNano:target/generated-test-sources" /> + <arg value="--proto_path=../src" /> + <arg value="--proto_path=src/test/java" /> + <arg value="../src/google/protobuf/unittest_nano.proto" /> + <arg value="../src/google/protobuf/unittest_simple_nano.proto" /> + <arg value="../src/google/protobuf/unittest_stringutf8_nano.proto" /> + <arg value="../src/google/protobuf/unittest_recursive_nano.proto" /> + <arg value="../src/google/protobuf/unittest_import_nano.proto" /> + </exec> </tasks> <testSourceRoot>target/generated-test-sources</testSourceRoot> <!--testSourceRoot>target/generated-test-sources/opt-space</testSourceRoot--> @@ -227,5 +237,47 @@ </plugins> </build> </profile> + <profile> + <id>nano</id> + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <includes> + <include>**/MessageNano.java</include> + <include>**/CodedInputByteBufferNano.java</include> + <include>**/CodedOutputByteBufferNano.java</include> + <include>**/InternalNano.java</include> + <include>**/InvalidProtocolBufferNanoException.java</include> + <include>**/WireFormatNano.java</include> + </includes> + <testIncludes> + <testInclude>**/NanoTest.java</testInclude> + <testInclude>**/NanoOuterClass.java</testInclude> + <testInclude>**/SimpleMessageNano.java</testInclude> + <testInclude>**/StringUtf8.java</testInclude> + <testInclude>**/RecursiveMessageNano.java</testInclude> + <testInclude>**/UnittestImportNano.java</testInclude> + </testIncludes> + </configuration> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <includes> + <include>**/NanoTest.java</include> + </includes> + </configuration> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <classifier>nano</classifier> + </configuration> + </plugin> + </plugins> + </build> + </profile> </profiles> </project> diff --git a/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java b/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java new file mode 100644 index 0000000..ed38788 --- /dev/null +++ b/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java @@ -0,0 +1,624 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2013 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano; + +import java.io.IOException; + +/** + * Reads and decodes protocol message fields. + * + * This class contains two kinds of methods: methods that read specific + * protocol message constructs and field types (e.g. {@link #readTag()} and + * {@link #readInt32()}) and methods that read low-level values (e.g. + * {@link #readRawVarint32()} and {@link #readRawBytes}). If you are reading + * encoded protocol messages, you should use the former methods, but if you are + * reading some other format of your own design, use the latter. + * + * @author kenton@google.com Kenton Varda + */ +public final class CodedInputByteBufferNano { + /** + * Create a new CodedInputStream wrapping the given byte array. + */ + public static CodedInputByteBufferNano newInstance(final byte[] buf) { + return newInstance(buf, 0, buf.length); + } + + /** + * Create a new CodedInputStream wrapping the given byte array slice. + */ + public static CodedInputByteBufferNano newInstance(final byte[] buf, final int off, + final int len) { + return new CodedInputByteBufferNano(buf, off, len); + } + + // ----------------------------------------------------------------- + + /** + * Attempt to read a field tag, returning zero if we have reached EOF. + * Protocol message parsers use this to read tags, since a protocol message + * may legally end wherever a tag occurs, and zero is not a valid tag number. + */ + public int readTag() throws IOException { + if (isAtEnd()) { + lastTag = 0; + return 0; + } + + lastTag = readRawVarint32(); + if (lastTag == 0) { + // If we actually read zero, that's not a valid tag. + throw InvalidProtocolBufferNanoException.invalidTag(); + } + return lastTag; + } + + /** + * Verifies that the last call to readTag() returned the given tag value. + * This is used to verify that a nested group ended with the correct + * end tag. + * + * @throws InvalidProtocolBufferNanoException {@code value} does not match the + * last tag. + */ + public void checkLastTagWas(final int value) + throws InvalidProtocolBufferNanoException { + if (lastTag != value) { + throw InvalidProtocolBufferNanoException.invalidEndTag(); + } + } + + /** + * Reads and discards a single field, given its tag value. + * + * @return {@code false} if the tag is an endgroup tag, in which case + * nothing is skipped. Otherwise, returns {@code true}. + */ + public boolean skipField(final int tag) throws IOException { + switch (WireFormatNano.getTagWireType(tag)) { + case WireFormatNano.WIRETYPE_VARINT: + readInt32(); + return true; + case WireFormatNano.WIRETYPE_FIXED64: + readRawLittleEndian64(); + return true; + case WireFormatNano.WIRETYPE_LENGTH_DELIMITED: + skipRawBytes(readRawVarint32()); + return true; + case WireFormatNano.WIRETYPE_START_GROUP: + skipMessage(); + checkLastTagWas( + WireFormatNano.makeTag(WireFormatNano.getTagFieldNumber(tag), + WireFormatNano.WIRETYPE_END_GROUP)); + return true; + case WireFormatNano.WIRETYPE_END_GROUP: + return false; + case WireFormatNano.WIRETYPE_FIXED32: + readRawLittleEndian32(); + return true; + default: + throw InvalidProtocolBufferNanoException.invalidWireType(); + } + } + + /** + * Reads and discards an entire message. This will read either until EOF + * or until an endgroup tag, whichever comes first. + */ + public void skipMessage() throws IOException { + while (true) { + final int tag = readTag(); + if (tag == 0 || !skipField(tag)) { + return; + } + } + } + + // ----------------------------------------------------------------- + + /** Read a {@code double} field value from the stream. */ + public double readDouble() throws IOException { + return Double.longBitsToDouble(readRawLittleEndian64()); + } + + /** Read a {@code float} field value from the stream. */ + public float readFloat() throws IOException { + return Float.intBitsToFloat(readRawLittleEndian32()); + } + + /** Read a {@code uint64} field value from the stream. */ + public long readUInt64() throws IOException { + return readRawVarint64(); + } + + /** Read an {@code int64} field value from the stream. */ + public long readInt64() throws IOException { + return readRawVarint64(); + } + + /** Read an {@code int32} field value from the stream. */ + public int readInt32() throws IOException { + return readRawVarint32(); + } + + /** Read a {@code fixed64} field value from the stream. */ + public long readFixed64() throws IOException { + return readRawLittleEndian64(); + } + + /** Read a {@code fixed32} field value from the stream. */ + public int readFixed32() throws IOException { + return readRawLittleEndian32(); + } + + /** Read a {@code bool} field value from the stream. */ + public boolean readBool() throws IOException { + return readRawVarint32() != 0; + } + + /** Read a {@code string} field value from the stream. */ + public String readString() throws IOException { + final int size = readRawVarint32(); + if (size <= (bufferSize - bufferPos) && size > 0) { + // Fast path: We already have the bytes in a contiguous buffer, so + // just copy directly from it. + final String result = new String(buffer, bufferPos, size, "UTF-8"); + bufferPos += size; + return result; + } else { + // Slow path: Build a byte array first then copy it. + return new String(readRawBytes(size), "UTF-8"); + } + } + + /** Read a {@code group} field value from the stream. */ + public void readGroup(final MessageNano msg, final int fieldNumber) + throws IOException { + if (recursionDepth >= recursionLimit) { + throw InvalidProtocolBufferNanoException.recursionLimitExceeded(); + } + ++recursionDepth; + msg.mergeFrom(this); + checkLastTagWas( + WireFormatNano.makeTag(fieldNumber, WireFormatNano.WIRETYPE_END_GROUP)); + --recursionDepth; + } + + public void readMessage(final MessageNano msg) + throws IOException { + final int length = readRawVarint32(); + if (recursionDepth >= recursionLimit) { + throw InvalidProtocolBufferNanoException.recursionLimitExceeded(); + } + final int oldLimit = pushLimit(length); + ++recursionDepth; + msg.mergeFrom(this); + checkLastTagWas(0); + --recursionDepth; + popLimit(oldLimit); + } + + /** Read a {@code bytes} field value from the stream. */ + public byte[] readBytes() throws IOException { + final int size = readRawVarint32(); + if (size <= (bufferSize - bufferPos) && size > 0) { + // Fast path: We already have the bytes in a contiguous buffer, so + // just copy directly from it. + final byte[] result = new byte[size]; + System.arraycopy(buffer, bufferPos, result, 0, size); + bufferPos += size; + return result; + } else { + // Slow path: Build a byte array first then copy it. + return readRawBytes(size); + } + } + + /** Read a {@code uint32} field value from the stream. */ + public int readUInt32() throws IOException { + return readRawVarint32(); + } + + /** + * Read an enum field value from the stream. Caller is responsible + * for converting the numeric value to an actual enum. + */ + public int readEnum() throws IOException { + return readRawVarint32(); + } + + /** Read an {@code sfixed32} field value from the stream. */ + public int readSFixed32() throws IOException { + return readRawLittleEndian32(); + } + + /** Read an {@code sfixed64} field value from the stream. */ + public long readSFixed64() throws IOException { + return readRawLittleEndian64(); + } + + /** Read an {@code sint32} field value from the stream. */ + public int readSInt32() throws IOException { + return decodeZigZag32(readRawVarint32()); + } + + /** Read an {@code sint64} field value from the stream. */ + public long readSInt64() throws IOException { + return decodeZigZag64(readRawVarint64()); + } + + // ================================================================= + + /** + * Read a raw Varint from the stream. If larger than 32 bits, discard the + * upper bits. + */ + public int readRawVarint32() throws IOException { + byte tmp = readRawByte(); + if (tmp >= 0) { + return tmp; + } + int result = tmp & 0x7f; + if ((tmp = readRawByte()) >= 0) { + result |= tmp << 7; + } else { + result |= (tmp & 0x7f) << 7; + if ((tmp = readRawByte()) >= 0) { + result |= tmp << 14; + } else { + result |= (tmp & 0x7f) << 14; + if ((tmp = readRawByte()) >= 0) { + result |= tmp << 21; + } else { + result |= (tmp & 0x7f) << 21; + result |= (tmp = readRawByte()) << 28; + if (tmp < 0) { + // Discard upper 32 bits. + for (int i = 0; i < 5; i++) { + if (readRawByte() >= 0) { + return result; + } + } + throw InvalidProtocolBufferNanoException.malformedVarint(); + } + } + } + } + return result; + } + + /** Read a raw Varint from the stream. */ + public long readRawVarint64() throws IOException { + int shift = 0; + long result = 0; + while (shift < 64) { + final byte b = readRawByte(); + result |= (long)(b & 0x7F) << shift; + if ((b & 0x80) == 0) { + return result; + } + shift += 7; + } + throw InvalidProtocolBufferNanoException.malformedVarint(); + } + + /** Read a 32-bit little-endian integer from the stream. */ + public int readRawLittleEndian32() throws IOException { + final byte b1 = readRawByte(); + final byte b2 = readRawByte(); + final byte b3 = readRawByte(); + final byte b4 = readRawByte(); + return ((b1 & 0xff) ) | + ((b2 & 0xff) << 8) | + ((b3 & 0xff) << 16) | + ((b4 & 0xff) << 24); + } + + /** Read a 64-bit little-endian integer from the stream. */ + public long readRawLittleEndian64() throws IOException { + final byte b1 = readRawByte(); + final byte b2 = readRawByte(); + final byte b3 = readRawByte(); + final byte b4 = readRawByte(); + final byte b5 = readRawByte(); + final byte b6 = readRawByte(); + final byte b7 = readRawByte(); + final byte b8 = readRawByte(); + return (((long)b1 & 0xff) ) | + (((long)b2 & 0xff) << 8) | + (((long)b3 & 0xff) << 16) | + (((long)b4 & 0xff) << 24) | + (((long)b5 & 0xff) << 32) | + (((long)b6 & 0xff) << 40) | + (((long)b7 & 0xff) << 48) | + (((long)b8 & 0xff) << 56); + } + + /** + * Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers + * into values that can be efficiently encoded with varint. (Otherwise, + * negative values must be sign-extended to 64 bits to be varint encoded, + * thus always taking 10 bytes on the wire.) + * + * @param n An unsigned 32-bit integer, stored in a signed int because + * Java has no explicit unsigned support. + * @return A signed 32-bit integer. + */ + public static int decodeZigZag32(final int n) { + return (n >>> 1) ^ -(n & 1); + } + + /** + * Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers + * into values that can be efficiently encoded with varint. (Otherwise, + * negative values must be sign-extended to 64 bits to be varint encoded, + * thus always taking 10 bytes on the wire.) + * + * @param n An unsigned 64-bit integer, stored in a signed int because + * Java has no explicit unsigned support. + * @return A signed 64-bit integer. + */ + public static long decodeZigZag64(final long n) { + return (n >>> 1) ^ -(n & 1); + } + + // ----------------------------------------------------------------- + + private final byte[] buffer; + private int bufferStart; + private int bufferSize; + private int bufferSizeAfterLimit; + private int bufferPos; + private int lastTag; + + /** The absolute position of the end of the current message. */ + private int currentLimit = Integer.MAX_VALUE; + + /** See setRecursionLimit() */ + private int recursionDepth; + private int recursionLimit = DEFAULT_RECURSION_LIMIT; + + /** See setSizeLimit() */ + private int sizeLimit = DEFAULT_SIZE_LIMIT; + + private static final int DEFAULT_RECURSION_LIMIT = 64; + private static final int DEFAULT_SIZE_LIMIT = 64 << 20; // 64MB + + private CodedInputByteBufferNano(final byte[] buffer, final int off, final int len) { + this.buffer = buffer; + bufferStart = off; + bufferSize = off + len; + bufferPos = off; + } + + /** + * Set the maximum message recursion depth. In order to prevent malicious + * messages from causing stack overflows, {@code CodedInputStream} limits + * how deeply messages may be nested. The default limit is 64. + * + * @return the old limit. + */ + public int setRecursionLimit(final int limit) { + if (limit < 0) { + throw new IllegalArgumentException( + "Recursion limit cannot be negative: " + limit); + } + final int oldLimit = recursionLimit; + recursionLimit = limit; + return oldLimit; + } + + /** + * Set the maximum message size. In order to prevent malicious + * messages from exhausting memory or causing integer overflows, + * {@code CodedInputStream} limits how large a message may be. + * The default limit is 64MB. You should set this limit as small + * as you can without harming your app's functionality. Note that + * size limits only apply when reading from an {@code InputStream}, not + * when constructed around a raw byte array. + * <p> + * If you want to read several messages from a single CodedInputStream, you + * could call {@link #resetSizeCounter()} after each one to avoid hitting the + * size limit. + * + * @return the old limit. + */ + public int setSizeLimit(final int limit) { + if (limit < 0) { + throw new IllegalArgumentException( + "Size limit cannot be negative: " + limit); + } + final int oldLimit = sizeLimit; + sizeLimit = limit; + return oldLimit; + } + + /** + * Resets the current size counter to zero (see {@link #setSizeLimit(int)}). + */ + public void resetSizeCounter() { + } + + /** + * Sets {@code currentLimit} to (current position) + {@code byteLimit}. This + * is called when descending into a length-delimited embedded message. + * + * @return the old limit. + */ + public int pushLimit(int byteLimit) throws InvalidProtocolBufferNanoException { + if (byteLimit < 0) { + throw InvalidProtocolBufferNanoException.negativeSize(); + } + byteLimit += bufferPos; + final int oldLimit = currentLimit; + if (byteLimit > oldLimit) { + throw InvalidProtocolBufferNanoException.truncatedMessage(); + } + currentLimit = byteLimit; + + recomputeBufferSizeAfterLimit(); + + return oldLimit; + } + + private void recomputeBufferSizeAfterLimit() { + bufferSize += bufferSizeAfterLimit; + final int bufferEnd = bufferSize; + if (bufferEnd > currentLimit) { + // Limit is in current buffer. + bufferSizeAfterLimit = bufferEnd - currentLimit; + bufferSize -= bufferSizeAfterLimit; + } else { + bufferSizeAfterLimit = 0; + } + } + + /** + * Discards the current limit, returning to the previous limit. + * + * @param oldLimit The old limit, as returned by {@code pushLimit}. + */ + public void popLimit(final int oldLimit) { + currentLimit = oldLimit; + recomputeBufferSizeAfterLimit(); + } + + /** + * Returns the number of bytes to be read before the current limit. + * If no limit is set, returns -1. + */ + public int getBytesUntilLimit() { + if (currentLimit == Integer.MAX_VALUE) { + return -1; + } + + final int currentAbsolutePosition = bufferPos; + return currentLimit - currentAbsolutePosition; + } + + /** + * Returns true if the stream has reached the end of the input. This is the + * case if either the end of the underlying input source has been reached or + * if the stream has reached a limit created using {@link #pushLimit(int)}. + */ + public boolean isAtEnd() { + return bufferPos == bufferSize; + } + + /** + * Get current position in buffer relative to beginning offset. + */ + public int getPosition() { + return bufferPos - bufferStart; + } + + /** + * Rewind to previous position. Cannot go forward. + */ + public void rewindToPosition(int position) { + if (position > bufferPos - bufferStart) { + throw new IllegalArgumentException( + "Position " + position + " is beyond current " + (bufferPos - bufferStart)); + } + if (position < 0) { + throw new IllegalArgumentException("Bad position " + position); + } + bufferPos = bufferStart + position; + } + + /** + * Read one byte from the input. + * + * @throws InvalidProtocolBufferNanoException The end of the stream or the current + * limit was reached. + */ + public byte readRawByte() throws IOException { + if (bufferPos == bufferSize) { + throw InvalidProtocolBufferNanoException.truncatedMessage(); + } + return buffer[bufferPos++]; + } + + /** + * Read a fixed size of bytes from the input. + * + * @throws InvalidProtocolBufferNanoException The end of the stream or the current + * limit was reached. + */ + public byte[] readRawBytes(final int size) throws IOException { + if (size < 0) { + throw InvalidProtocolBufferNanoException.negativeSize(); + } + + if (bufferPos + size > currentLimit) { + // Read to the end of the stream anyway. + skipRawBytes(currentLimit - bufferPos); + // Then fail. + throw InvalidProtocolBufferNanoException.truncatedMessage(); + } + + if (size <= bufferSize - bufferPos) { + // We have all the bytes we need already. + final byte[] bytes = new byte[size]; + System.arraycopy(buffer, bufferPos, bytes, 0, size); + bufferPos += size; + return bytes; + } else { + throw InvalidProtocolBufferNanoException.truncatedMessage(); + } + } + + /** + * Reads and discards {@code size} bytes. + * + * @throws InvalidProtocolBufferNanoException The end of the stream or the current + * limit was reached. + */ + public void skipRawBytes(final int size) throws IOException { + if (size < 0) { + throw InvalidProtocolBufferNanoException.negativeSize(); + } + + if (bufferPos + size > currentLimit) { + // Read to the end of the stream anyway. + skipRawBytes(currentLimit - bufferPos); + // Then fail. + throw InvalidProtocolBufferNanoException.truncatedMessage(); + } + + if (size <= bufferSize - bufferPos) { + // We have all the bytes we need already. + bufferPos += size; + } else { + throw InvalidProtocolBufferNanoException.truncatedMessage(); + } + } +} diff --git a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java new file mode 100644 index 0000000..769bb19 --- /dev/null +++ b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java @@ -0,0 +1,910 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2013 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +/** + * Encodes and writes protocol message fields. + * + * <p>This class contains two kinds of methods: methods that write specific + * protocol message constructs and field types (e.g. {@link #writeTag} and + * {@link #writeInt32}) and methods that write low-level values (e.g. + * {@link #writeRawVarint32} and {@link #writeRawBytes}). If you are + * writing encoded protocol messages, you should use the former methods, but if + * you are writing some other format of your own design, use the latter. + * + * <p>This class is totally unsynchronized. + * + * @author kneton@google.com Kenton Varda + */ +public final class CodedOutputByteBufferNano { + private final byte[] buffer; + private final int limit; + private int position; + + private CodedOutputByteBufferNano(final byte[] buffer, final int offset, + final int length) { + this.buffer = buffer; + position = offset; + limit = offset + length; + } + + /** + * Create a new {@code CodedOutputStream} that writes directly to the given + * byte array. If more bytes are written than fit in the array, + * {@link OutOfSpaceException} will be thrown. Writing directly to a flat + * array is faster than writing to an {@code OutputStream}. + */ + public static CodedOutputByteBufferNano newInstance(final byte[] flatArray) { + return newInstance(flatArray, 0, flatArray.length); + } + + /** + * Create a new {@code CodedOutputStream} that writes directly to the given + * byte array slice. If more bytes are written than fit in the slice, + * {@link OutOfSpaceException} will be thrown. Writing directly to a flat + * array is faster than writing to an {@code OutputStream}. + */ + public static CodedOutputByteBufferNano newInstance(final byte[] flatArray, + final int offset, + final int length) { + return new CodedOutputByteBufferNano(flatArray, offset, length); + } + + // ----------------------------------------------------------------- + + /** Write a {@code double} field, including tag, to the stream. */ + public void writeDouble(final int fieldNumber, final double value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED64); + writeDoubleNoTag(value); + } + + /** Write a {@code float} field, including tag, to the stream. */ + public void writeFloat(final int fieldNumber, final float value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED32); + writeFloatNoTag(value); + } + + /** Write a {@code uint64} field, including tag, to the stream. */ + public void writeUInt64(final int fieldNumber, final long value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeUInt64NoTag(value); + } + + /** Write an {@code int64} field, including tag, to the stream. */ + public void writeInt64(final int fieldNumber, final long value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeInt64NoTag(value); + } + + /** Write an {@code int32} field, including tag, to the stream. */ + public void writeInt32(final int fieldNumber, final int value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeInt32NoTag(value); + } + + /** Write a {@code fixed64} field, including tag, to the stream. */ + public void writeFixed64(final int fieldNumber, final long value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED64); + writeFixed64NoTag(value); + } + + /** Write a {@code fixed32} field, including tag, to the stream. */ + public void writeFixed32(final int fieldNumber, final int value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED32); + writeFixed32NoTag(value); + } + + /** Write a {@code bool} field, including tag, to the stream. */ + public void writeBool(final int fieldNumber, final boolean value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeBoolNoTag(value); + } + + /** Write a {@code string} field, including tag, to the stream. */ + public void writeString(final int fieldNumber, final String value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_LENGTH_DELIMITED); + writeStringNoTag(value); + } + + /** Write a {@code group} field, including tag, to the stream. */ + public void writeGroup(final int fieldNumber, final MessageNano value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_START_GROUP); + writeGroupNoTag(value); + writeTag(fieldNumber, WireFormatNano.WIRETYPE_END_GROUP); + } + + /** Write an embedded message field, including tag, to the stream. */ + public void writeMessage(final int fieldNumber, final MessageNano value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value); + } + + /** Write a {@code bytes} field, including tag, to the stream. */ + public void writeBytes(final int fieldNumber, final byte[] value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_LENGTH_DELIMITED); + 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 { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeUInt32NoTag(value); + } + + /** + * Write an enum field, including tag, to the stream. Caller is responsible + * for converting the enum value to its numeric value. + */ + public void writeEnum(final int fieldNumber, final int value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeEnumNoTag(value); + } + + /** Write an {@code sfixed32} field, including tag, to the stream. */ + public void writeSFixed32(final int fieldNumber, final int value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED32); + writeSFixed32NoTag(value); + } + + /** Write an {@code sfixed64} field, including tag, to the stream. */ + public void writeSFixed64(final int fieldNumber, final long value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED64); + writeSFixed64NoTag(value); + } + + /** Write an {@code sint32} field, including tag, to the stream. */ + public void writeSInt32(final int fieldNumber, final int value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeSInt32NoTag(value); + } + + /** Write an {@code sint64} field, including tag, to the stream. */ + public void writeSInt64(final int fieldNumber, final long value) + throws IOException { + writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT); + writeSInt64NoTag(value); + } + + /** + * Write a MessageSet extension field to the stream. For historical reasons, + * the wire format differs from normal fields. + */ +// public void writeMessageSetExtension(final int fieldNumber, +// final MessageMicro value) +// throws IOException { +// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_START_GROUP); +// writeUInt32(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber); +// writeMessage(WireFormatMicro.MESSAGE_SET_MESSAGE, value); +// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_END_GROUP); +// } + + /** + * Write an unparsed MessageSet extension field to the stream. For + * historical reasons, the wire format differs from normal fields. + */ +// public void writeRawMessageSetExtension(final int fieldNumber, +// final ByteStringMicro value) +// throws IOException { +// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_START_GROUP); +// writeUInt32(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber); +// writeBytes(WireFormatMicro.MESSAGE_SET_MESSAGE, value); +// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_END_GROUP); +// } + + // ----------------------------------------------------------------- + + /** Write a {@code double} field to the stream. */ + public void writeDoubleNoTag(final double value) throws IOException { + writeRawLittleEndian64(Double.doubleToLongBits(value)); + } + + /** Write a {@code float} field to the stream. */ + public void writeFloatNoTag(final float value) throws IOException { + writeRawLittleEndian32(Float.floatToIntBits(value)); + } + + /** Write a {@code uint64} field to the stream. */ + public void writeUInt64NoTag(final long value) throws IOException { + writeRawVarint64(value); + } + + /** Write an {@code int64} field to the stream. */ + public void writeInt64NoTag(final long value) throws IOException { + writeRawVarint64(value); + } + + /** Write an {@code int32} field to the stream. */ + public void writeInt32NoTag(final int value) throws IOException { + if (value >= 0) { + writeRawVarint32(value); + } else { + // Must sign-extend. + writeRawVarint64(value); + } + } + + /** Write a {@code fixed64} field to the stream. */ + public void writeFixed64NoTag(final long value) throws IOException { + writeRawLittleEndian64(value); + } + + /** Write a {@code fixed32} field to the stream. */ + public void writeFixed32NoTag(final int value) throws IOException { + writeRawLittleEndian32(value); + } + + /** Write a {@code bool} field to the stream. */ + public void writeBoolNoTag(final boolean value) throws IOException { + writeRawByte(value ? 1 : 0); + } + + /** Write a {@code string} field to the stream. */ + public void writeStringNoTag(final String value) throws IOException { + // Unfortunately there does not appear to be any way to tell Java to encode + // UTF-8 directly into our buffer, so we have to let it create its own byte + // array and then copy. + final byte[] bytes = value.getBytes("UTF-8"); + writeRawVarint32(bytes.length); + writeRawBytes(bytes); + } + + /** Write a {@code group} field to the stream. */ + public void writeGroupNoTag(final MessageNano value) throws IOException { + value.writeTo(this); + } + + /** Write an embedded message field to the stream. */ + public void writeMessageNoTag(final MessageNano value) throws IOException { + writeRawVarint32(value.getCachedSize()); + value.writeTo(this); + } + + /** Write a {@code bytes} field to the stream. */ + public void writeBytesNoTag(final byte[] value) throws IOException { + writeRawVarint32(value.length); + 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); + } + + /** + * Write an enum field to the stream. Caller is responsible + * for converting the enum value to its numeric value. + */ + public void writeEnumNoTag(final int value) throws IOException { + writeRawVarint32(value); + } + + /** Write an {@code sfixed32} field to the stream. */ + public void writeSFixed32NoTag(final int value) throws IOException { + writeRawLittleEndian32(value); + } + + /** Write an {@code sfixed64} field to the stream. */ + public void writeSFixed64NoTag(final long value) throws IOException { + writeRawLittleEndian64(value); + } + + /** Write an {@code sint32} field to the stream. */ + public void writeSInt32NoTag(final int value) throws IOException { + writeRawVarint32(encodeZigZag32(value)); + } + + /** Write an {@code sint64} field to the stream. */ + public void writeSInt64NoTag(final long value) throws IOException { + writeRawVarint64(encodeZigZag64(value)); + } + + // ================================================================= + + /** + * Compute the number of bytes that would be needed to encode a + * {@code double} field, including tag. + */ + public static int computeDoubleSize(final int fieldNumber, + final double value) { + return computeTagSize(fieldNumber) + computeDoubleSizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code float} field, including tag. + */ + public static int computeFloatSize(final int fieldNumber, final float value) { + return computeTagSize(fieldNumber) + computeFloatSizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code uint64} field, including tag. + */ + public static int computeUInt64Size(final int fieldNumber, final long value) { + return computeTagSize(fieldNumber) + computeUInt64SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code int64} field, including tag. + */ + public static int computeInt64Size(final int fieldNumber, final long value) { + return computeTagSize(fieldNumber) + computeInt64SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code int32} field, including tag. + */ + public static int computeInt32Size(final int fieldNumber, final int value) { + return computeTagSize(fieldNumber) + computeInt32SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code fixed64} field, including tag. + */ + public static int computeFixed64Size(final int fieldNumber, + final long value) { + return computeTagSize(fieldNumber) + computeFixed64SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code fixed32} field, including tag. + */ + public static int computeFixed32Size(final int fieldNumber, + final int value) { + return computeTagSize(fieldNumber) + computeFixed32SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code bool} field, including tag. + */ + public static int computeBoolSize(final int fieldNumber, + final boolean value) { + return computeTagSize(fieldNumber) + computeBoolSizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code string} field, including tag. + */ + public static int computeStringSize(final int fieldNumber, + final String value) { + return computeTagSize(fieldNumber) + computeStringSizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code group} field, including tag. + */ + public static int computeGroupSize(final int fieldNumber, + final MessageNano value) { + return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * embedded message field, including tag. + */ + public static int computeMessageSize(final int fieldNumber, + final MessageNano value) { + return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code bytes} field, including tag. + */ + public static int computeBytesSize(final int fieldNumber, + final byte[] value) { + return computeTagSize(fieldNumber) + computeBytesSizeNoTag(value); + } + + /** + * 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) { + return computeTagSize(fieldNumber) + computeUInt32SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * enum field, including tag. Caller is responsible for converting the + * enum value to its numeric value. + */ + public static int computeEnumSize(final int fieldNumber, final int value) { + return computeTagSize(fieldNumber) + computeEnumSizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sfixed32} field, including tag. + */ + public static int computeSFixed32Size(final int fieldNumber, + final int value) { + return computeTagSize(fieldNumber) + computeSFixed32SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sfixed64} field, including tag. + */ + public static int computeSFixed64Size(final int fieldNumber, + final long value) { + return computeTagSize(fieldNumber) + computeSFixed64SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sint32} field, including tag. + */ + public static int computeSInt32Size(final int fieldNumber, final int value) { + return computeTagSize(fieldNumber) + computeSInt32SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sint64} field, including tag. + */ + public static int computeSInt64Size(final int fieldNumber, final long value) { + return computeTagSize(fieldNumber) + computeSInt64SizeNoTag(value); + } + + /** + * Compute the number of bytes that would be needed to encode a + * MessageSet extension to the stream. For historical reasons, + * the wire format differs from normal fields. + */ +// public static int computeMessageSetExtensionSize( +// final int fieldNumber, final MessageMicro value) { +// return computeTagSize(WireFormatMicro.MESSAGE_SET_ITEM) * 2 + +// computeUInt32Size(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber) + +// computeMessageSize(WireFormatMicro.MESSAGE_SET_MESSAGE, value); +// } + + /** + * Compute the number of bytes that would be needed to encode an + * unparsed MessageSet extension field to the stream. For + * historical reasons, the wire format differs from normal fields. + */ +// public static int computeRawMessageSetExtensionSize( +// final int fieldNumber, final ByteStringMicro value) { +// return computeTagSize(WireFormatMicro.MESSAGE_SET_ITEM) * 2 + +// computeUInt32Size(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber) + +// computeBytesSize(WireFormatMicro.MESSAGE_SET_MESSAGE, value); +// } + + // ----------------------------------------------------------------- + + /** + * Compute the number of bytes that would be needed to encode a + * {@code double} field, including tag. + */ + public static int computeDoubleSizeNoTag(final double value) { + return LITTLE_ENDIAN_64_SIZE; + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code float} field, including tag. + */ + public static int computeFloatSizeNoTag(final float value) { + return LITTLE_ENDIAN_32_SIZE; + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code uint64} field, including tag. + */ + public static int computeUInt64SizeNoTag(final long value) { + return computeRawVarint64Size(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code int64} field, including tag. + */ + public static int computeInt64SizeNoTag(final long value) { + return computeRawVarint64Size(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code int32} field, including tag. + */ + public static int computeInt32SizeNoTag(final int value) { + if (value >= 0) { + return computeRawVarint32Size(value); + } else { + // Must sign-extend. + return 10; + } + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code fixed64} field. + */ + public static int computeFixed64SizeNoTag(final long value) { + return LITTLE_ENDIAN_64_SIZE; + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code fixed32} field. + */ + public static int computeFixed32SizeNoTag(final int value) { + return LITTLE_ENDIAN_32_SIZE; + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code bool} field. + */ + public static int computeBoolSizeNoTag(final boolean value) { + return 1; + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code string} field. + */ + public static int computeStringSizeNoTag(final String value) { + try { + final byte[] bytes = value.getBytes("UTF-8"); + return computeRawVarint32Size(bytes.length) + + bytes.length; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("UTF-8 not supported."); + } + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code group} field. + */ + public static int computeGroupSizeNoTag(final MessageNano value) { + return value.getSerializedSize(); + } + + /** + * Compute the number of bytes that would be needed to encode an embedded + * message field. + */ + public static int computeMessageSizeNoTag(final MessageNano value) { + final int size = value.getSerializedSize(); + return computeRawVarint32Size(size) + size; + } + + /** + * Compute the number of bytes that would be needed to encode a + * {@code bytes} field. + */ + public static int computeBytesSizeNoTag(final byte[] value) { + return computeRawVarint32Size(value.length) + value.length; + } + + /** + * 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) { + return computeRawVarint32Size(value); + } + + /** + * Compute the number of bytes that would be needed to encode an enum field. + * Caller is responsible for converting the enum value to its numeric value. + */ + public static int computeEnumSizeNoTag(final int value) { + return computeRawVarint32Size(value); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sfixed32} field. + */ + public static int computeSFixed32SizeNoTag(final int value) { + return LITTLE_ENDIAN_32_SIZE; + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sfixed64} field. + */ + public static int computeSFixed64SizeNoTag(final long value) { + return LITTLE_ENDIAN_64_SIZE; + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sint32} field. + */ + public static int computeSInt32SizeNoTag(final int value) { + return computeRawVarint32Size(encodeZigZag32(value)); + } + + /** + * Compute the number of bytes that would be needed to encode an + * {@code sint64} field. + */ + public static int computeSInt64SizeNoTag(final long value) { + return computeRawVarint64Size(encodeZigZag64(value)); + } + + // ================================================================= + + /** + * If writing to a flat array, return the space left in the array. + * Otherwise, throws {@code UnsupportedOperationException}. + */ + public int spaceLeft() { + return limit - position; + } + + /** + * Verifies that {@link #spaceLeft()} returns zero. It's common to create + * a byte array that is exactly big enough to hold a message, then write to + * it with a {@code CodedOutputStream}. Calling {@code checkNoSpaceLeft()} + * after writing verifies that the message was actually as big as expected, + * which can help catch bugs. + */ + public void checkNoSpaceLeft() { + if (spaceLeft() != 0) { + throw new IllegalStateException( + "Did not write as much data as expected."); + } + } + + /** + * If you create a CodedOutputStream around a simple flat array, you must + * not attempt to write more bytes than the array has space. Otherwise, + * this exception will be thrown. + */ + public static class OutOfSpaceException extends IOException { + private static final long serialVersionUID = -6947486886997889499L; + + OutOfSpaceException(int position, int limit) { + super("CodedOutputStream was writing to a flat byte array and ran " + + "out of space (pos " + position + " limit " + limit + ")."); + } + } + + /** Write a single byte. */ + public void writeRawByte(final byte value) throws IOException { + if (position == limit) { + // We're writing to a single buffer. + throw new OutOfSpaceException(position, limit); + } + + buffer[position++] = value; + } + + /** Write a single byte, represented by an integer value. */ + public void writeRawByte(final int value) throws IOException { + writeRawByte((byte) value); + } + + /** Write an array of bytes. */ + public void writeRawBytes(final byte[] value) throws IOException { + writeRawBytes(value, 0, value.length); + } + + /** Write part of an array of bytes. */ + public void writeRawBytes(final byte[] value, int offset, int length) + throws IOException { + if (limit - position >= length) { + // We have room in the current buffer. + System.arraycopy(value, offset, buffer, position, length); + position += length; + } else { + // We're writing to a single buffer. + throw new OutOfSpaceException(position, limit); + } + } + + /** Encode and write a tag. */ + public void writeTag(final int fieldNumber, final int wireType) + throws IOException { + writeRawVarint32(WireFormatNano.makeTag(fieldNumber, wireType)); + } + + /** Compute the number of bytes that would be needed to encode a tag. */ + public static int computeTagSize(final int fieldNumber) { + return computeRawVarint32Size(WireFormatNano.makeTag(fieldNumber, 0)); + } + + /** + * Encode and write a varint. {@code value} is treated as + * unsigned, so it won't be sign-extended if negative. + */ + public void writeRawVarint32(int value) throws IOException { + while (true) { + if ((value & ~0x7F) == 0) { + writeRawByte(value); + return; + } else { + writeRawByte((value & 0x7F) | 0x80); + value >>>= 7; + } + } + } + + /** + * Compute the number of bytes that would be needed to encode a varint. + * {@code value} is treated as unsigned, so it won't be sign-extended if + * negative. + */ + public static int computeRawVarint32Size(final int value) { + if ((value & (0xffffffff << 7)) == 0) return 1; + if ((value & (0xffffffff << 14)) == 0) return 2; + if ((value & (0xffffffff << 21)) == 0) return 3; + if ((value & (0xffffffff << 28)) == 0) return 4; + return 5; + } + + /** Encode and write a varint. */ + public void writeRawVarint64(long value) throws IOException { + while (true) { + if ((value & ~0x7FL) == 0) { + writeRawByte((int)value); + return; + } else { + writeRawByte(((int)value & 0x7F) | 0x80); + value >>>= 7; + } + } + } + + /** Compute the number of bytes that would be needed to encode a varint. */ + public static int computeRawVarint64Size(final long value) { + if ((value & (0xffffffffffffffffL << 7)) == 0) return 1; + if ((value & (0xffffffffffffffffL << 14)) == 0) return 2; + if ((value & (0xffffffffffffffffL << 21)) == 0) return 3; + if ((value & (0xffffffffffffffffL << 28)) == 0) return 4; + if ((value & (0xffffffffffffffffL << 35)) == 0) return 5; + if ((value & (0xffffffffffffffffL << 42)) == 0) return 6; + if ((value & (0xffffffffffffffffL << 49)) == 0) return 7; + if ((value & (0xffffffffffffffffL << 56)) == 0) return 8; + if ((value & (0xffffffffffffffffL << 63)) == 0) return 9; + return 10; + } + + /** Write a little-endian 32-bit integer. */ + public void writeRawLittleEndian32(final int value) throws IOException { + writeRawByte((value ) & 0xFF); + writeRawByte((value >> 8) & 0xFF); + writeRawByte((value >> 16) & 0xFF); + writeRawByte((value >> 24) & 0xFF); + } + + public static final int LITTLE_ENDIAN_32_SIZE = 4; + + /** Write a little-endian 64-bit integer. */ + public void writeRawLittleEndian64(final long value) throws IOException { + writeRawByte((int)(value ) & 0xFF); + writeRawByte((int)(value >> 8) & 0xFF); + writeRawByte((int)(value >> 16) & 0xFF); + writeRawByte((int)(value >> 24) & 0xFF); + writeRawByte((int)(value >> 32) & 0xFF); + writeRawByte((int)(value >> 40) & 0xFF); + writeRawByte((int)(value >> 48) & 0xFF); + writeRawByte((int)(value >> 56) & 0xFF); + } + + public static final int LITTLE_ENDIAN_64_SIZE = 8; + + /** + * Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers + * into values that can be efficiently encoded with varint. (Otherwise, + * negative values must be sign-extended to 64 bits to be varint encoded, + * thus always taking 10 bytes on the wire.) + * + * @param n A signed 32-bit integer. + * @return An unsigned 32-bit integer, stored in a signed int because + * Java has no explicit unsigned support. + */ + public static int encodeZigZag32(final int n) { + // Note: the right-shift must be arithmetic + return (n << 1) ^ (n >> 31); + } + + /** + * Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers + * into values that can be efficiently encoded with varint. (Otherwise, + * negative values must be sign-extended to 64 bits to be varint encoded, + * thus always taking 10 bytes on the wire.) + * + * @param n A signed 64-bit integer. + * @return An unsigned 64-bit integer, stored in a signed int because + * Java has no explicit unsigned support. + */ + public static long encodeZigZag64(final long n) { + // Note: the right-shift must be arithmetic + return (n << 1) ^ (n >> 63); + } +} diff --git a/java/src/main/java/com/google/protobuf/nano/InternalNano.java b/java/src/main/java/com/google/protobuf/nano/InternalNano.java new file mode 100644 index 0000000..4930951 --- /dev/null +++ b/java/src/main/java/com/google/protobuf/nano/InternalNano.java @@ -0,0 +1,113 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano; + +import java.io.UnsupportedEncodingException; + +/** + * The classes contained within are used internally by the Protocol Buffer + * library and generated message implementations. They are public only because + * those generated messages do not reside in the {@code protobuf} package. + * Others should not use this class directly. + * + * @author kenton@google.com (Kenton Varda) + */ +public class InternalNano { + /** + * Helper called by generated code to construct default values for string + * fields. + * <p> + * The protocol compiler does not actually contain a UTF-8 decoder -- it + * just pushes UTF-8-encoded text around without touching it. The one place + * where this presents a problem is when generating Java string literals. + * Unicode characters in the string literal would normally need to be encoded + * using a Unicode escape sequence, which would require decoding them. + * To get around this, protoc instead embeds the UTF-8 bytes into the + * generated code and leaves it to the runtime library to decode them. + * <p> + * It gets worse, though. If protoc just generated a byte array, like: + * new byte[] {0x12, 0x34, 0x56, 0x78} + * Java actually generates *code* which allocates an array and then fills + * in each value. This is much less efficient than just embedding the bytes + * directly into the bytecode. To get around this, we need another + * work-around. String literals are embedded directly, so protoc actually + * generates a string literal corresponding to the bytes. The easiest way + * to do this is to use the ISO-8859-1 character set, which corresponds to + * the first 256 characters of the Unicode range. Protoc can then use + * good old CEscape to generate the string. + * <p> + * So we have a string literal which represents a set of bytes which + * represents another string. This function -- stringDefaultValue -- + * converts from the generated string to the string we actually want. The + * generated code calls this automatically. + */ + public static final String stringDefaultValue(String bytes) { + try { + return new String(bytes.getBytes("ISO-8859-1"), "UTF-8"); + } catch (UnsupportedEncodingException e) { + // This should never happen since all JVMs are required to implement + // both of the above character sets. + throw new IllegalStateException( + "Java VM does not support a standard character set.", e); + } + } + + /** + * Helper called by generated code to construct default values for bytes + * fields. + * <p> + * This is a lot like {@link #stringDefaultValue}, but for bytes fields. + * In this case we only need the second of the two hacks -- allowing us to + * embed raw bytes as a string literal with ISO-8859-1 encoding. + */ + public static final byte[] bytesDefaultValue(String bytes) { + try { + return bytes.getBytes("ISO-8859-1"); + } catch (UnsupportedEncodingException e) { + // This should never happen since all JVMs are required to implement + // ISO-8859-1. + throw new IllegalStateException( + "Java VM does not support a standard character set.", e); + } + } + + /** + * Helper function to convert a string into UTF-8 while turning the + * UnsupportedEncodingException to a RuntimeException. + */ + public static final byte[] copyFromUtf8(final String text) { + try { + return text.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("UTF-8 not supported?"); + } + } +} diff --git a/java/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java b/java/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java new file mode 100644 index 0000000..ff0af9d --- /dev/null +++ b/java/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java @@ -0,0 +1,93 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2013 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano; + +import java.io.IOException; + +/** + * Thrown when a protocol message being parsed is invalid in some way, + * e.g. it contains a malformed varint or a negative byte length. + * + * @author kenton@google.com Kenton Varda + */ +public class InvalidProtocolBufferNanoException extends IOException { + private static final long serialVersionUID = -1616151763072450476L; + + public InvalidProtocolBufferNanoException(final String description) { + super(description); + } + + static InvalidProtocolBufferNanoException truncatedMessage() { + return new InvalidProtocolBufferNanoException( + "While parsing a protocol message, the input ended unexpectedly " + + "in the middle of a field. This could mean either than the " + + "input has been truncated or that an embedded message " + + "misreported its own length."); + } + + static InvalidProtocolBufferNanoException negativeSize() { + return new InvalidProtocolBufferNanoException( + "CodedInputStream encountered an embedded string or message " + + "which claimed to have negative size."); + } + + static InvalidProtocolBufferNanoException malformedVarint() { + return new InvalidProtocolBufferNanoException( + "CodedInputStream encountered a malformed varint."); + } + + static InvalidProtocolBufferNanoException invalidTag() { + return new InvalidProtocolBufferNanoException( + "Protocol message contained an invalid tag (zero)."); + } + + static InvalidProtocolBufferNanoException invalidEndTag() { + return new InvalidProtocolBufferNanoException( + "Protocol message end-group tag did not match expected tag."); + } + + static InvalidProtocolBufferNanoException invalidWireType() { + return new InvalidProtocolBufferNanoException( + "Protocol message tag had invalid wire type."); + } + + static InvalidProtocolBufferNanoException recursionLimitExceeded() { + return new InvalidProtocolBufferNanoException( + "Protocol message had too many levels of nesting. May be malicious. " + + "Use CodedInputStream.setRecursionLimit() to increase the depth limit."); + } + + static InvalidProtocolBufferNanoException sizeLimitExceeded() { + return new InvalidProtocolBufferNanoException( + "Protocol message was too large. May be malicious. " + + "Use CodedInputStream.setSizeLimit() to increase the size limit."); + } +} diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNano.java b/java/src/main/java/com/google/protobuf/nano/MessageNano.java new file mode 100644 index 0000000..66080cc --- /dev/null +++ b/java/src/main/java/com/google/protobuf/nano/MessageNano.java @@ -0,0 +1,128 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2013 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano; + +import java.io.IOException; + +/** + * Abstract interface implemented by Protocol Message objects. + * + * @author wink@google.com Wink Saville + */ +public abstract class MessageNano { + /** + * Get the number of bytes required to encode this message. + * Returns the cached size or calls getSerializedSize which + * sets the cached size. This is used internally when serializing + * so the size is only computed once. If a member is modified + * then this could be stale call getSerializedSize if in doubt. + */ + abstract public int getCachedSize(); + + /** + * Computes the number of bytes required to encode this message. + * The size is cached and the cached result can be retrieved + * using getCachedSize(). + */ + abstract public int getSerializedSize(); + + /** + * Serializes the message and writes it to {@code output}. This does not + * flush or close the stream. + */ + abstract public void writeTo(CodedOutputByteBufferNano output) throws java.io.IOException; + + /** + * 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; + + /** + * Serialize to a byte array. + * @return byte array with the serialized data. + */ + public static final byte[] toByteArray(MessageNano msg) { + final byte[] result = new byte[msg.getSerializedSize()]; + toByteArray(msg, result, 0, result.length); + return result; + } + + /** + * Serialize to a byte array starting at offset through length. The + * method getSerializedSize must have been called prior to calling + * this method so the proper length is know. If an attempt to + * 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) { + try { + final CodedOutputByteBufferNano output = + CodedOutputByteBufferNano.newInstance(data, offset, length); + msg.writeTo(output); + output.checkNoSpaceLeft(); + } catch (IOException e) { + throw new RuntimeException("Serializing to a byte array threw an IOException " + + "(should never happen)."); + } + } + + /** + * Parse {@code data} as a message of this type and merge it with the + * message being built. + */ + public static final MessageNano mergeFrom(MessageNano msg, final byte[] data) + throws InvalidProtocolBufferNanoException { + return mergeFrom(msg, data, 0, data.length); + } + + /** + * Parse {@code data} as a message of this type and merge it with the + * message being built. + */ + public static final MessageNano mergeFrom(MessageNano msg, final byte[] data, final int off, + final int len) throws InvalidProtocolBufferNanoException { + try { + final CodedInputByteBufferNano input = + CodedInputByteBufferNano.newInstance(data, off, len); + msg.mergeFrom(input); + input.checkLastTagWas(0); + return msg; + } catch (InvalidProtocolBufferNanoException e) { + throw e; + } catch (IOException e) { + throw new RuntimeException("Reading from a byte array threw an IOException (should " + + "never happen)."); + } + } +} diff --git a/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java b/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java new file mode 100644 index 0000000..8fa3636 --- /dev/null +++ b/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java @@ -0,0 +1,138 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2013 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano; + +import java.io.IOException; + +/** + * This class is used internally by the Protocol Buffer library and generated + * message implementations. It is public only because those generated messages + * do not reside in the {@code protobuf} package. Others should not use this + * class directly. + * + * This class contains constants and helper functions useful for dealing with + * the Protocol Buffer wire format. + * + * @author kenton@google.com Kenton Varda + */ +public final class WireFormatNano { + // Do not allow instantiation. + private WireFormatNano() {} + + static final int WIRETYPE_VARINT = 0; + static final int WIRETYPE_FIXED64 = 1; + static final int WIRETYPE_LENGTH_DELIMITED = 2; + static final int WIRETYPE_START_GROUP = 3; + static final int WIRETYPE_END_GROUP = 4; + static final int WIRETYPE_FIXED32 = 5; + + static final int TAG_TYPE_BITS = 3; + static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1; + + /** Given a tag value, determines the wire type (the lower 3 bits). */ + static int getTagWireType(final int tag) { + return tag & TAG_TYPE_MASK; + } + + /** Given a tag value, determines the field number (the upper 29 bits). */ + public static int getTagFieldNumber(final int tag) { + return tag >>> TAG_TYPE_BITS; + } + + /** Makes a tag value given a field number and wire type. */ + static int makeTag(final int fieldNumber, final int wireType) { + 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[] = {}; + public static final double EMPTY_DOUBLE_ARRAY[] = {}; + public static final boolean EMPTY_BOOLEAN_ARRAY[] = {}; + public static final String EMPTY_STRING_ARRAY[] = {}; + public static final byte[] EMPTY_BYTES_ARRAY[] = {}; + public static final byte[] EMPTY_BYTES = {}; + + /** + * Called by subclasses to parse an unknown field. + * @return {@code true} unless the tag is an end-group tag. + */ + public static boolean parseUnknownField( + final CodedInputByteBufferNano input, + final int tag) throws IOException { + return input.skipField(tag); + } + + /** + * 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). + * + * Rewinds to current input position before returning. + * + * @param input stream input, pointing to the byte after the first tag + * @param tag repeated field tag just read + * @return length of array + * @throws IOException + */ + public static final int getRepeatedFieldArrayLength( + final CodedInputByteBufferNano input, + final int tag) throws IOException { + int arrayLength = 1; + int startPos = input.getPosition(); + input.skipField(tag); + while (input.getBytesUntilLimit() > 0) { + int thisTag = input.readTag(); + if (thisTag != tag) { + break; + } + input.skipField(tag); + arrayLength++; + } + input.rewindToPosition(startPos); + return arrayLength; + } +} diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java new file mode 100644 index 0000000..85389d1 --- /dev/null +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -0,0 +1,2104 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2013 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.nano.InternalNano; +import com.google.protobuf.nano.MessageNano; +import com.google.protobuf.nano.NanoOuterClass; +import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano; +import com.google.protobuf.nano.RecursiveMessageNano; +import com.google.protobuf.nano.SimpleMessageNano; +import com.google.protobuf.nano.UnittestImportNano; +import com.google.protobuf.nano.CodedInputByteBufferNano; + +import junit.framework.TestCase; + +/** + * Test nano runtime. + * + * @author ulas@google.com Ulas Kirazci + */ +public class NanoTest extends TestCase { + public void setUp() throws Exception { + } + + public void testSimpleMessageNano() throws Exception { + SimpleMessageNano msg = new SimpleMessageNano(); + assertEquals(123, msg.d); + assertEquals(null, msg.nestedMsg); + assertEquals(SimpleMessageNano.BAZ, msg.defaultNestedEnum); + + msg.d = 456; + assertEquals(456, msg.d); + + SimpleMessageNano.NestedMessage nestedMsg = new SimpleMessageNano.NestedMessage(); + nestedMsg.bb = 2; + assertEquals(2, nestedMsg.bb); + msg.nestedMsg = nestedMsg; + assertEquals(2, msg.nestedMsg.bb); + + msg.defaultNestedEnum = SimpleMessageNano.BAR; + assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum); + + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + SimpleMessageNano newMsg = SimpleMessageNano.parseFrom(result); + assertEquals(456, newMsg.d); + assertEquals(2, msg.nestedMsg.bb); + assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum); + } + + public void testRecursiveMessageNano() throws Exception { + RecursiveMessageNano msg = new RecursiveMessageNano(); + assertTrue(msg.repeatedRecursiveMessageNano.length == 0); + + RecursiveMessageNano msg1 = new RecursiveMessageNano(); + msg1.id = 1; + assertEquals(1, msg1.id); + RecursiveMessageNano msg2 = new RecursiveMessageNano(); + msg2.id = 2; + RecursiveMessageNano msg3 = new RecursiveMessageNano(); + msg3.id = 3; + + RecursiveMessageNano.NestedMessage nestedMsg = new RecursiveMessageNano.NestedMessage(); + nestedMsg.a = msg1; + assertEquals(1, nestedMsg.a.id); + + msg.id = 0; + msg.nestedMessage = nestedMsg; + msg.optionalRecursiveMessageNano = msg2; + msg.repeatedRecursiveMessageNano = new RecursiveMessageNano[] { msg3 }; + + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 16); + assertEquals(result.length, msgSerializedSize); + + RecursiveMessageNano newMsg = RecursiveMessageNano.parseFrom(result); + assertEquals(1, newMsg.repeatedRecursiveMessageNano.length); + + assertEquals(0, newMsg.id); + assertEquals(1, newMsg.nestedMessage.a.id); + assertEquals(2, newMsg.optionalRecursiveMessageNano.id); + assertEquals(3, newMsg.repeatedRecursiveMessageNano[0].id); + } + + public void testNanoRequiredInt32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.id = 123; + assertEquals(123, msg.id); + msg.clear().id = 456; + assertEquals(456, msg.id); + msg.clear(); + + msg.id = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 3); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.id); + } + + public void testNanoOptionalInt32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalInt32 = 123; + assertEquals(123, msg.optionalInt32); + msg.clear() + .optionalInt32 = 456; + assertEquals(456, msg.optionalInt32); + msg.clear(); + + msg.optionalInt32 = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 5); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.optionalInt32); + } + + public void testNanoOptionalInt64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalInt64 = 123; + assertEquals(123, msg.optionalInt64); + msg.clear() + .optionalInt64 = 456; + assertEquals(456, msg.optionalInt64); + msg.clear(); + assertEquals(0, msg.optionalInt64); + + msg.optionalInt64 = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 5); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.optionalInt64); + } + + public void testNanoOptionalUint32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalUint32 = 123; + assertEquals(123, msg.optionalUint32); + msg.clear() + .optionalUint32 = 456; + assertEquals(456, msg.optionalUint32); + msg.clear(); + assertEquals(0, msg.optionalUint32); + + msg.optionalUint32 = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 5); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.optionalUint32); + } + + public void testNanoOptionalUint64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalUint64 = 123; + assertEquals(123, msg.optionalUint64); + msg.clear() + .optionalUint64 = 456; + assertEquals(456, msg.optionalUint64); + msg.clear(); + assertEquals(0, msg.optionalUint64); + + msg.optionalUint64 = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 5); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.optionalUint64); + } + + public void testNanoOptionalSint32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalSint32 = 123; + assertEquals(123, msg.optionalSint32); + msg.clear() + .optionalSint32 = 456; + assertEquals(456, msg.optionalSint32); + msg.clear(); + assertEquals(0, msg.optionalSint32); + + msg.optionalSint32 = -123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(-123, newMsg.optionalSint32); + } + + public void testNanoOptionalSint64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalSint64 = 123; + assertEquals(123, msg.optionalSint64); + msg.clear() + .optionalSint64 = 456; + assertEquals(456, msg.optionalSint64); + msg.clear(); + assertEquals(0, msg.optionalSint64); + + msg.optionalSint64 = -123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(-123, newMsg.optionalSint64); + } + + public void testNanoOptionalFixed32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalFixed32 = 123; + assertEquals(123, msg.optionalFixed32); + msg.clear() + .optionalFixed32 = 456; + assertEquals(456, msg.optionalFixed32); + msg.clear(); + assertEquals(0, msg.optionalFixed32); + + msg.optionalFixed32 = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.optionalFixed32); + } + + public void testNanoOptionalFixed64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalFixed64 = 123; + assertEquals(123, msg.optionalFixed64); + msg.clear() + .optionalFixed64 = 456; + assertEquals(456, msg.optionalFixed64); + msg.clear(); + assertEquals(0, msg.optionalFixed64); + + msg.optionalFixed64 = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 12); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.optionalFixed64); + } + + public void testNanoOptionalSfixed32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalSfixed32 = 123; + assertEquals(123, msg.optionalSfixed32); + msg.clear() + .optionalSfixed32 = 456; + assertEquals(456, msg.optionalSfixed32); + msg.clear(); + assertEquals(0, msg.optionalSfixed32); + + msg.optionalSfixed32 = 123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(123, newMsg.optionalSfixed32); + } + + public void testNanoOptionalSfixed64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalSfixed64 = 123; + assertEquals(123, msg.optionalSfixed64); + msg.clear() + .optionalSfixed64 = 456; + assertEquals(456, msg.optionalSfixed64); + msg.clear(); + assertEquals(0, msg.optionalSfixed64); + + msg.optionalSfixed64 = -123; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 12); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(-123, newMsg.optionalSfixed64); + } + + public void testNanoOptionalFloat() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalFloat = 123f; + assertTrue(123.0f == msg.optionalFloat); + msg.clear() + .optionalFloat = 456.0f; + assertTrue(456.0f == msg.optionalFloat); + msg.clear(); + assertTrue(0.0f == msg.optionalFloat); + + msg.optionalFloat = -123.456f; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(-123.456f == newMsg.optionalFloat); + } + + public void testNanoOptionalDouble() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalDouble = 123; + assertTrue(123.0 == msg.optionalDouble); + msg.clear() + .optionalDouble = 456.0; + assertTrue(456.0 == msg.optionalDouble); + msg.clear(); + assertTrue(0.0 == msg.optionalDouble); + + msg.optionalDouble = -123.456; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 12); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(-123.456 == newMsg.optionalDouble); + } + + public void testNanoOptionalBool() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalBool = true; + assertTrue(msg.optionalBool); + msg.clear() + .optionalBool = true; + assertTrue(msg.optionalBool); + msg.clear(); + assertFalse(msg.optionalBool); + + msg.optionalBool = true; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 5); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalBool); + } + + public void testNanoOptionalString() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalString = "hello"; + assertEquals("hello", msg.optionalString); + msg.clear(); + assertTrue(msg.optionalString.isEmpty()); + msg.clear() + .optionalString = "hello2"; + assertEquals("hello2", msg.optionalString); + msg.clear(); + assertTrue(msg.optionalString.isEmpty()); + + msg.optionalString = "bye"; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalString != null); + assertEquals("bye", newMsg.optionalString); + } + + public void testNanoOptionalBytes() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertFalse(msg.optionalBytes.length > 0); + msg.optionalBytes = InternalNano.copyFromUtf8("hello"); + assertTrue(msg.optionalBytes.length > 0); + assertEquals("hello", new String(msg.optionalBytes, "UTF-8")); + msg.clear(); + assertFalse(msg.optionalBytes.length > 0); + msg.clear() + .optionalBytes = InternalNano.copyFromUtf8("hello"); + assertTrue(msg.optionalBytes.length > 0); + msg.clear(); + assertFalse(msg.optionalBytes.length > 0); + + msg.optionalBytes = InternalNano.copyFromUtf8("bye"); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalBytes.length > 0); + assertEquals("bye", new String(newMsg.optionalBytes, "UTF-8")); + } + + public void testNanoOptionalGroup() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + TestAllTypesNano.OptionalGroup grp = new TestAllTypesNano.OptionalGroup(); + grp.a = 1; + assertFalse(msg.optionalGroup != null); + msg.optionalGroup = grp; + assertTrue(msg.optionalGroup != null); + assertEquals(1, msg.optionalGroup.a); + msg.clear(); + assertFalse(msg.optionalGroup != null); + msg.clear() + .optionalGroup = new TestAllTypesNano.OptionalGroup(); + msg.optionalGroup.a = 2; + assertTrue(msg.optionalGroup != null); + msg.clear(); + assertFalse(msg.optionalGroup != null); + + msg.optionalGroup = grp; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 10); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalGroup != null); + assertEquals(1, newMsg.optionalGroup.a); + } + + public void testNanoOptionalNestedMessage() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + TestAllTypesNano.NestedMessage nestedMsg = new TestAllTypesNano.NestedMessage(); + nestedMsg.bb = 1; + assertFalse(msg.optionalNestedMessage != null); + msg.optionalNestedMessage = nestedMsg; + assertTrue(msg.optionalNestedMessage != null); + assertEquals(1, msg.optionalNestedMessage.bb); + msg.clear(); + assertFalse(msg.optionalNestedMessage != null); + msg.clear() + .optionalNestedMessage = new TestAllTypesNano.NestedMessage(); + msg.optionalNestedMessage.bb = 2; + assertTrue(msg.optionalNestedMessage != null); + msg.clear(); + assertFalse(msg.optionalNestedMessage != null); + + msg.optionalNestedMessage = nestedMsg; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalNestedMessage != null); + assertEquals(1, newMsg.optionalNestedMessage.bb); + } + + public void testNanoOptionalForeignMessage() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + NanoOuterClass.ForeignMessageNano nestedMsg = new NanoOuterClass.ForeignMessageNano(); + nestedMsg.c = 1; + assertFalse(msg.optionalForeignMessage != null); + msg.optionalForeignMessage = nestedMsg; + assertTrue(msg.optionalForeignMessage != null); + assertEquals(1, msg.optionalForeignMessage.c); + msg.clear(); + assertFalse(msg.optionalForeignMessage != null); + msg.clear() + .optionalForeignMessage = new NanoOuterClass.ForeignMessageNano(); + msg.optionalForeignMessage.c = 2; + assertTrue(msg.optionalForeignMessage != null); + msg.clear(); + assertFalse(msg.optionalForeignMessage != null); + + msg.optionalForeignMessage = nestedMsg; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalForeignMessage != null); + assertEquals(1, newMsg.optionalForeignMessage.c); + } + + public void testNanoOptionalImportMessage() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + UnittestImportNano.ImportMessageNano nestedMsg = new UnittestImportNano.ImportMessageNano(); + nestedMsg.d = 1; + assertFalse(msg.optionalImportMessage != null); + msg.optionalImportMessage = nestedMsg; + assertTrue(msg.optionalImportMessage != null); + assertEquals(1, msg.optionalImportMessage.d); + msg.clear(); + assertFalse(msg.optionalImportMessage != null); + msg.clear() + .optionalImportMessage = new UnittestImportNano.ImportMessageNano(); + msg.optionalImportMessage.d = 2; + assertTrue(msg.optionalImportMessage != null); + msg.clear(); + assertFalse(msg.optionalImportMessage != null); + + msg.optionalImportMessage = nestedMsg; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalImportMessage != null); + assertEquals(1, newMsg.optionalImportMessage.d); + } + + public void testNanoOptionalNestedEnum() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalNestedEnum = TestAllTypesNano.BAR; + assertEquals(TestAllTypesNano.BAR, msg.optionalNestedEnum); + msg.clear() + .optionalNestedEnum = TestAllTypesNano.BAZ; + assertEquals(TestAllTypesNano.BAZ, msg.optionalNestedEnum); + msg.clear(); + assertEquals(TestAllTypesNano.FOO, msg.optionalNestedEnum); + + msg.optionalNestedEnum = TestAllTypesNano.BAR; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(TestAllTypesNano.BAR, newMsg.optionalNestedEnum); + } + + public void testNanoOptionalForeignEnum() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAR; + assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.optionalForeignEnum); + msg.clear() + .optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAZ; + assertEquals(NanoOuterClass.FOREIGN_NANO_BAZ, msg.optionalForeignEnum); + msg.clear(); + assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.optionalForeignEnum); + + msg.optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAR; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, newMsg.optionalForeignEnum); + } + + public void testNanoOptionalImportEnum() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAR; + assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.optionalImportEnum); + msg.clear() + .optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAZ; + assertEquals(UnittestImportNano.IMPORT_NANO_BAZ, msg.optionalImportEnum); + msg.clear(); + assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.optionalImportEnum); + + msg.optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAR; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(UnittestImportNano.IMPORT_NANO_BAR, newMsg.optionalImportEnum); + } + + public void testNanoOptionalStringPiece() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalStringPiece = "hello"; + assertEquals("hello", msg.optionalStringPiece); + msg.clear(); + assertTrue(msg.optionalStringPiece.isEmpty()); + msg.clear() + .optionalStringPiece = "hello2"; + assertEquals("hello2", msg.optionalStringPiece); + msg.clear(); + assertTrue(msg.optionalStringPiece.isEmpty()); + + msg.optionalStringPiece = "bye"; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalStringPiece != null); + assertEquals("bye", newMsg.optionalStringPiece); + } + + public void testNanoOptionalCord() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.optionalCord = "hello"; + assertEquals("hello", msg.optionalCord); + msg.clear(); + assertTrue(msg.optionalCord.isEmpty()); + msg.clear() + .optionalCord = "hello2"; + assertEquals("hello2", msg.optionalCord); + msg.clear(); + assertTrue(msg.optionalCord.isEmpty()); + + msg.optionalCord = "bye"; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertTrue(newMsg.optionalCord != null); + assertEquals("bye", newMsg.optionalCord); + } + + public void testNanoRepeatedInt32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedInt32.length); + msg.repeatedInt32 = new int[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedInt32[1]); + assertEquals(456, msg.repeatedInt32[2]); + msg.clear(); + assertEquals(0, msg.repeatedInt32.length); + msg.clear() + .repeatedInt32 = new int[] { 456 }; + assertEquals(1, msg.repeatedInt32.length); + assertEquals(456, msg.repeatedInt32[0]); + msg.clear(); + assertEquals(0, msg.repeatedInt32.length); + + // Test 1 entry + msg.clear() + .repeatedInt32 = new int[] { 123 }; + assertEquals(1, msg.repeatedInt32.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedInt32.length); + assertEquals(123, newMsg.repeatedInt32[0]); + + // Test 2 entries + msg.clear() + .repeatedInt32 = new int[] { 123, 456 }; + assertEquals(2, msg.repeatedInt32.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 10); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedInt32.length); + assertEquals(123, newMsg.repeatedInt32[0]); + assertEquals(456, newMsg.repeatedInt32[1]); + } + + public void testNanoRepeatedInt64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedInt64.length); + msg.repeatedInt64 = new long[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedInt64[1]); + assertEquals(456, msg.repeatedInt64[2]); + msg.clear(); + assertEquals(0, msg.repeatedInt64.length); + msg.clear() + .repeatedInt64 = new long[] { 456 }; + assertEquals(1, msg.repeatedInt64.length); + assertEquals(456, msg.repeatedInt64[0]); + msg.clear(); + assertEquals(0, msg.repeatedInt64.length); + + // Test 1 entry + msg.clear() + .repeatedInt64 = new long[] { 123 }; + assertEquals(1, msg.repeatedInt64.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedInt64.length); + assertEquals(123, newMsg.repeatedInt64[0]); + + // Test 2 entries + msg.clear() + .repeatedInt64 = new long[] { 123, 456 }; + assertEquals(2, msg.repeatedInt64.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 10); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedInt64.length); + assertEquals(123, newMsg.repeatedInt64[0]); + assertEquals(456, newMsg.repeatedInt64[1]); + } + + public void testNanoRepeatedUint32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedUint32.length); + msg.repeatedUint32 = new int[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedUint32[1]); + assertEquals(456, msg.repeatedUint32[2]); + msg.clear(); + assertEquals(0, msg.repeatedUint32.length); + msg.clear() + .repeatedUint32 = new int[] { 456 }; + assertEquals(1, msg.repeatedUint32.length); + assertEquals(456, msg.repeatedUint32[0]); + msg.clear(); + assertEquals(0, msg.repeatedUint32.length); + + // Test 1 entry + msg.clear() + .repeatedUint32 = new int[] { 123 }; + assertEquals(1, msg.repeatedUint32.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedUint32.length); + assertEquals(123, newMsg.repeatedUint32[0]); + + // Test 2 entries + msg.clear() + .repeatedUint32 = new int[] { 123, 456 }; + assertEquals(2, msg.repeatedUint32.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 10); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedUint32.length); + assertEquals(123, newMsg.repeatedUint32[0]); + assertEquals(456, newMsg.repeatedUint32[1]); + } + + public void testNanoRepeatedUint64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedUint64.length); + msg.repeatedUint64 = new long[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedUint64[1]); + assertEquals(456, msg.repeatedUint64[2]); + msg.clear(); + assertEquals(0, msg.repeatedUint64.length); + msg.clear() + .repeatedUint64 = new long[] { 456 }; + assertEquals(1, msg.repeatedUint64.length); + assertEquals(456, msg.repeatedUint64[0]); + msg.clear(); + assertEquals(0, msg.repeatedUint64.length); + + // Test 1 entry + msg.clear() + .repeatedUint64 = new long[] { 123 }; + assertEquals(1, msg.repeatedUint64.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedUint64.length); + assertEquals(123, newMsg.repeatedUint64[0]); + + // Test 2 entries + msg.clear() + .repeatedUint64 = new long[] { 123, 456 }; + assertEquals(2, msg.repeatedUint64.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 10); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedUint64.length); + assertEquals(123, newMsg.repeatedUint64[0]); + assertEquals(456, newMsg.repeatedUint64[1]); + } + + public void testNanoRepeatedSint32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedSint32.length); + msg.repeatedSint32 = new int[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedSint32[1]); + assertEquals(456, msg.repeatedSint32[2]); + msg.clear(); + assertEquals(0, msg.repeatedSint32.length); + msg.clear() + .repeatedSint32 = new int[] { 456 }; + assertEquals(1, msg.repeatedSint32.length); + assertEquals(456, msg.repeatedSint32[0]); + msg.clear(); + assertEquals(0, msg.repeatedSint32.length); + + // Test 1 entry + msg.clear() + .repeatedSint32 = new int[] { 123 }; + assertEquals(1, msg.repeatedSint32.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 7); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedSint32.length); + assertEquals(123, newMsg.repeatedSint32[0]); + + // Test 2 entries + msg.clear() + .repeatedSint32 = new int[] { 123, 456 }; + assertEquals(2, msg.repeatedSint32.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 11); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedSint32.length); + assertEquals(123, newMsg.repeatedSint32[0]); + assertEquals(456, newMsg.repeatedSint32[1]); + } + + public void testNanoRepeatedSint64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedSint64.length); + msg.repeatedSint64 = new long[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedSint64[1]); + assertEquals(456, msg.repeatedSint64[2]); + msg.clear(); + assertEquals(0, msg.repeatedSint64.length); + msg.clear() + .repeatedSint64 = new long[] { 456 }; + assertEquals(1, msg.repeatedSint64.length); + assertEquals(456, msg.repeatedSint64[0]); + msg.clear(); + assertEquals(0, msg.repeatedSint64.length); + + // Test 1 entry + msg.clear() + .repeatedSint64 = new long[] { 123 }; + assertEquals(1, msg.repeatedSint64.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 7); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedSint64.length); + assertEquals(123, newMsg.repeatedSint64[0]); + + // Test 2 entries + msg.clear() + .repeatedSint64 = new long[] { 123, 456 }; + assertEquals(2, msg.repeatedSint64.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 11); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedSint64.length); + assertEquals(123, newMsg.repeatedSint64[0]); + assertEquals(456, newMsg.repeatedSint64[1]); + } + + public void testNanoRepeatedFixed32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedFixed32.length); + msg.repeatedFixed32 = new int[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedFixed32[1]); + assertEquals(456, msg.repeatedFixed32[2]); + msg.clear(); + assertEquals(0, msg.repeatedFixed32.length); + msg.clear() + .repeatedFixed32 = new int[] { 456 }; + assertEquals(1, msg.repeatedFixed32.length); + assertEquals(456, msg.repeatedFixed32[0]); + msg.clear(); + assertEquals(0, msg.repeatedFixed32.length); + + // Test 1 entry + msg.clear() + .repeatedFixed32 = new int[] { 123 }; + assertEquals(1, msg.repeatedFixed32.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedFixed32.length); + assertEquals(123, newMsg.repeatedFixed32[0]); + + // Test 2 entries + msg.clear() + .repeatedFixed32 = new int[] { 123, 456 }; + assertEquals(2, msg.repeatedFixed32.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 15); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedFixed32.length); + assertEquals(123, newMsg.repeatedFixed32[0]); + assertEquals(456, newMsg.repeatedFixed32[1]); + } + + public void testNanoRepeatedFixed64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedFixed64.length); + msg.repeatedFixed64 = new long[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedFixed64[1]); + assertEquals(456, msg.repeatedFixed64[2]); + msg.clear(); + assertEquals(0, msg.repeatedFixed64.length); + msg.clear() + .repeatedFixed64 = new long[] { 456 }; + assertEquals(1, msg.repeatedFixed64.length); + assertEquals(456, msg.repeatedFixed64[0]); + msg.clear(); + assertEquals(0, msg.repeatedFixed64.length); + + // Test 1 entry + msg.clear() + .repeatedFixed64 = new long[] { 123 }; + assertEquals(1, msg.repeatedFixed64.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 13); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedFixed64.length); + assertEquals(123, newMsg.repeatedFixed64[0]); + + // Test 2 entries + msg.clear() + .repeatedFixed64 = new long[] { 123, 456 }; + assertEquals(2, msg.repeatedFixed64.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 23); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedFixed64.length); + assertEquals(123, newMsg.repeatedFixed64[0]); + assertEquals(456, newMsg.repeatedFixed64[1]); + } + + public void testNanoRepeatedSfixed32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedSfixed32.length); + msg.repeatedSfixed32 = new int[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedSfixed32[1]); + assertEquals(456, msg.repeatedSfixed32[2]); + msg.clear(); + assertEquals(0, msg.repeatedSfixed32.length); + msg.clear() + .repeatedSfixed32 = new int[] { 456 }; + assertEquals(1, msg.repeatedSfixed32.length); + assertEquals(456, msg.repeatedSfixed32[0]); + msg.clear(); + assertEquals(0, msg.repeatedSfixed32.length); + + // Test 1 entry + msg.clear() + .repeatedSfixed32 = new int[] { 123 }; + assertEquals(1, msg.repeatedSfixed32.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedSfixed32.length); + assertEquals(123, newMsg.repeatedSfixed32[0]); + + // Test 2 entries + msg.clear() + .repeatedSfixed32 = new int[] { 123, 456 }; + assertEquals(2, msg.repeatedSfixed32.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 15); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedSfixed32.length); + assertEquals(123, newMsg.repeatedSfixed32[0]); + assertEquals(456, newMsg.repeatedSfixed32[1]); + } + + public void testNanoRepeatedSfixed64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedSfixed64.length); + msg.repeatedSfixed64 = new long[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedSfixed64[1]); + assertEquals(456, msg.repeatedSfixed64[2]); + msg.clear(); + assertEquals(0, msg.repeatedSfixed64.length); + msg.clear() + .repeatedSfixed64 = new long[] { 456 }; + assertEquals(1, msg.repeatedSfixed64.length); + assertEquals(456, msg.repeatedSfixed64[0]); + msg.clear(); + assertEquals(0, msg.repeatedSfixed64.length); + + // Test 1 entry + msg.clear() + .repeatedSfixed64 = new long[] { 123 }; + assertEquals(1, msg.repeatedSfixed64.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 13); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedSfixed64.length); + assertEquals(123, newMsg.repeatedSfixed64[0]); + + // Test 2 entries + msg.clear() + .repeatedSfixed64 = new long[] { 123, 456 }; + assertEquals(2, msg.repeatedSfixed64.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 23); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedSfixed64.length); + assertEquals(123, newMsg.repeatedSfixed64[0]); + assertEquals(456, newMsg.repeatedSfixed64[1]); + } + + public void testNanoRepeatedFloat() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedFloat.length); + msg.repeatedFloat = new float[] { 123f, 789f, 456f }; + assertEquals(789f, msg.repeatedFloat[1]); + assertEquals(456f, msg.repeatedFloat[2]); + msg.clear(); + assertEquals(0, msg.repeatedFloat.length); + msg.clear() + .repeatedFloat = new float[] { 456f }; + assertEquals(1, msg.repeatedFloat.length); + assertEquals(456f, msg.repeatedFloat[0]); + msg.clear(); + assertEquals(0, msg.repeatedFloat.length); + + // Test 1 entry + msg.clear() + .repeatedFloat = new float[] { 123f }; + assertEquals(1, msg.repeatedFloat.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedFloat.length); + assertEquals(123f, newMsg.repeatedFloat[0]); + + // Test 2 entries + msg.clear() + .repeatedFloat = new float[] { 123f, 456f }; + assertEquals(2, msg.repeatedFloat.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 15); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedFloat.length); + assertEquals(123f, newMsg.repeatedFloat[0]); + assertEquals(456f, newMsg.repeatedFloat[1]); + } + + public void testNanoRepeatedDouble() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedDouble.length); + msg.repeatedDouble = new double[] { 123.0, 789.0, 456.0 }; + assertEquals(789.0, msg.repeatedDouble[1]); + assertEquals(456.0, msg.repeatedDouble[2]); + msg.clear(); + assertEquals(0, msg.repeatedDouble.length); + msg.clear() + .repeatedDouble = new double[] { 456.0 }; + assertEquals(1, msg.repeatedDouble.length); + assertEquals(456.0, msg.repeatedDouble[0]); + msg.clear(); + assertEquals(0, msg.repeatedDouble.length); + + // Test 1 entry + msg.clear() + .repeatedDouble = new double[] { 123.0 }; + assertEquals(1, msg.repeatedDouble.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 13); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedDouble.length); + assertEquals(123.0, newMsg.repeatedDouble[0]); + + // Test 2 entries + msg.clear() + .repeatedDouble = new double[] { 123.0, 456.0 }; + assertEquals(2, msg.repeatedDouble.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 23); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedDouble.length); + assertEquals(123.0, newMsg.repeatedDouble[0]); + assertEquals(456.0, newMsg.repeatedDouble[1]); + } + + public void testNanoRepeatedBool() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedBool.length); + msg.repeatedBool = new boolean[] { false, true, false }; + assertTrue(msg.repeatedBool[1]); + assertFalse(msg.repeatedBool[2]); + msg.clear(); + assertEquals(0, msg.repeatedBool.length); + msg.clear() + .repeatedBool = new boolean[] { true }; + assertEquals(1, msg.repeatedBool.length); + assertTrue(msg.repeatedBool[0]); + msg.clear(); + assertEquals(0, msg.repeatedBool.length); + + // Test 1 entry + msg.clear() + .repeatedBool = new boolean[] { false }; + assertEquals(1, msg.repeatedBool.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedBool.length); + assertFalse(newMsg.repeatedBool[0]); + + // Test 2 entries + msg.clear() + .repeatedBool = new boolean[] { true, false }; + assertEquals(2, msg.repeatedBool.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedBool.length); + assertTrue(newMsg.repeatedBool[0]); + assertFalse(newMsg.repeatedBool[1]); + } + + public void testNanoRepeatedString() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedString.length); + msg.repeatedString = new String[] { "hello", "bye", "boo" }; + assertEquals("bye", msg.repeatedString[1]); + assertEquals("boo", msg.repeatedString[2]); + msg.clear(); + assertEquals(0, msg.repeatedString.length); + msg.clear() + .repeatedString = new String[] { "boo" }; + assertEquals(1, msg.repeatedString.length); + assertEquals("boo", msg.repeatedString[0]); + msg.clear(); + assertEquals(0, msg.repeatedString.length); + + // Test 1 entry + msg.clear() + .repeatedString = new String[] { "" }; + assertEquals(1, msg.repeatedString.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedString.length); + assertTrue(newMsg.repeatedString[0].isEmpty()); + + // Test 2 entries + msg.clear() + .repeatedString = new String[] { "hello", "world" }; + assertEquals(2, msg.repeatedString.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 19); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedString.length); + assertEquals("hello", newMsg.repeatedString[0]); + assertEquals("world", newMsg.repeatedString[1]); + } + + public void testNanoRepeatedBytes() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedBytes.length); + msg.repeatedBytes = new byte[][] { + InternalNano.copyFromUtf8("hello"), + InternalNano.copyFromUtf8("bye"), + InternalNano.copyFromUtf8("boo") + }; + assertEquals("bye", new String(msg.repeatedBytes[1], "UTF-8")); + assertEquals("boo", new String(msg.repeatedBytes[2], "UTF-8")); + msg.clear(); + assertEquals(0, msg.repeatedBytes.length); + msg.clear() + .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("boo") }; + assertEquals(1, msg.repeatedBytes.length); + assertEquals("boo", new String(msg.repeatedBytes[0], "UTF-8")); + msg.clear(); + assertEquals(0, msg.repeatedBytes.length); + + // Test 1 entry + msg.clear() + .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("") }; + assertEquals(1, msg.repeatedBytes.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedBytes.length); + assertTrue(newMsg.repeatedBytes[0].length == 0); + + // Test 2 entries + msg.clear() + .repeatedBytes = new byte[][] { + InternalNano.copyFromUtf8("hello"), + InternalNano.copyFromUtf8("world") + }; + assertEquals(2, msg.repeatedBytes.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 19); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedBytes.length); + assertEquals("hello", new String(newMsg.repeatedBytes[0], "UTF-8")); + assertEquals("world", new String(newMsg.repeatedBytes[1], "UTF-8")); + } + + public void testNanoRepeatedGroup() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + TestAllTypesNano.RepeatedGroup group0 = + new TestAllTypesNano.RepeatedGroup(); + group0.a = 0; + TestAllTypesNano.RepeatedGroup group1 = + new TestAllTypesNano.RepeatedGroup(); + group1.a = 1; + TestAllTypesNano.RepeatedGroup group2 = + new TestAllTypesNano.RepeatedGroup(); + group2.a = 2; + + msg.repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0, group1, group2 }; + assertEquals(3, msg.repeatedGroup.length); + assertEquals(0, msg.repeatedGroup[0].a); + assertEquals(1, msg.repeatedGroup[1].a); + assertEquals(2, msg.repeatedGroup[2].a); + msg.clear(); + assertEquals(0, msg.repeatedGroup.length); + msg.clear() + .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group1 }; + assertEquals(1, msg.repeatedGroup.length); + assertEquals(1, msg.repeatedGroup[0].a); + msg.clear(); + assertEquals(0, msg.repeatedGroup.length); + + // Test 1 entry + msg.clear() + .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0 }; + assertEquals(1, msg.repeatedGroup.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 7); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedGroup.length); + assertEquals(0, newMsg.repeatedGroup[0].a); + + // Test 2 entries + msg.clear() + .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0, group1 }; + assertEquals(2, msg.repeatedGroup.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 14); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedGroup.length); + assertEquals(0, newMsg.repeatedGroup[0].a); + assertEquals(1, newMsg.repeatedGroup[1].a); + } + + public void testNanoRepeatedNestedMessage() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + TestAllTypesNano.NestedMessage nestedMsg0 = + new TestAllTypesNano.NestedMessage(); + nestedMsg0.bb = 0; + TestAllTypesNano.NestedMessage nestedMsg1 = + new TestAllTypesNano.NestedMessage(); + nestedMsg1.bb = 1; + TestAllTypesNano.NestedMessage nestedMsg2 = + new TestAllTypesNano.NestedMessage(); + nestedMsg2.bb = 2; + + msg.repeatedNestedMessage = + new TestAllTypesNano.NestedMessage[] { nestedMsg0, nestedMsg1, nestedMsg2 }; + assertEquals(3, msg.repeatedNestedMessage.length); + assertEquals(0, msg.repeatedNestedMessage[0].bb); + assertEquals(1, msg.repeatedNestedMessage[1].bb); + assertEquals(2, msg.repeatedNestedMessage[2].bb); + msg.clear(); + assertEquals(0, msg.repeatedNestedMessage.length); + msg.clear() + .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg1 }; + assertEquals(1, msg.repeatedNestedMessage.length); + assertEquals(1, msg.repeatedNestedMessage[0].bb); + msg.clear(); + assertEquals(0, msg.repeatedNestedMessage.length); + + // Test 1 entry + msg.clear() + .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0 }; + assertEquals(1, msg.repeatedNestedMessage.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedNestedMessage.length); + assertEquals(0, newMsg.repeatedNestedMessage[0].bb); + + // Test 2 entries + msg.clear() + .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0, nestedMsg1 }; + assertEquals(2, msg.repeatedNestedMessage.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 11); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedNestedMessage.length); + assertEquals(0, newMsg.repeatedNestedMessage[0].bb); + assertEquals(1, newMsg.repeatedNestedMessage[1].bb); + } + + public void testNanoRepeatedForeignMessage() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + NanoOuterClass.ForeignMessageNano foreignMsg0 = + new NanoOuterClass.ForeignMessageNano(); + foreignMsg0.c = 0; + NanoOuterClass.ForeignMessageNano foreignMsg1 = + new NanoOuterClass.ForeignMessageNano(); + foreignMsg1.c = 1; + NanoOuterClass.ForeignMessageNano foreignMsg2 = + new NanoOuterClass.ForeignMessageNano(); + foreignMsg2.c = 2; + + msg.repeatedForeignMessage = + new NanoOuterClass.ForeignMessageNano[] { foreignMsg0, foreignMsg1, foreignMsg2 }; + assertEquals(3, msg.repeatedForeignMessage.length); + assertEquals(0, msg.repeatedForeignMessage[0].c); + assertEquals(1, msg.repeatedForeignMessage[1].c); + assertEquals(2, msg.repeatedForeignMessage[2].c); + msg.clear(); + assertEquals(0, msg.repeatedForeignMessage.length); + msg.clear() + .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg1 }; + assertEquals(1, msg.repeatedForeignMessage.length); + assertEquals(1, msg.repeatedForeignMessage[0].c); + msg.clear(); + assertEquals(0, msg.repeatedForeignMessage.length); + + // Test 1 entry + msg.clear() + .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg0 }; + assertEquals(1, msg.repeatedForeignMessage.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedForeignMessage.length); + assertEquals(0, newMsg.repeatedForeignMessage[0].c); + + // Test 2 entries + msg.clear() + .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg0, foreignMsg1 }; + assertEquals(2, msg.repeatedForeignMessage.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 11); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedForeignMessage.length); + assertEquals(0, newMsg.repeatedForeignMessage[0].c); + assertEquals(1, newMsg.repeatedForeignMessage[1].c); + } + + public void testNanoRepeatedImportMessage() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + UnittestImportNano.ImportMessageNano foreignMsg0 = + new UnittestImportNano.ImportMessageNano(); + foreignMsg0.d = 0; + UnittestImportNano.ImportMessageNano foreignMsg1 = + new UnittestImportNano.ImportMessageNano(); + foreignMsg1.d = 1; + UnittestImportNano.ImportMessageNano foreignMsg2 = + new UnittestImportNano.ImportMessageNano(); + foreignMsg2.d = 2; + + msg.repeatedImportMessage = + new UnittestImportNano.ImportMessageNano[] { foreignMsg0, foreignMsg1, foreignMsg2 }; + assertEquals(3, msg.repeatedImportMessage.length); + assertEquals(0, msg.repeatedImportMessage[0].d); + assertEquals(1, msg.repeatedImportMessage[1].d); + assertEquals(2, msg.repeatedImportMessage[2].d); + msg.clear(); + assertEquals(0, msg.repeatedImportMessage.length); + msg.clear() + .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg1 }; + assertEquals(1, msg.repeatedImportMessage.length); + assertEquals(1, msg.repeatedImportMessage[0].d); + msg.clear(); + assertEquals(0, msg.repeatedImportMessage.length); + + // Test 1 entry + msg.clear() + .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg0 }; + assertEquals(1, msg.repeatedImportMessage.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedImportMessage.length); + assertEquals(0, newMsg.repeatedImportMessage[0].d); + + // Test 2 entries + msg.clear() + .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg0, foreignMsg1 }; + assertEquals(2, msg.repeatedImportMessage.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 11); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedImportMessage.length); + assertEquals(0, newMsg.repeatedImportMessage[0].d); + assertEquals(1, newMsg.repeatedImportMessage[1].d); + } + + public void testNanoRepeatedNestedEnum() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.repeatedNestedEnum = new int[] { + TestAllTypesNano.FOO, + TestAllTypesNano.BAR, + TestAllTypesNano.BAZ + }; + assertEquals(3, msg.repeatedNestedEnum.length); + assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]); + assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[1]); + assertEquals(TestAllTypesNano.BAZ, msg.repeatedNestedEnum[2]); + msg.clear(); + assertEquals(0, msg.repeatedNestedEnum.length); + msg.clear() + .repeatedNestedEnum = new int[] { TestAllTypesNano.BAR }; + assertEquals(1, msg.repeatedNestedEnum.length); + assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[0]); + msg.clear(); + assertEquals(0, msg.repeatedNestedEnum.length); + + // Test 1 entry + msg.clear() + .repeatedNestedEnum = new int[] { TestAllTypesNano.FOO }; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedNestedEnum.length); + assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]); + + // Test 2 entries + msg.clear() + .repeatedNestedEnum = new int[] { TestAllTypesNano.FOO, TestAllTypesNano.BAR }; + assertEquals(2, msg.repeatedNestedEnum.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedNestedEnum.length); + assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]); + assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[1]); + } + + public void testNanoRepeatedForeignEnum() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.repeatedForeignEnum = new int[] { + NanoOuterClass.FOREIGN_NANO_FOO, + NanoOuterClass.FOREIGN_NANO_BAR, + NanoOuterClass.FOREIGN_NANO_BAZ + }; + assertEquals(3, msg.repeatedForeignEnum.length); + assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]); + assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[1]); + assertEquals(NanoOuterClass.FOREIGN_NANO_BAZ, msg.repeatedForeignEnum[2]); + msg.clear(); + assertEquals(0, msg.repeatedForeignEnum.length); + msg.clear() + .repeatedForeignEnum = new int[] { NanoOuterClass.FOREIGN_NANO_BAR }; + assertEquals(1, msg.repeatedForeignEnum.length); + assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[0]); + msg.clear(); + assertEquals(0, msg.repeatedForeignEnum.length); + + // Test 1 entry + msg.clear() + .repeatedForeignEnum = new int[] { NanoOuterClass.FOREIGN_NANO_FOO }; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedForeignEnum.length); + assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]); + + // Test 2 entries + msg.clear() + .repeatedForeignEnum = new int[] { + NanoOuterClass.FOREIGN_NANO_FOO, + NanoOuterClass.FOREIGN_NANO_BAR + }; + assertEquals(2, msg.repeatedForeignEnum.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedForeignEnum.length); + assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]); + assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[1]); + } + + public void testNanoRepeatedImportEnum() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.repeatedImportEnum = new int[] { + UnittestImportNano.IMPORT_NANO_FOO, + UnittestImportNano.IMPORT_NANO_BAR, + UnittestImportNano.IMPORT_NANO_BAZ + }; + assertEquals(3, msg.repeatedImportEnum.length); + assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]); + assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[1]); + assertEquals(UnittestImportNano.IMPORT_NANO_BAZ, msg.repeatedImportEnum[2]); + msg.clear(); + assertEquals(0, msg.repeatedImportEnum.length); + msg.clear() + .repeatedImportEnum = new int[] { UnittestImportNano.IMPORT_NANO_BAR }; + assertEquals(1, msg.repeatedImportEnum.length); + assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[0]); + msg.clear(); + assertEquals(0, msg.repeatedImportEnum.length); + + // Test 1 entry + msg.clear() + .repeatedImportEnum = new int[] { UnittestImportNano.IMPORT_NANO_FOO }; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedImportEnum.length); + assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]); + + // Test 2 entries + msg.clear() + .repeatedImportEnum = new int[] { + UnittestImportNano.IMPORT_NANO_FOO, + UnittestImportNano.IMPORT_NANO_BAR + }; + assertEquals(2, msg.repeatedImportEnum.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedImportEnum.length); + assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]); + assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[1]); + } + + public void testNanoRepeatedStringPiece() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedStringPiece.length); + msg.repeatedStringPiece = new String[] { "hello", "bye", "boo" }; + assertEquals("bye", msg.repeatedStringPiece[1]); + assertEquals("boo", msg.repeatedStringPiece[2]); + msg.clear(); + assertEquals(0, msg.repeatedStringPiece.length); + msg.clear() + .repeatedStringPiece = new String[] { "boo" }; + assertEquals(1, msg.repeatedStringPiece.length); + assertEquals("boo", msg.repeatedStringPiece[0]); + msg.clear(); + assertEquals(0, msg.repeatedStringPiece.length); + + // Test 1 entry + msg.clear() + .repeatedStringPiece = new String[] { "" }; + assertEquals(1, msg.repeatedStringPiece.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedStringPiece.length); + assertTrue(newMsg.repeatedStringPiece[0].isEmpty()); + + // Test 2 entries + msg.clear() + .repeatedStringPiece = new String[] { "hello", "world" }; + assertEquals(2, msg.repeatedStringPiece.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 19); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedStringPiece.length); + assertEquals("hello", newMsg.repeatedStringPiece[0]); + assertEquals("world", newMsg.repeatedStringPiece[1]); + } + + public void testNanoRepeatedCord() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedCord.length); + msg.repeatedCord = new String[] { "hello", "bye", "boo" }; + assertEquals("bye", msg.repeatedCord[1]); + assertEquals("boo", msg.repeatedCord[2]); + msg.clear(); + assertEquals(0, msg.repeatedCord.length); + msg.clear() + .repeatedCord = new String[] { "boo" }; + assertEquals(1, msg.repeatedCord.length); + assertEquals("boo", msg.repeatedCord[0]); + msg.clear(); + assertEquals(0, msg.repeatedCord.length); + + // Test 1 entry + msg.clear() + .repeatedCord = new String[] { "" }; + assertEquals(1, msg.repeatedCord.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 6); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedCord.length); + assertTrue(newMsg.repeatedCord[0].isEmpty()); + + // Test 2 entries + msg.clear() + .repeatedCord = new String[] { "hello", "world" }; + assertEquals(2, msg.repeatedCord.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 19); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedCord.length); + assertEquals("hello", newMsg.repeatedCord[0]); + assertEquals("world", newMsg.repeatedCord[1]); + } + + public void testNanoRepeatedPackedInt32() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedPackedInt32.length); + msg.repeatedPackedInt32 = new int[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedPackedInt32[1]); + assertEquals(456, msg.repeatedPackedInt32[2]); + msg.clear(); + assertEquals(0, msg.repeatedPackedInt32.length); + msg.clear() + .repeatedPackedInt32 = new int[] { 456 }; + assertEquals(1, msg.repeatedPackedInt32.length); + assertEquals(456, msg.repeatedPackedInt32[0]); + msg.clear(); + assertEquals(0, msg.repeatedPackedInt32.length); + + // Test 1 entry + msg.clear() + .repeatedPackedInt32 = new int[] { 123 }; + assertEquals(1, msg.repeatedPackedInt32.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 7); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedPackedInt32.length); + assertEquals(123, newMsg.repeatedPackedInt32[0]); + + // Test 2 entries + msg.clear() + .repeatedPackedInt32 = new int[] { 123, 456 }; + assertEquals(2, msg.repeatedPackedInt32.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 9); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedPackedInt32.length); + assertEquals(123, newMsg.repeatedPackedInt32[0]); + assertEquals(456, newMsg.repeatedPackedInt32[1]); + } + + public void testNanoRepeatedPackedSfixed64() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + assertEquals(0, msg.repeatedPackedSfixed64.length); + msg.repeatedPackedSfixed64 = new long[] { 123, 789, 456 }; + assertEquals(789, msg.repeatedPackedSfixed64[1]); + assertEquals(456, msg.repeatedPackedSfixed64[2]); + msg.clear(); + assertEquals(0, msg.repeatedPackedSfixed64.length); + msg.clear() + .repeatedPackedSfixed64 = new long[] { 456 }; + assertEquals(1, msg.repeatedPackedSfixed64.length); + assertEquals(456, msg.repeatedPackedSfixed64[0]); + msg.clear(); + assertEquals(0, msg.repeatedPackedSfixed64.length); + + // Test 1 entry + msg.clear() + .repeatedPackedSfixed64 = new long[] { 123 }; + assertEquals(1, msg.repeatedPackedSfixed64.length); + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 14); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedPackedSfixed64.length); + assertEquals(123, newMsg.repeatedPackedSfixed64[0]); + + // Test 2 entries + msg.clear() + .repeatedPackedSfixed64 = new long[] { 123, 456 }; + assertEquals(2, msg.repeatedPackedSfixed64.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 22); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedPackedSfixed64.length); + assertEquals(123, newMsg.repeatedPackedSfixed64[0]); + assertEquals(456, newMsg.repeatedPackedSfixed64[1]); + } + + public void testNanoRepeatedPackedNestedEnum() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.repeatedPackedNestedEnum = new int[] { + TestAllTypesNano.FOO, + TestAllTypesNano.BAR, + TestAllTypesNano.BAZ + }; + assertEquals(3, msg.repeatedPackedNestedEnum.length); + assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]); + assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[1]); + assertEquals(TestAllTypesNano.BAZ, msg.repeatedPackedNestedEnum[2]); + msg.clear(); + assertEquals(0, msg.repeatedPackedNestedEnum.length); + msg.clear() + .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.BAR }; + assertEquals(1, msg.repeatedPackedNestedEnum.length); + assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[0]); + msg.clear(); + assertEquals(0, msg.repeatedPackedNestedEnum.length); + + // Test 1 entry + msg.clear() + .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.FOO }; + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 7); + assertEquals(result.length, msgSerializedSize); + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(1, newMsg.repeatedPackedNestedEnum.length); + assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]); + + // Test 2 entries + msg.clear() + .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.FOO, TestAllTypesNano.BAR }; + assertEquals(2, msg.repeatedPackedNestedEnum.length); + result = MessageNano.toByteArray(msg); + msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 8); + assertEquals(result.length, msgSerializedSize); + + newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(2, newMsg.repeatedPackedNestedEnum.length); + assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]); + assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[1]); + } + + public void testNanoRepeatedInt32ReMerge() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.repeatedInt32 = new int[] { 234 }; + byte [] result1 = MessageNano.toByteArray(msg); + + msg.clear().optionalInt32 = 789; + byte [] result2 = MessageNano.toByteArray(msg); + + msg.clear().repeatedInt32 = new int[] { 123, 456 }; + byte [] result3 = MessageNano.toByteArray(msg); + + // Concatenate the three serializations and read as one message. + byte [] result = new byte[result1.length + result2.length + result3.length]; + System.arraycopy(result1, 0, result, 0, result1.length); + System.arraycopy(result2, 0, result, result1.length, result2.length); + System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(789, newMsg.optionalInt32); + assertEquals(3, newMsg.repeatedInt32.length); + assertEquals(234, newMsg.repeatedInt32[0]); + assertEquals(123, newMsg.repeatedInt32[1]); + assertEquals(456, newMsg.repeatedInt32[2]); + } + + public void testNanoRepeatedNestedEnumReMerge() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + msg.repeatedNestedEnum = new int[] { TestAllTypesNano.FOO }; + byte [] result1 = MessageNano.toByteArray(msg); + + msg.clear().optionalInt32 = 789; + byte [] result2 = MessageNano.toByteArray(msg); + + msg.clear().repeatedNestedEnum = new int[] { TestAllTypesNano.BAR, TestAllTypesNano.FOO }; + byte [] result3 = MessageNano.toByteArray(msg); + + // Concatenate the three serializations and read as one message. + byte [] result = new byte[result1.length + result2.length + result3.length]; + System.arraycopy(result1, 0, result, 0, result1.length); + System.arraycopy(result2, 0, result, result1.length, result2.length); + System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(789, newMsg.optionalInt32); + assertEquals(3, newMsg.repeatedNestedEnum.length); + assertEquals(TestAllTypesNano.FOO, newMsg.repeatedNestedEnum[0]); + assertEquals(TestAllTypesNano.BAR, newMsg.repeatedNestedEnum[1]); + assertEquals(TestAllTypesNano.FOO, newMsg.repeatedNestedEnum[2]); + } + + public void testNanoRepeatedNestedMessageReMerge() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + TestAllTypesNano.NestedMessage nestedMsg0 = + new TestAllTypesNano.NestedMessage(); + nestedMsg0.bb = 0; + TestAllTypesNano.NestedMessage nestedMsg1 = + new TestAllTypesNano.NestedMessage(); + nestedMsg1.bb = 1; + TestAllTypesNano.NestedMessage nestedMsg2 = + new TestAllTypesNano.NestedMessage(); + nestedMsg2.bb = 2; + + msg.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0 }; + byte [] result1 = MessageNano.toByteArray(msg); + + msg.clear().optionalInt32 = 789; + byte [] result2 = MessageNano.toByteArray(msg); + + msg.clear().repeatedNestedMessage = + new TestAllTypesNano.NestedMessage[] { nestedMsg1, nestedMsg2 }; + byte [] result3 = MessageNano.toByteArray(msg); + + // Concatenate the three serializations and read as one message. + byte [] result = new byte[result1.length + result2.length + result3.length]; + System.arraycopy(result1, 0, result, 0, result1.length); + System.arraycopy(result2, 0, result, result1.length, result2.length); + System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length); + + TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); + assertEquals(789, newMsg.optionalInt32); + assertEquals(3, newMsg.repeatedNestedMessage.length); + assertEquals(nestedMsg0.bb, newMsg.repeatedNestedMessage[0].bb); + assertEquals(nestedMsg1.bb, newMsg.repeatedNestedMessage[1].bb); + assertEquals(nestedMsg2.bb, newMsg.repeatedNestedMessage[2].bb); + } + + public void testNanoDefaults() throws Exception { + TestAllTypesNano msg = new TestAllTypesNano(); + for (int i = 0; i < 2; i++) { + assertEquals(41, msg.defaultInt32); + assertEquals(42, msg.defaultInt64); + assertEquals(43, msg.defaultUint32); + assertEquals(44, msg.defaultUint64); + assertEquals(-45, msg.defaultSint32); + assertEquals(46, msg.defaultSint64); + assertEquals(47, msg.defaultFixed32); + assertEquals(48, msg.defaultFixed64); + assertEquals(49, msg.defaultSfixed32); + assertEquals(-50, msg.defaultSfixed64); + assertTrue(51.5f == msg.defaultFloat); + assertTrue(52.0e3 == msg.defaultDouble); + assertEquals(true, msg.defaultBool); + assertEquals("hello", msg.defaultString); + assertEquals("world", new String(msg.defaultBytes, "UTF-8")); + assertEquals("dünya", msg.defaultStringNonascii); + assertEquals("dünyab", new String(msg.defaultBytesNonascii, "UTF-8")); + assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum); + assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum); + assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum); + + // Default values are not output, except for required fields. + byte [] result = MessageNano.toByteArray(msg); + int msgSerializedSize = msg.getSerializedSize(); + //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length); + assertTrue(msgSerializedSize == 3); + assertEquals(result.length, msgSerializedSize); + msg.clear(); + } + } + + /** + * Test that a bug in skipRawBytes() has been fixed: if the skip skips + * exactly up to a limit, this should not break things. + */ + public void testSkipRawBytesBug() throws Exception { + byte[] rawBytes = new byte[] { 1, 2 }; + CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(rawBytes); + + int limit = input.pushLimit(1); + input.skipRawBytes(1); + input.popLimit(limit); + assertEquals(2, input.readRawByte()); + } + + /** + * Test that a bug in skipRawBytes() has been fixed: if the skip skips + * past the end of a buffer with a limit that has been set past the end of + * that buffer, this should not break things. + */ + public void testSkipRawBytesPastEndOfBufferWithLimit() throws Exception { + byte[] rawBytes = new byte[] { 1, 2, 3, 4, 5 }; + CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(rawBytes); + + int limit = input.pushLimit(4); + // In order to expose the bug we need to read at least one byte to prime the + // buffer inside the CodedInputStream. + assertEquals(1, input.readRawByte()); + // Skip to the end of the limit. + input.skipRawBytes(3); + assertTrue(input.isAtEnd()); + input.popLimit(limit); + assertEquals(5, input.readRawByte()); + } +} |