Index: third_party/protobuf/java/core/src/main/java/com/google/protobuf/CodedInputStream.java |
diff --git a/third_party/protobuf/java/src/main/java/com/google/protobuf/CodedInputStream.java b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/CodedInputStream.java |
similarity index 95% |
rename from third_party/protobuf/java/src/main/java/com/google/protobuf/CodedInputStream.java |
rename to third_party/protobuf/java/core/src/main/java/com/google/protobuf/CodedInputStream.java |
index adc915369a40a0be258dd88f0a1fa4770e588955..e8860651a4fdc604a8f32c9ea1423a0a1354eddb 100644 |
--- a/third_party/protobuf/java/src/main/java/com/google/protobuf/CodedInputStream.java |
+++ b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/CodedInputStream.java |
@@ -55,7 +55,14 @@ public final class CodedInputStream { |
* Create a new CodedInputStream wrapping the given InputStream. |
*/ |
public static CodedInputStream newInstance(final InputStream input) { |
- return new CodedInputStream(input); |
+ return new CodedInputStream(input, BUFFER_SIZE); |
+ } |
+ |
+ /** |
+ * Create a new CodedInputStream wrapping the given InputStream. |
+ */ |
+ static CodedInputStream newInstance(final InputStream input, int bufferSize) { |
+ return new CodedInputStream(input, bufferSize); |
} |
/** |
@@ -70,7 +77,15 @@ public final class CodedInputStream { |
*/ |
public static CodedInputStream newInstance(final byte[] buf, final int off, |
final int len) { |
- CodedInputStream result = new CodedInputStream(buf, off, len); |
+ return newInstance(buf, off, len, false /* bufferIsImmutable */); |
+ } |
+ |
+ /** |
+ * Create a new CodedInputStream wrapping the given byte array slice. |
+ */ |
+ static CodedInputStream newInstance( |
+ final byte[] buf, final int off, final int len, final boolean bufferIsImmutable) { |
+ CodedInputStream result = new CodedInputStream(buf, off, len, bufferIsImmutable); |
try { |
// Some uses of CodedInputStream can be more efficient if they know |
// exactly how many bytes are available. By pushing the end point of the |
@@ -113,31 +128,6 @@ public final class CodedInputStream { |
} |
} |
- /** |
- * Create a new CodedInputStream wrapping a LiteralByteString. |
- */ |
- static CodedInputStream newInstance(LiteralByteString byteString) { |
- CodedInputStream result = new CodedInputStream(byteString); |
- try { |
- // Some uses of CodedInputStream can be more efficient if they know |
- // exactly how many bytes are available. By pushing the end point of the |
- // buffer as a limit, we allow them to get this information via |
- // getBytesUntilLimit(). Pushing a limit that we know is at the end of |
- // the stream can never hurt, since we can never past that point anyway. |
- result.pushLimit(byteString.size()); |
- } catch (InvalidProtocolBufferException ex) { |
- // The only reason pushLimit() might throw an exception here is if len |
- // is negative. Normally pushLimit()'s parameter comes directly off the |
- // wire, so it's important to catch exceptions in case of corrupt or |
- // malicious data. However, in this case, we expect that len is not a |
- // user-supplied value, so we can assume that it being negative indicates |
- // a programming error. Therefore, throwing an unchecked exception is |
- // appropriate. |
- throw new IllegalArgumentException(ex); |
- } |
- return result; |
- } |
- |
// ----------------------------------------------------------------- |
/** |
@@ -378,6 +368,11 @@ public final class CodedInputStream { |
return result; |
} else if (size == 0) { |
return ""; |
+ } else if (size <= bufferSize) { |
+ refillBuffer(size); |
+ String result = new String(buffer, bufferPos, size, Internal.UTF_8); |
+ bufferPos += size; |
+ return result; |
} else { |
// Slow path: Build a byte array first then copy it. |
return new String(readRawBytesSlowPath(size), Internal.UTF_8); |
@@ -392,14 +387,21 @@ public final class CodedInputStream { |
public String readStringRequireUtf8() throws IOException { |
final int size = readRawVarint32(); |
final byte[] bytes; |
- int pos = bufferPos; |
- if (size <= (bufferSize - pos) && size > 0) { |
+ final int oldPos = bufferPos; |
+ final int pos; |
+ if (size <= (bufferSize - oldPos) && size > 0) { |
// Fast path: We already have the bytes in a contiguous buffer, so |
// just copy directly from it. |
bytes = buffer; |
- bufferPos = pos + size; |
+ bufferPos = oldPos + size; |
+ pos = oldPos; |
} else if (size == 0) { |
return ""; |
+ } else if (size <= bufferSize) { |
+ refillBuffer(size); |
+ bytes = buffer; |
+ pos = 0; |
+ bufferPos = pos + size; |
} else { |
// Slow path: Build a byte array first then copy it. |
bytes = readRawBytesSlowPath(size); |
@@ -506,7 +508,7 @@ public final class CodedInputStream { |
// Fast path: We already have the bytes in a contiguous buffer, so |
// just copy directly from it. |
final ByteString result = bufferIsImmutable && enableAliasing |
- ? new BoundedByteString(buffer, bufferPos, size) |
+ ? ByteString.wrap(buffer, bufferPos, size) |
: ByteString.copyFrom(buffer, bufferPos, size); |
bufferPos += size; |
return result; |
@@ -514,7 +516,7 @@ public final class CodedInputStream { |
return ByteString.EMPTY; |
} else { |
// Slow path: Build a byte array first then copy it. |
- return new LiteralByteString(readRawBytesSlowPath(size)); |
+ return ByteString.wrap(readRawBytesSlowPath(size)); |
} |
} |
@@ -886,17 +888,18 @@ public final class CodedInputStream { |
private static final int DEFAULT_SIZE_LIMIT = 64 << 20; // 64MB |
private static final int BUFFER_SIZE = 4096; |
- private CodedInputStream(final byte[] buffer, final int off, final int len) { |
+ private CodedInputStream( |
+ final byte[] buffer, final int off, final int len, boolean bufferIsImmutable) { |
this.buffer = buffer; |
bufferSize = off + len; |
bufferPos = off; |
totalBytesRetired = -off; |
input = null; |
- bufferIsImmutable = false; |
+ this.bufferIsImmutable = bufferIsImmutable; |
} |
- private CodedInputStream(final InputStream input) { |
- buffer = new byte[BUFFER_SIZE]; |
+ private CodedInputStream(final InputStream input, int bufferSize) { |
+ buffer = new byte[bufferSize]; |
bufferSize = 0; |
bufferPos = 0; |
totalBytesRetired = 0; |
@@ -904,15 +907,6 @@ public final class CodedInputStream { |
bufferIsImmutable = false; |
} |
- private CodedInputStream(final LiteralByteString byteString) { |
- buffer = byteString.bytes; |
- bufferPos = byteString.getOffsetIntoBytes(); |
- bufferSize = bufferPos + byteString.size(); |
- totalBytesRetired = -bufferPos; |
- input = null; |
- bufferIsImmutable = true; |
- } |
- |
public void enableAliasing(boolean enabled) { |
this.enableAliasing = enabled; |
} |