diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java index 387b29e9..a223754a 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.Reader; import java.util.Iterator; import java.util.Map; +import java.util.Arrays; /** * This reader walks the elements of a JsonElement as if it was coming from a @@ -282,15 +283,10 @@ public final class JsonTreeReader extends JsonReader { private void push(Object newTop) { if (stackSize == stack.length) { - Object[] newStack = new Object[stackSize * 2]; - int[] newPathIndices = new int[stackSize * 2]; - String[] newPathNames = new String[stackSize * 2]; - System.arraycopy(stack, 0, newStack, 0, stackSize); - System.arraycopy(pathIndices, 0, newPathIndices, 0, stackSize); - System.arraycopy(pathNames, 0, newPathNames, 0, stackSize); - stack = newStack; - pathIndices = newPathIndices; - pathNames = newPathNames; + int newLength = stackSize * 2; + stack = Arrays.copyOf(stack, newLength); + pathIndices = Arrays.copyOf(pathIndices, newLength); + pathNames = Arrays.copyOf(pathNames, newLength); } stack[stackSize++] = newTop; } diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index e0799d73..1e279386 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -22,6 +22,7 @@ import java.io.Closeable; import java.io.EOFException; import java.io.IOException; import java.io.Reader; +import java.util.Arrays; /** * Reads a JSON (RFC 7159) @@ -1262,15 +1263,10 @@ public class JsonReader implements Closeable { private void push(int newTop) { if (stackSize == stack.length) { - int[] newStack = new int[stackSize * 2]; - int[] newPathIndices = new int[stackSize * 2]; - String[] newPathNames = new String[stackSize * 2]; - System.arraycopy(stack, 0, newStack, 0, stackSize); - System.arraycopy(pathIndices, 0, newPathIndices, 0, stackSize); - System.arraycopy(pathNames, 0, newPathNames, 0, stackSize); - stack = newStack; - pathIndices = newPathIndices; - pathNames = newPathNames; + int newLength = stackSize * 2; + stack = Arrays.copyOf(stack, newLength); + pathIndices = Arrays.copyOf(pathIndices, newLength); + pathNames = Arrays.copyOf(pathNames, newLength); } stack[stackSize++] = newTop; } diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index 8148816c..597bd569 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -20,6 +20,7 @@ import java.io.Closeable; import java.io.Flushable; import java.io.IOException; import java.io.Writer; +import java.util.Arrays; import static com.google.gson.stream.JsonScope.DANGLING_NAME; import static com.google.gson.stream.JsonScope.EMPTY_ARRAY; @@ -352,9 +353,7 @@ public class JsonWriter implements Closeable, Flushable { private void push(int newTop) { if (stackSize == stack.length) { - int[] newStack = new int[stackSize * 2]; - System.arraycopy(stack, 0, newStack, 0, stackSize); - stack = newStack; + stack = Arrays.copyOf(stack, stackSize * 2); } stack[stackSize++] = newTop; }