From 101ad18f71d350f730acfb0bc39688723175bc20 Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Fri, 10 Jun 2022 16:58:23 +0200 Subject: [PATCH] Minor optimizations. (cherry picked from commit 2b04f7205ccec9f43c9c58e9471709f9f620f5b6) --- .../io/FastByteArrayOutputStream.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/common/common-io/src/main/java/com/twelvemonkeys/io/FastByteArrayOutputStream.java b/common/common-io/src/main/java/com/twelvemonkeys/io/FastByteArrayOutputStream.java index 87b8fade..a23f0812 100755 --- a/common/common-io/src/main/java/com/twelvemonkeys/io/FastByteArrayOutputStream.java +++ b/common/common-io/src/main/java/com/twelvemonkeys/io/FastByteArrayOutputStream.java @@ -34,6 +34,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; /** * An unsynchronized {@code ByteArrayOutputStream} implementation. This version @@ -42,11 +43,8 @@ import java.io.OutputStream; * @author Harald Kuhr * @version $Id: FastByteArrayOutputStream.java#2 $ */ -// TODO: Performance test of a stream impl that uses list of fixed size blocks, rather than contiguous block +// TODO: Performance test of a stream impl that uses list of fixed size blocks, rather than contiguous block public final class FastByteArrayOutputStream extends ByteArrayOutputStream { - /** Max grow size (unless if writing more than this amount of bytes) */ - protected int maxGrowSize = 1024 * 1024; // 1 MB - /** * Creates a {@code ByteArrayOutputStream} with the given initial buffer * size. @@ -97,10 +95,8 @@ public final class FastByteArrayOutputStream extends ByteArrayOutputStream { private void growIfNeeded(int pNewCount) { if (pNewCount > buf.length) { - int newSize = Math.max(Math.min(buf.length << 1, buf.length + maxGrowSize), pNewCount); - byte[] newBuf = new byte[newSize]; - System.arraycopy(buf, 0, newBuf, 0, count); - buf = newBuf; + int newSize = Math.max(buf.length << 1, pNewCount); + buf = Arrays.copyOf(buf, newSize); } } @@ -113,10 +109,7 @@ public final class FastByteArrayOutputStream extends ByteArrayOutputStream { // Non-synchronized version of toByteArray @Override public byte[] toByteArray() { - byte[] newBuf = new byte[count]; - System.arraycopy(buf, 0, newBuf, 0, count); - - return newBuf; + return Arrays.copyOf(buf, count); } /**