This is an automated email from the ASF dual-hosted git repository. jking pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/thrift.git
The following commit(s) were added to refs/heads/master by this push: new 9e813ae THRIFT-4702: Improve AutoExpandingBuffer 9e813ae is described below commit 9e813aeed2291a25fc965ba58944c3705b9d15c6 Author: Beluga Behr <dam6...@gmail.com> AuthorDate: Mon Dec 31 10:58:19 2018 -0500 THRIFT-4702: Improve AutoExpandingBuffer --- lib/java/README.md | 9 ++++++++ .../thrift/transport/AutoExpandingBuffer.java | 26 ++++++++++------------ .../AutoExpandingBufferReadTransport.java | 4 ++-- .../AutoExpandingBufferWriteTransport.java | 4 ++-- .../thrift/transport/TFastFramedTransport.java | 6 ++--- .../thrift/transport/TestAutoExpandingBuffer.java | 2 +- .../TestAutoExpandingBufferReadTransport.java | 2 +- .../TestAutoExpandingBufferWriteTransport.java | 2 +- 8 files changed, 31 insertions(+), 24 deletions(-) diff --git a/lib/java/README.md b/lib/java/README.md index 1e4aed2..cdd4059 100644 --- a/lib/java/README.md +++ b/lib/java/README.md @@ -161,3 +161,12 @@ Dependencies Gradle http://gradle.org/ + +# Breaking Changes + +## 0.12.0 + +The access modifier of the AutoExpandingBuffer class has been changed from +public to default (package) and will no longer be accessible by third-party +libraries. + diff --git a/lib/java/src/org/apache/thrift/transport/AutoExpandingBuffer.java b/lib/java/src/org/apache/thrift/transport/AutoExpandingBuffer.java index b02905f..fc3aa92 100644 --- a/lib/java/src/org/apache/thrift/transport/AutoExpandingBuffer.java +++ b/lib/java/src/org/apache/thrift/transport/AutoExpandingBuffer.java @@ -18,6 +18,8 @@ */ package org.apache.thrift.transport; +import java.util.Arrays; + /** * Helper class that wraps a byte[] so that it can expand and be reused. Users * should call resizeIfNecessary to make sure the buffer has suitable capacity, @@ -25,28 +27,24 @@ package org.apache.thrift.transport; * rate slightly faster than the requested capacity with the (untested) * objective of avoiding expensive buffer allocations and copies. */ -public class AutoExpandingBuffer { +class AutoExpandingBuffer { private byte[] array; - private final double growthCoefficient; - - public AutoExpandingBuffer(int initialCapacity, double growthCoefficient) { - if (growthCoefficient < 1.0) { - throw new IllegalArgumentException("Growth coefficient must be >= 1.0"); - } - array = new byte[initialCapacity]; - this.growthCoefficient = growthCoefficient; + public AutoExpandingBuffer(int initialCapacity) { + this.array = new byte[initialCapacity]; } public void resizeIfNecessary(int size) { - if (array.length < size) { - byte[] newBuf = new byte[(int)(size * growthCoefficient)]; - System.arraycopy(array, 0, newBuf, 0, array.length); - array = newBuf; + final int currentCapacity = this.array.length; + if (currentCapacity < size) { + // Increase by a factor of 1.5x + int growCapacity = currentCapacity + (currentCapacity >> 1); + int newCapacity = Math.max(growCapacity, size); + this.array = Arrays.copyOf(array, newCapacity); } } public byte[] array() { - return array; + return this.array; } } diff --git a/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferReadTransport.java b/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferReadTransport.java index 081bc48..a28d254 100644 --- a/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferReadTransport.java +++ b/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferReadTransport.java @@ -28,8 +28,8 @@ public class AutoExpandingBufferReadTransport extends TTransport { private int pos = 0; private int limit = 0; - public AutoExpandingBufferReadTransport(int initialCapacity, double overgrowthCoefficient) { - this.buf = new AutoExpandingBuffer(initialCapacity, overgrowthCoefficient); + public AutoExpandingBufferReadTransport(int initialCapacity) { + this.buf = new AutoExpandingBuffer(initialCapacity); } public void fill(TTransport inTrans, int length) throws TTransportException { diff --git a/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferWriteTransport.java b/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferWriteTransport.java index 9b35693..ad2ec55 100644 --- a/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferWriteTransport.java +++ b/lib/java/src/org/apache/thrift/transport/AutoExpandingBufferWriteTransport.java @@ -26,8 +26,8 @@ public final class AutoExpandingBufferWriteTransport extends TTransport { private final AutoExpandingBuffer buf; private int pos; - public AutoExpandingBufferWriteTransport(int initialCapacity, double growthCoefficient) { - this.buf = new AutoExpandingBuffer(initialCapacity, growthCoefficient); + public AutoExpandingBufferWriteTransport(int initialCapacity) { + this.buf = new AutoExpandingBuffer(initialCapacity); this.pos = 0; } diff --git a/lib/java/src/org/apache/thrift/transport/TFastFramedTransport.java b/lib/java/src/org/apache/thrift/transport/TFastFramedTransport.java index d265600..891d798 100644 --- a/lib/java/src/org/apache/thrift/transport/TFastFramedTransport.java +++ b/lib/java/src/org/apache/thrift/transport/TFastFramedTransport.java @@ -106,8 +106,8 @@ public class TFastFramedTransport extends TTransport { this.underlying = underlying; this.maxLength = maxLength; this.initialBufferCapacity = initialBufferCapacity; - writeBuffer = new AutoExpandingBufferWriteTransport(initialBufferCapacity, 1.5); - readBuffer = new AutoExpandingBufferReadTransport(initialBufferCapacity, 1.5); + writeBuffer = new AutoExpandingBufferWriteTransport(initialBufferCapacity); + readBuffer = new AutoExpandingBufferReadTransport(initialBufferCapacity); } @Override @@ -167,7 +167,7 @@ public class TFastFramedTransport extends TTransport { } public void clear() { - readBuffer = new AutoExpandingBufferReadTransport(initialBufferCapacity, 1.5); + readBuffer = new AutoExpandingBufferReadTransport(initialBufferCapacity); } @Override diff --git a/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBuffer.java b/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBuffer.java index 337dcf8..c353489 100644 --- a/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBuffer.java +++ b/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBuffer.java @@ -23,7 +23,7 @@ import junit.framework.TestCase; public class TestAutoExpandingBuffer extends TestCase { public void testExpands() throws Exception { // has expected initial capacity - AutoExpandingBuffer b = new AutoExpandingBuffer(10, 1.5); + AutoExpandingBuffer b = new AutoExpandingBuffer(10); assertEquals(10, b.array().length); // doesn't shrink diff --git a/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferReadTransport.java b/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferReadTransport.java index 2e1f947..83ebc2d 100644 --- a/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferReadTransport.java +++ b/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferReadTransport.java @@ -32,7 +32,7 @@ public class TestAutoExpandingBufferReadTransport extends TestCase { } public void testIt() throws Exception { - AutoExpandingBufferReadTransport t = new AutoExpandingBufferReadTransport(150, 1.5); + AutoExpandingBufferReadTransport t = new AutoExpandingBufferReadTransport(150); TMemoryInputTransport membuf = new TMemoryInputTransport(HUNDRED_BYTES); diff --git a/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferWriteTransport.java b/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferWriteTransport.java index d5f239d..6b04feb 100644 --- a/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferWriteTransport.java +++ b/lib/java/test/org/apache/thrift/transport/TestAutoExpandingBufferWriteTransport.java @@ -25,7 +25,7 @@ import junit.framework.TestCase; public class TestAutoExpandingBufferWriteTransport extends TestCase { public void testIt() throws Exception { - AutoExpandingBufferWriteTransport t = new AutoExpandingBufferWriteTransport(1, 1.5); + AutoExpandingBufferWriteTransport t = new AutoExpandingBufferWriteTransport(1); assertEquals(1, t.getBuf().array().length); byte[] b1 = new byte[]{1,2,3}; t.write(b1);