diff options
author | Brian Duff <bduff@google.com> | 2015-10-22 22:23:15 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-10-22 22:23:15 +0000 |
commit | f0b8850a8e63b6811f9470dad5704c613724377f (patch) | |
tree | 2acf69463201d68a5ac74118204b8ff6ea338b46 | |
parent | c0bdb5415e78dfb6a07257fd7538960d6bef7cca (diff) | |
parent | 9b8022f792896f4a6831abe8e30ee5b7977a3677 (diff) | |
download | protobuf-brillo-m7-release.tar.gz |
Merge "Minor cleanup in CodedInputByteBufferNano."studio-1.2-releasebrillo-m7-releasebrillo-m7-mr-devbrillo-m7-dev
-rw-r--r-- | java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java | 41 |
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. */ |