From 6ea07ef84ccd10631e891a199d0b96fb09b72b16 Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Mon, 7 Oct 2024 19:23:13 -0700 Subject: [PATCH] Remove unnecessary intermediate casts in CodedOutputStream https://docs.oracle.com/javase/specs/jls/se10/html/jls-5.html#jls-5.1.3 The JLS guarantees this is the same: > A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value. PiperOrigin-RevId: 683416604 --- .../google/protobuf/CodedOutputStream.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 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 92765b9b4a..6896f71195 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -1414,14 +1414,14 @@ public abstract class CodedOutputStream extends ByteOutput { 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); - buffer[position + 1] = (byte) ((int) (value >> 8)); - buffer[position + 2] = (byte) ((int) (value >> 16)); - buffer[position + 3] = (byte) ((int) (value >> 24)); - buffer[position + 4] = (byte) ((int) (value >> 32)); - buffer[position + 5] = (byte) ((int) (value >> 40)); - buffer[position + 6] = (byte) ((int) (value >> 48)); - buffer[position + 7] = (byte) ((int) (value >> 56)); + buffer[position] = (byte) value; + buffer[position + 1] = (byte) (value >> 8); + buffer[position + 2] = (byte) (value >> 16); + buffer[position + 3] = (byte) (value >> 24); + buffer[position + 4] = (byte) (value >> 32); + buffer[position + 5] = (byte) (value >> 40); + buffer[position + 6] = (byte) (value >> 48); + buffer[position + 7] = (byte) (value >> 56); } catch (IndexOutOfBoundsException e) { throw new OutOfSpaceException(position, limit, FIXED64_SIZE, e); } @@ -2384,10 +2384,10 @@ public abstract class CodedOutputStream extends ByteOutput { buffer[position++] = (byte) (value >> 8); buffer[position++] = (byte) (value >> 16); buffer[position++] = (byte) (value >> 24); - buffer[position++] = (byte) ((int) (value >> 32)); - buffer[position++] = (byte) ((int) (value >> 40)); - buffer[position++] = (byte) ((int) (value >> 48)); - buffer[position++] = (byte) ((int) (value >> 56)); + buffer[position++] = (byte) (value >> 32); + buffer[position++] = (byte) (value >> 40); + buffer[position++] = (byte) (value >> 48); + buffer[position++] = (byte) (value >> 56); this.position = position; totalBytesWritten += FIXED64_SIZE; }