From 935d099ad9f6692e727406764bb3226d935e899d Mon Sep 17 00:00:00 2001 From: David Mollitor Date: Wed, 27 Oct 2021 11:08:33 -0400 Subject: [PATCH] Standardize on Array copyOf --- .../java/com/google/protobuf/BooleanArrayList.java | 13 +++---------- .../java/com/google/protobuf/DoubleArrayList.java | 13 +++---------- .../java/com/google/protobuf/FloatArrayList.java | 13 +++---------- .../main/java/com/google/protobuf/IntArrayList.java | 13 +++---------- .../java/com/google/protobuf/LongArrayList.java | 13 +++---------- .../java/com/google/protobuf/ProtobufArrayList.java | 4 +--- 6 files changed, 16 insertions(+), 53 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java index 451fce1e84..35c9240780 100644 --- a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java @@ -197,10 +197,7 @@ final class BooleanArrayList extends AbstractProtobufList if (size == array.length) { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - boolean[] newArray = new boolean[length]; - - System.arraycopy(array, 0, newArray, 0, size); - array = newArray; + array = Arrays.copyOf(array, length); } array[size++] = element; @@ -219,14 +216,10 @@ final class BooleanArrayList extends AbstractProtobufList } else { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - boolean[] newArray = new boolean[length]; - - // Copy the first part directly - System.arraycopy(array, 0, newArray, 0, index); + array = Arrays.copyOf(array, length); // Copy the rest shifted over by one to make room - System.arraycopy(array, index, newArray, index + 1, size - index); - array = newArray; + System.arraycopy(array, index, array, index + 1, size - index); } array[index] = element; diff --git a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java index 4085653437..ec4a416781 100644 --- a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java @@ -197,10 +197,7 @@ final class DoubleArrayList extends AbstractProtobufList if (size == array.length) { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - double[] newArray = new double[length]; - - System.arraycopy(array, 0, newArray, 0, size); - array = newArray; + array = Arrays.copyOf(array, length); } array[size++] = element; @@ -219,14 +216,10 @@ final class DoubleArrayList extends AbstractProtobufList } else { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - double[] newArray = new double[length]; - - // Copy the first part directly - System.arraycopy(array, 0, newArray, 0, index); + array = Arrays.copyOf(array, length); // Copy the rest shifted over by one to make room - System.arraycopy(array, index, newArray, index + 1, size - index); - array = newArray; + System.arraycopy(array, index, array, index + 1, size - index); } array[index] = element; diff --git a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java index e6feba8a35..4d42b7c495 100644 --- a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java @@ -196,10 +196,7 @@ final class FloatArrayList extends AbstractProtobufList if (size == array.length) { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - float[] newArray = new float[length]; - - System.arraycopy(array, 0, newArray, 0, size); - array = newArray; + array = Arrays.copyOf(array, length); } array[size++] = element; @@ -218,14 +215,10 @@ final class FloatArrayList extends AbstractProtobufList } else { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - float[] newArray = new float[length]; - - // Copy the first part directly - System.arraycopy(array, 0, newArray, 0, index); + array = Arrays.copyOf(array, length); // Copy the rest shifted over by one to make room - System.arraycopy(array, index, newArray, index + 1, size - index); - array = newArray; + System.arraycopy(array, index, array, index + 1, size - index); } array[index] = element; diff --git a/java/core/src/main/java/com/google/protobuf/IntArrayList.java b/java/core/src/main/java/com/google/protobuf/IntArrayList.java index 9daeebed99..5392e0e408 100644 --- a/java/core/src/main/java/com/google/protobuf/IntArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/IntArrayList.java @@ -196,10 +196,7 @@ final class IntArrayList extends AbstractProtobufList if (size == array.length) { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - int[] newArray = new int[length]; - - System.arraycopy(array, 0, newArray, 0, size); - array = newArray; + array = Arrays.copyOf(array, length); } array[size++] = element; @@ -218,14 +215,10 @@ final class IntArrayList extends AbstractProtobufList } else { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - int[] newArray = new int[length]; - - // Copy the first part directly - System.arraycopy(array, 0, newArray, 0, index); + array = Arrays.copyOf(array, length); // Copy the rest shifted over by one to make room - System.arraycopy(array, index, newArray, index + 1, size - index); - array = newArray; + System.arraycopy(array, index, array, index + 1, size - index); } array[index] = element; diff --git a/java/core/src/main/java/com/google/protobuf/LongArrayList.java b/java/core/src/main/java/com/google/protobuf/LongArrayList.java index bda43a41bb..c971b5a995 100644 --- a/java/core/src/main/java/com/google/protobuf/LongArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/LongArrayList.java @@ -196,10 +196,7 @@ final class LongArrayList extends AbstractProtobufList if (size == array.length) { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - long[] newArray = new long[length]; - - System.arraycopy(array, 0, newArray, 0, size); - array = newArray; + array = Arrays.copyOf(array, length); } array[size++] = element; @@ -218,14 +215,10 @@ final class LongArrayList extends AbstractProtobufList } else { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - long[] newArray = new long[length]; - - // Copy the first part directly - System.arraycopy(array, 0, newArray, 0, index); + array = Arrays.copyOf(array, length); // Copy the rest shifted over by one to make room - System.arraycopy(array, index, newArray, index + 1, size - index); - array = newArray; + System.arraycopy(array, index, array, index + 1, size - index); } array[index] = element; diff --git a/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java b/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java index 33e4bd5e35..fd82233fcf 100644 --- a/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java @@ -80,9 +80,7 @@ final class ProtobufArrayList extends AbstractProtobufList implements Rand if (size == array.length) { // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; - E[] newArray = Arrays.copyOf(array, length); - - array = newArray; + array = Arrays.copyOf(array, length); } array[size++] = element;