aboutsummaryrefslogtreecommitdiff
path: root/java/core/src/main/java/com/google/protobuf/CodedInputStream.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/CodedInputStream.java')
-rw-r--r--java/core/src/main/java/com/google/protobuf/CodedInputStream.java130
1 files changed, 24 insertions, 106 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java
index 1297462e..df4cc664 100644
--- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java
+++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java
@@ -64,12 +64,6 @@ public abstract class CodedInputStream {
// Integer.MAX_VALUE == 0x7FFFFFF == INT_MAX from limits.h
private static final int DEFAULT_SIZE_LIMIT = Integer.MAX_VALUE;
- /**
- * Whether to enable our custom UTF-8 decode codepath which does not use {@link StringCoding}.
- * Currently disabled.
- */
- private static final boolean ENABLE_CUSTOM_UTF8_DECODE = false;
-
/** Visible for subclasses. See setRecursionLimit() */
int recursionDepth;
@@ -83,8 +77,11 @@ public abstract class CodedInputStream {
return newInstance(input, DEFAULT_BUFFER_SIZE);
}
- /** Create a new CodedInputStream wrapping the given InputStream. */
- static CodedInputStream newInstance(final InputStream input, int bufferSize) {
+ /** Create a new CodedInputStream wrapping the given InputStream, with a specified buffer size. */
+ public static CodedInputStream newInstance(final InputStream input, int bufferSize) {
+ if (bufferSize <= 0) {
+ throw new IllegalArgumentException("bufferSize must be > 0");
+ }
if (input == null) {
// TODO(nathanmittler): Ideally we should throw here. This is done for backward compatibility.
return newInstance(EMPTY_BYTE_ARRAY);
@@ -136,7 +133,7 @@ public abstract class CodedInputStream {
/** Create a new CodedInputStream wrapping the given byte array slice. */
public static CodedInputStream newInstance(final byte[] buf, final int off, final int len) {
- return newInstance(buf, off, len, false /* bufferIsImmutable */);
+ return newInstance(buf, off, len, /* bufferIsImmutable= */ false);
}
/** Create a new CodedInputStream wrapping the given byte array slice. */
@@ -172,7 +169,7 @@ public abstract class CodedInputStream {
* trying to alter the ByteBuffer's status.
*/
public static CodedInputStream newInstance(ByteBuffer buf) {
- return newInstance(buf, false /* bufferIsImmutable */);
+ return newInstance(buf, /* bufferIsImmutable= */ false);
}
/** Create a new CodedInputStream wrapping the given buffer. */
@@ -416,22 +413,7 @@ public abstract class CodedInputStream {
return oldLimit;
}
-
- private boolean explicitDiscardUnknownFields = false;
-
- private static volatile boolean proto3DiscardUnknownFieldsDefault = false;
-
- static void setProto3DiscardUnknownsByDefaultForTest() {
- proto3DiscardUnknownFieldsDefault = true;
- }
-
- static void setProto3KeepUnknownsByDefaultForTest() {
- proto3DiscardUnknownFieldsDefault = false;
- }
-
- static boolean getProto3DiscardUnknownFieldsDefault() {
- return proto3DiscardUnknownFieldsDefault;
- }
+ private boolean shouldDiscardUnknownFields = false;
/**
* Sets this {@code CodedInputStream} to discard unknown fields. Only applies to full runtime
@@ -442,7 +424,7 @@ public abstract class CodedInputStream {
* runtime.
*/
final void discardUnknownFields() {
- explicitDiscardUnknownFields = true;
+ shouldDiscardUnknownFields = true;
}
/**
@@ -450,7 +432,7 @@ public abstract class CodedInputStream {
* default.
*/
final void unsetDiscardUnknownFields() {
- explicitDiscardUnknownFields = false;
+ shouldDiscardUnknownFields = false;
}
/**
@@ -458,19 +440,7 @@ public abstract class CodedInputStream {
* runtime messages.
*/
final boolean shouldDiscardUnknownFields() {
- return explicitDiscardUnknownFields;
- }
-
- /**
- * Whether unknown fields in this input stream should be discarded during parsing for proto3 full
- * runtime messages.
- *
- * <p>This function was temporarily introduced before proto3 unknown fields behavior is changed.
- * TODO(liujisi): remove this and related code in GeneratedMessage after proto3 unknown
- * fields migration is done.
- */
- final boolean shouldDiscardUnknownFieldsProto3() {
- return explicitDiscardUnknownFields ? true : proto3DiscardUnknownFieldsDefault;
+ return shouldDiscardUnknownFields;
}
/**
@@ -831,19 +801,9 @@ public abstract class CodedInputStream {
public String readStringRequireUtf8() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= (limit - pos)) {
- if (ENABLE_CUSTOM_UTF8_DECODE) {
- String result = Utf8.decodeUtf8(buffer, pos, size);
- pos += size;
- return result;
- } else {
- // TODO(martinrb): We could save a pass by validating while decoding.
- if (!Utf8.isValidUtf8(buffer, pos, pos + size)) {
- throw InvalidProtocolBufferException.invalidUtf8();
- }
- final int tempPos = pos;
- pos += size;
- return new String(buffer, tempPos, size, UTF_8);
- }
+ String result = Utf8.decodeUtf8(buffer, pos, size);
+ pos += size;
+ return result;
}
if (size == 0) {
@@ -1559,25 +1519,10 @@ public abstract class CodedInputStream {
public String readStringRequireUtf8() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= remaining()) {
- if (ENABLE_CUSTOM_UTF8_DECODE) {
- final int bufferPos = bufferPos(pos);
- String result = Utf8.decodeUtf8(buffer, bufferPos, size);
- pos += size;
- return result;
- } else {
- // TODO(nathanmittler): Is there a way to avoid this copy?
- // The same as readBytes' logic
- byte[] bytes = new byte[size];
- UnsafeUtil.copyMemory(pos, bytes, 0, size);
- // TODO(martinrb): We could save a pass by validating while decoding.
- if (!Utf8.isValidUtf8(bytes)) {
- throw InvalidProtocolBufferException.invalidUtf8();
- }
-
- String result = new String(bytes, UTF_8);
- pos += size;
- return result;
- }
+ final int bufferPos = bufferPos(pos);
+ String result = Utf8.decodeUtf8(buffer, bufferPos, size);
+ pos += size;
+ return result;
}
if (size == 0) {
@@ -2345,15 +2290,7 @@ public abstract class CodedInputStream {
bytes = readRawBytesSlowPath(size);
tempPos = 0;
}
- if (ENABLE_CUSTOM_UTF8_DECODE) {
- return Utf8.decodeUtf8(bytes, tempPos, size);
- } else {
- // TODO(martinrb): We could save a pass by validating while decoding.
- if (!Utf8.isValidUtf8(bytes, tempPos, tempPos + size)) {
- throw InvalidProtocolBufferException.invalidUtf8();
- }
- return new String(bytes, tempPos, size, UTF_8);
- }
+ return Utf8.decodeUtf8(bytes, tempPos, size);
}
@Override
@@ -3373,34 +3310,15 @@ public abstract class CodedInputStream {
public String readStringRequireUtf8() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= currentByteBufferLimit - currentByteBufferPos) {
- if (ENABLE_CUSTOM_UTF8_DECODE) {
- final int bufferPos = (int) (currentByteBufferPos - currentByteBufferStartPos);
- String result = Utf8.decodeUtf8(currentByteBuffer, bufferPos, size);
- currentByteBufferPos += size;
- return result;
- } else {
- byte[] bytes = new byte[size];
- UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, size);
- if (!Utf8.isValidUtf8(bytes)) {
- throw InvalidProtocolBufferException.invalidUtf8();
- }
- String result = new String(bytes, UTF_8);
- currentByteBufferPos += size;
- return result;
- }
+ final int bufferPos = (int) (currentByteBufferPos - currentByteBufferStartPos);
+ String result = Utf8.decodeUtf8(currentByteBuffer, bufferPos, size);
+ currentByteBufferPos += size;
+ return result;
}
if (size >= 0 && size <= remaining()) {
byte[] bytes = new byte[size];
readRawBytesTo(bytes, 0, size);
- if (ENABLE_CUSTOM_UTF8_DECODE) {
- return Utf8.decodeUtf8(bytes, 0, size);
- } else {
- if (!Utf8.isValidUtf8(bytes)) {
- throw InvalidProtocolBufferException.invalidUtf8();
- }
- String result = new String(bytes, UTF_8);
- return result;
- }
+ return Utf8.decodeUtf8(bytes, 0, size);
}
if (size == 0) {