aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Nord <[email protected]>2011-12-09 12:43:53 +0100
committerHenrik Nord <[email protected]>2011-12-09 12:44:08 +0100
commitc20450e213fccaf38e39f9416d619515c50870ed (patch)
treef046b8453a7c4a918fb9b407e72419182682fddb
parent7bd353a976a0bf4d93b962376daa0d38958335d0 (diff)
parent607ba455098a4750f830a5f29f9739591108647e (diff)
downloadotp-c20450e213fccaf38e39f9416d619515c50870ed.tar.gz
otp-c20450e213fccaf38e39f9416d619515c50870ed.tar.bz2
otp-c20450e213fccaf38e39f9416d619515c50870ed.zip
Merge branch 'nk/jinterface_better_buffer_alloc'
* nk/jinterface_better_buffer_alloc: restore Java5 compatibility JInterface: improve OtpOutputStream buffer allocation OTP-9806
-rw-r--r--lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java55
1 files changed, 39 insertions, 16 deletions
diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java
index 181350100f..ab7642d49d 100644
--- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java
+++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java
@@ -25,6 +25,7 @@ import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
+import java.util.Arrays;
/**
* Provides a stream for encoding Erlang terms to external format, for
@@ -39,7 +40,7 @@ public class OtpOutputStream extends ByteArrayOutputStream {
/** The default initial size of the stream. * */
public static final int defaultInitialSize = 2048;
- /** The default increment used when growing the stream. * */
+ /** The default increment used when growing the stream (increment at least this much). * */
public static final int defaultIncrement = 2048;
// static formats, used to encode floats and doubles
@@ -95,6 +96,41 @@ public class OtpOutputStream extends ByteArrayOutputStream {
}
/**
+ * Trims the capacity of this <tt>OtpOutputStream</tt> instance to be the
+ * buffer's current size. An application can use this operation to minimize
+ * the storage of an <tt>OtpOutputStream</tt> instance.
+ */
+ public void trimToSize() {
+ if (super.count < super.buf.length) {
+ final byte[] tmp = new byte[super.count];
+ System.arraycopy(super.buf, 0, tmp, 0, super.count);
+ super.buf = tmp;
+ }
+ }
+
+ /**
+ * Increases the capacity of this <tt>OtpOutputStream</tt> instance, if
+ * necessary, to ensure that it can hold at least the number of elements
+ * specified by the minimum capacity argument.
+ *
+ * @param minCapacity the desired minimum capacity
+ */
+ public void ensureCapacity(int minCapacity) {
+ int oldCapacity = super.buf.length;
+ if (minCapacity > oldCapacity) {
+ int newCapacity = (oldCapacity * 3)/2 + 1;
+ if (newCapacity < oldCapacity + defaultIncrement)
+ newCapacity = oldCapacity + defaultIncrement;
+ if (newCapacity < minCapacity)
+ newCapacity = minCapacity;
+ // minCapacity is usually close to size, so this is a win:
+ final byte[] tmp = new byte[newCapacity];
+ System.arraycopy(super.buf, 0, tmp, 0, super.count);
+ super.buf = tmp;
+ }
+ }
+
+ /**
* Write one byte to the stream.
*
* @param b
@@ -102,13 +138,7 @@ public class OtpOutputStream extends ByteArrayOutputStream {
*
*/
public void write(final byte b) {
- if (super.count >= super.buf.length) {
- // System.err.println("Expanding buffer from " + this.buf.length
- // + " to " + (this.buf.length+defaultIncrement));
- final byte[] tmp = new byte[super.buf.length + defaultIncrement];
- System.arraycopy(super.buf, 0, tmp, 0, super.count);
- super.buf = tmp;
- }
+ ensureCapacity(super.count + 1);
super.buf[super.count++] = b;
}
@@ -122,14 +152,7 @@ public class OtpOutputStream extends ByteArrayOutputStream {
@Override
public void write(final byte[] buf) {
- if (super.count + buf.length > super.buf.length) {
- // System.err.println("Expanding buffer from " + super.buf.length
- // + " to " + (buf.length + super.buf.lengt + defaultIncrement));
- final byte[] tmp = new byte[super.buf.length + buf.length
- + defaultIncrement];
- System.arraycopy(super.buf, 0, tmp, 0, super.count);
- super.buf = tmp;
- }
+ ensureCapacity(super.count + buf.length);
System.arraycopy(buf, 0, super.buf, super.count, buf.length);
super.count += buf.length;
}