aboutsummaryrefslogtreecommitdiff
path: root/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/CodedOutputStream.java')
-rw-r--r--java/core/src/main/java/com/google/protobuf/CodedOutputStream.java100
1 files changed, 53 insertions, 47 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
index 3e32c2c5..7b1ac651 100644
--- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
+++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
@@ -30,8 +30,8 @@
package com.google.protobuf;
-import static com.google.protobuf.WireFormat.FIXED_32_SIZE;
-import static com.google.protobuf.WireFormat.FIXED_64_SIZE;
+import static com.google.protobuf.WireFormat.FIXED32_SIZE;
+import static com.google.protobuf.WireFormat.FIXED64_SIZE;
import static com.google.protobuf.WireFormat.MAX_VARINT_SIZE;
import static java.lang.Math.max;
@@ -59,13 +59,12 @@ import java.util.logging.Logger;
public abstract class CodedOutputStream extends ByteOutput {
private static final Logger logger = Logger.getLogger(CodedOutputStream.class.getName());
private static final boolean HAS_UNSAFE_ARRAY_OPERATIONS = UnsafeUtil.hasUnsafeArrayOperations();
- private static final long ARRAY_BASE_OFFSET = UnsafeUtil.getArrayBaseOffset();
/**
* @deprecated Use {@link #computeFixed32SizeNoTag(int)} instead.
*/
@Deprecated
- public static final int LITTLE_ENDIAN_32_SIZE = FIXED_32_SIZE;
+ public static final int LITTLE_ENDIAN_32_SIZE = FIXED32_SIZE;
/**
* The buffer size used in {@link #newInstance(OutputStream)}.
@@ -184,7 +183,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* maps are sorted on the lexicographical order of the UTF8 encoded keys.
* </ul>
*/
- void useDeterministicSerialization() {
+ public void useDeterministicSerialization() {
serializationDeterministic = true;
}
@@ -378,6 +377,7 @@ public abstract class CodedOutputStream extends ByteOutput {
public abstract void writeMessage(final int fieldNumber, final MessageLite value)
throws IOException;
+
/**
* Write a MessageSet extension field to the stream. For historical reasons,
* the wire format differs from normal fields.
@@ -482,6 +482,7 @@ public abstract class CodedOutputStream extends ByteOutput {
// Abstract to avoid overhead of additional virtual method calls.
public abstract void writeMessageNoTag(final MessageLite value) throws IOException;
+
//=================================================================
@ExperimentalApi
@@ -667,6 +668,7 @@ public abstract class CodedOutputStream extends ByteOutput {
return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value);
}
+
/**
* Compute the number of bytes that would be needed to encode a
* MessageSet extension to the stream. For historical reasons,
@@ -755,7 +757,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* {@code fixed32} field.
*/
public static int computeFixed32SizeNoTag(@SuppressWarnings("unused") final int unused) {
- return FIXED_32_SIZE;
+ return FIXED32_SIZE;
}
/**
@@ -763,7 +765,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* {@code sfixed32} field.
*/
public static int computeSFixed32SizeNoTag(@SuppressWarnings("unused") final int unused) {
- return FIXED_32_SIZE;
+ return FIXED32_SIZE;
}
/**
@@ -813,7 +815,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* {@code fixed64} field.
*/
public static int computeFixed64SizeNoTag(@SuppressWarnings("unused") final long unused) {
- return FIXED_64_SIZE;
+ return FIXED64_SIZE;
}
/**
@@ -821,7 +823,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* {@code sfixed64} field.
*/
public static int computeSFixed64SizeNoTag(@SuppressWarnings("unused") final long unused) {
- return FIXED_64_SIZE;
+ return FIXED64_SIZE;
}
/**
@@ -829,7 +831,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* {@code float} field, including tag.
*/
public static int computeFloatSizeNoTag(@SuppressWarnings("unused") final float unused) {
- return FIXED_32_SIZE;
+ return FIXED32_SIZE;
}
/**
@@ -837,7 +839,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* {@code double} field, including tag.
*/
public static int computeDoubleSizeNoTag(@SuppressWarnings("unused") final double unused) {
- return FIXED_64_SIZE;
+ return FIXED64_SIZE;
}
/**
@@ -914,6 +916,7 @@ public abstract class CodedOutputStream extends ByteOutput {
return computeLengthDelimitedFieldSize(value.getSerializedSize());
}
+
static int computeLengthDelimitedFieldSize(int fieldLength) {
return computeUInt32SizeNoTag(fieldLength) + fieldLength;
}
@@ -1050,6 +1053,7 @@ public abstract class CodedOutputStream extends ByteOutput {
writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP);
}
+
/**
* Write a {@code group} field to the stream.
*
@@ -1060,6 +1064,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value.writeTo(this);
}
+
/**
* Compute the number of bytes that would be needed to encode a
* {@code group} field, including tag.
@@ -1071,6 +1076,7 @@ public abstract class CodedOutputStream extends ByteOutput {
return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value);
}
+
/**
* Compute the number of bytes that would be needed to encode a
* {@code group} field.
@@ -1080,6 +1086,7 @@ public abstract class CodedOutputStream extends ByteOutput {
return value.getSerializedSize();
}
+
/**
* Encode and write a varint. {@code value} is treated as
* unsigned, so it won't be sign-extended if negative.
@@ -1274,6 +1281,7 @@ public abstract class CodedOutputStream extends ByteOutput {
writeMessageNoTag(value);
}
+
@Override
public final void writeMessageSetExtension(final int fieldNumber, final MessageLite value)
throws IOException {
@@ -1298,6 +1306,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value.writeTo(this);
}
+
@Override
public final void write(byte value) throws IOException {
try {
@@ -1321,15 +1330,12 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public final void writeUInt32NoTag(int value) throws IOException {
if (HAS_UNSAFE_ARRAY_OPERATIONS && spaceLeft() >= MAX_VARINT_SIZE) {
- long pos = ARRAY_BASE_OFFSET + position;
while (true) {
if ((value & ~0x7F) == 0) {
- UnsafeUtil.putByte(buffer, pos++, (byte) value);
- position++;
+ UnsafeUtil.putByte(buffer, position++, (byte) value);
return;
} else {
- UnsafeUtil.putByte(buffer, pos++, (byte) ((value & 0x7F) | 0x80));
- position++;
+ UnsafeUtil.putByte(buffer, position++, (byte) ((value & 0x7F) | 0x80));
value >>>= 7;
}
}
@@ -1367,15 +1373,12 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public final void writeUInt64NoTag(long value) throws IOException {
if (HAS_UNSAFE_ARRAY_OPERATIONS && spaceLeft() >= MAX_VARINT_SIZE) {
- long pos = ARRAY_BASE_OFFSET + position;
while (true) {
if ((value & ~0x7FL) == 0) {
- UnsafeUtil.putByte(buffer, pos++, (byte) value);
- position++;
+ UnsafeUtil.putByte(buffer, position++, (byte) value);
return;
} else {
- UnsafeUtil.putByte(buffer, pos++, (byte) (((int) value & 0x7F) | 0x80));
- position++;
+ UnsafeUtil.putByte(buffer, position++, (byte) (((int) value & 0x7F) | 0x80));
value >>>= 7;
}
}
@@ -1615,6 +1618,7 @@ public abstract class CodedOutputStream extends ByteOutput {
writeMessageNoTag(value);
}
+
@Override
public void writeMessageSetExtension(final int fieldNumber, final MessageLite value)
throws IOException {
@@ -1639,6 +1643,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value.writeTo(this);
}
+
@Override
public void write(byte value) throws IOException {
try {
@@ -1935,6 +1940,7 @@ public abstract class CodedOutputStream extends ByteOutput {
writeMessageNoTag(value);
}
+
@Override
public void writeMessageSetExtension(int fieldNumber, MessageLite value) throws IOException {
writeTag(WireFormat.MESSAGE_SET_ITEM, WireFormat.WIRETYPE_START_GROUP);
@@ -1957,6 +1963,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value.writeTo(this);
}
+
@Override
public void write(byte value) throws IOException {
if (position >= limit) {
@@ -2030,7 +2037,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed32NoTag(int value) throws IOException {
buffer.putInt(bufferPos(position), value);
- position += FIXED_32_SIZE;
+ position += FIXED32_SIZE;
}
@Override
@@ -2064,7 +2071,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed64NoTag(long value) throws IOException {
buffer.putLong(bufferPos(position), value);
- position += FIXED_64_SIZE;
+ position += FIXED64_SIZE;
}
@Override
@@ -2081,8 +2088,7 @@ public abstract class CodedOutputStream extends ByteOutput {
String.format("Pos: %d, limit: %d, len: %d", position, limit, length));
}
- UnsafeUtil.copyMemory(
- value, UnsafeUtil.getArrayBaseOffset() + offset, null, position, length);
+ UnsafeUtil.copyMemory(value, offset, position, length);
position += length;
}
@@ -2249,19 +2255,17 @@ public abstract class CodedOutputStream extends ByteOutput {
*/
final void bufferUInt32NoTag(int value) {
if (HAS_UNSAFE_ARRAY_OPERATIONS) {
- final long originalPos = ARRAY_BASE_OFFSET + position;
- long pos = originalPos;
+ final long originalPos = position;
while (true) {
if ((value & ~0x7F) == 0) {
- UnsafeUtil.putByte(buffer, pos++, (byte) value);
+ UnsafeUtil.putByte(buffer, position++, (byte) value);
break;
} else {
- UnsafeUtil.putByte(buffer, pos++, (byte) ((value & 0x7F) | 0x80));
+ UnsafeUtil.putByte(buffer, position++, (byte) ((value & 0x7F) | 0x80));
value >>>= 7;
}
}
- int delta = (int) (pos - originalPos);
- position += delta;
+ int delta = (int) (position - originalPos);
totalBytesWritten += delta;
} else {
while (true) {
@@ -2284,19 +2288,17 @@ public abstract class CodedOutputStream extends ByteOutput {
*/
final void bufferUInt64NoTag(long value) {
if (HAS_UNSAFE_ARRAY_OPERATIONS) {
- final long originalPos = ARRAY_BASE_OFFSET + position;
- long pos = originalPos;
+ final long originalPos = position;
while (true) {
if ((value & ~0x7FL) == 0) {
- UnsafeUtil.putByte(buffer, pos++, (byte) value);
+ UnsafeUtil.putByte(buffer, position++, (byte) value);
break;
} else {
- UnsafeUtil.putByte(buffer, pos++, (byte) (((int) value & 0x7F) | 0x80));
+ UnsafeUtil.putByte(buffer, position++, (byte) (((int) value & 0x7F) | 0x80));
value >>>= 7;
}
}
- int delta = (int) (pos - originalPos);
- position += delta;
+ int delta = (int) (position - originalPos);
totalBytesWritten += delta;
} else {
while (true) {
@@ -2322,7 +2324,7 @@ public abstract class CodedOutputStream extends ByteOutput {
buffer[position++] = (byte) ((value >> 8) & 0xFF);
buffer[position++] = (byte) ((value >> 16) & 0xFF);
buffer[position++] = (byte) ((value >> 24) & 0xFF);
- totalBytesWritten += FIXED_32_SIZE;
+ totalBytesWritten += FIXED32_SIZE;
}
/**
@@ -2338,7 +2340,7 @@ public abstract class CodedOutputStream extends ByteOutput {
buffer[position++] = (byte) ((int) (value >> 40) & 0xFF);
buffer[position++] = (byte) ((int) (value >> 48) & 0xFF);
buffer[position++] = (byte) ((int) (value >> 56) & 0xFF);
- totalBytesWritten += FIXED_64_SIZE;
+ totalBytesWritten += FIXED64_SIZE;
}
}
@@ -2379,7 +2381,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed32(final int fieldNumber, final int value) throws IOException {
- flushIfNotAvailable(MAX_VARINT_SIZE + FIXED_32_SIZE);
+ flushIfNotAvailable(MAX_VARINT_SIZE + FIXED32_SIZE);
bufferTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
bufferFixed32NoTag(value);
}
@@ -2393,7 +2395,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed64(final int fieldNumber, final long value) throws IOException {
- flushIfNotAvailable(MAX_VARINT_SIZE + FIXED_64_SIZE);
+ flushIfNotAvailable(MAX_VARINT_SIZE + FIXED64_SIZE);
bufferTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
bufferFixed64NoTag(value);
}
@@ -2468,6 +2470,7 @@ public abstract class CodedOutputStream extends ByteOutput {
writeMessageNoTag(value);
}
+
@Override
public void writeMessageSetExtension(final int fieldNumber, final MessageLite value)
throws IOException {
@@ -2492,6 +2495,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value.writeTo(this);
}
+
@Override
public void write(byte value) throws IOException {
if (position == limit) {
@@ -2519,7 +2523,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed32NoTag(final int value) throws IOException {
- flushIfNotAvailable(FIXED_32_SIZE);
+ flushIfNotAvailable(FIXED32_SIZE);
bufferFixed32NoTag(value);
}
@@ -2531,7 +2535,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed64NoTag(final long value) throws IOException {
- flushIfNotAvailable(FIXED_64_SIZE);
+ flushIfNotAvailable(FIXED64_SIZE);
bufferFixed64NoTag(value);
}
@@ -2682,7 +2686,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed32(final int fieldNumber, final int value) throws IOException {
- flushIfNotAvailable(MAX_VARINT_SIZE + FIXED_32_SIZE);
+ flushIfNotAvailable(MAX_VARINT_SIZE + FIXED32_SIZE);
bufferTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
bufferFixed32NoTag(value);
}
@@ -2696,7 +2700,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed64(final int fieldNumber, final long value) throws IOException {
- flushIfNotAvailable(MAX_VARINT_SIZE + FIXED_64_SIZE);
+ flushIfNotAvailable(MAX_VARINT_SIZE + FIXED64_SIZE);
bufferTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
bufferFixed64NoTag(value);
}
@@ -2771,6 +2775,7 @@ public abstract class CodedOutputStream extends ByteOutput {
writeMessageNoTag(value);
}
+
@Override
public void writeMessageSetExtension(final int fieldNumber, final MessageLite value)
throws IOException {
@@ -2795,6 +2800,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value.writeTo(this);
}
+
@Override
public void write(byte value) throws IOException {
if (position == limit) {
@@ -2822,7 +2828,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed32NoTag(final int value) throws IOException {
- flushIfNotAvailable(FIXED_32_SIZE);
+ flushIfNotAvailable(FIXED32_SIZE);
bufferFixed32NoTag(value);
}
@@ -2834,7 +2840,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@Override
public void writeFixed64NoTag(final long value) throws IOException {
- flushIfNotAvailable(FIXED_64_SIZE);
+ flushIfNotAvailable(FIXED64_SIZE);
bufferFixed64NoTag(value);
}