aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Mittler <nathanmittler@google.com>2015-10-22 15:19:42 -0700
committerBrian Duff <bduff@google.com>2015-10-22 15:21:13 -0700
commit9b8022f792896f4a6831abe8e30ee5b7977a3677 (patch)
tree2acf69463201d68a5ac74118204b8ff6ea338b46
parentc0bdb5415e78dfb6a07257fd7538960d6bef7cca (diff)
downloadprotobuf-9b8022f792896f4a6831abe8e30ee5b7977a3677.tar.gz
Minor cleanup in CodedInputByteBufferNano.
There is some cruft in the CodedInputByteBufferNano related to fast/slow path for reading bytes that is not applicable for nano, due to the fact that all of the data is buffered into a final byte array. Cleaning up the code a bit to make it more explicit that truncation will result if the data is not buffered. Change-Id: I94c44e970790df0b9b6b598b8dfe1d510dd40bc0
-rw-r--r--java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java41
1 files changed, 21 insertions, 20 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java b/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
index 7cfbf8a27..844d0a896 100644
--- a/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
@@ -187,16 +187,16 @@ public final class CodedInputByteBufferNano {
/** 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");
+ if (size < 0) {
+ throw InvalidProtocolBufferNanoException.negativeSize();
}
+ if (size > (bufferSize - bufferPos)) {
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+
+ final String result = new String(buffer, bufferPos, size, "UTF-8");
+ bufferPos += size;
+ return result;
}
/** Read a {@code group} field value from the stream. */
@@ -229,19 +229,20 @@ public final class CodedInputByteBufferNano {
/** 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 if (size == 0) {
+ if (size < 0) {
+ throw InvalidProtocolBufferNanoException.negativeSize();
+ }
+ if (size == 0) {
return WireFormatNano.EMPTY_BYTES;
- } else {
- // Slow path: Build a byte array first then copy it.
- return readRawBytes(size);
}
+ if (size > (bufferSize - bufferPos)) {
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+
+ final byte[] result = new byte[size];
+ System.arraycopy(buffer, bufferPos, result, 0, size);
+ bufferPos += size;
+ return result;
}
/** Read a {@code uint32} field value from the stream. */