From bae8d6a4f357f8c9be0661e80a9d163fba56ee7c Mon Sep 17 00:00:00 2001 From: Nico Kruber Date: Mon, 28 Jan 2013 19:43:01 +0100 Subject: jinterface: new limited OutputStream implementation without the need to resize (saves memory re-allocations) --- .../java_src/com/ericsson/otp/erlang/OtpOutputStream.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'lib/jinterface/java_src') 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 541df20369..1512f9f2aa 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java @@ -48,7 +48,7 @@ public class OtpOutputStream extends ByteArrayOutputStream { private static final BigDecimal ten = new BigDecimal(10.0); private static final BigDecimal one = new BigDecimal(1.0); - private boolean fixedSize = false; + private int fixedSize = Integer.MAX_VALUE; /** * Create a stream with the default initial size (2048 bytes). @@ -124,16 +124,17 @@ public class OtpOutputStream extends ByteArrayOutputStream { * @param minCapacity the desired minimum capacity */ public void ensureCapacity(int minCapacity) { + if (minCapacity > fixedSize) { + throw new IllegalArgumentException("Trying to increase fixed-size buffer"); + } int oldCapacity = super.buf.length; if (minCapacity > oldCapacity) { - if (fixedSize) { - throw new IllegalArgumentException("Trying to increase fixed-size buffer"); - } int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < oldCapacity + defaultIncrement) newCapacity = oldCapacity + defaultIncrement; if (newCapacity < minCapacity) newCapacity = minCapacity; + newCapacity = Math.min(fixedSize, newCapacity); // 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); @@ -822,8 +823,7 @@ public class OtpOutputStream extends ByteArrayOutputStream { // we need destCount bytes for an uncompressed term // -> if compression uses more, use the uncompressed term! int destCount = startCount + oos.size(); - this.resize(destCount); - this.fixedSize = true; + this.fixedSize = destCount; final java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream( this); try { @@ -848,7 +848,7 @@ public class OtpOutputStream extends ByteArrayOutputStream { throw new java.lang.IllegalArgumentException( "Intermediate stream failed for Erlang object " + o); } finally { - this.fixedSize = false; + this.fixedSize = Integer.MAX_VALUE; } } } -- cgit v1.2.3