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 532ff3ad99b8..57be310f0027 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -1348,15 +1348,16 @@ public final void writeUInt32NoTag(int value) throws IOException { public final void writeFixed32NoTag(int value) throws IOException { int position = this.position; // Perf: hoist field to register to avoid load/stores. try { - buffer[position++] = (byte) (value & 0xFF); - buffer[position++] = (byte) ((value >> 8) & 0xFF); - buffer[position++] = (byte) ((value >> 16) & 0xFF); - buffer[position++] = (byte) ((value >> 24) & 0xFF); + buffer[position] = (byte) (value & 0xFF); + buffer[position + 1] = (byte) ((value >> 8) & 0xFF); + buffer[position + 2] = (byte) ((value >> 16) & 0xFF); + buffer[position + 3] = (byte) ((value >> 24) & 0xFF); } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e); + String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED32_SIZE), e); } - this.position = position; // Only update position if we stayed within the array bounds. + // Only update position if we stayed within the array bounds. + this.position = position + FIXED32_SIZE; } @Override @@ -1393,19 +1394,20 @@ public final void writeUInt64NoTag(long value) throws IOException { public final void writeFixed64NoTag(long value) throws IOException { int position = this.position; // Perf: hoist field to register to avoid load/stores. try { - buffer[position++] = (byte) ((int) (value) & 0xFF); - buffer[position++] = (byte) ((int) (value >> 8) & 0xFF); - buffer[position++] = (byte) ((int) (value >> 16) & 0xFF); - buffer[position++] = (byte) ((int) (value >> 24) & 0xFF); - buffer[position++] = (byte) ((int) (value >> 32) & 0xFF); - buffer[position++] = (byte) ((int) (value >> 40) & 0xFF); - buffer[position++] = (byte) ((int) (value >> 48) & 0xFF); - buffer[position++] = (byte) ((int) (value >> 56) & 0xFF); + buffer[position] = (byte) ((int) (value) & 0xFF); + buffer[position + 1] = (byte) ((int) (value >> 8) & 0xFF); + buffer[position + 2] = (byte) ((int) (value >> 16) & 0xFF); + buffer[position + 3] = (byte) ((int) (value >> 24) & 0xFF); + buffer[position + 4] = (byte) ((int) (value >> 32) & 0xFF); + buffer[position + 5] = (byte) ((int) (value >> 40) & 0xFF); + buffer[position + 6] = (byte) ((int) (value >> 48) & 0xFF); + buffer[position + 7] = (byte) ((int) (value >> 56) & 0xFF); } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException( - String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e); + String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED64_SIZE), e); } - this.position = position; // Only update position if we stayed within the array bounds. + // Only update position if we stayed within the array bounds. + this.position = position + FIXED64_SIZE; } @Override