diff options
author | Nico Kruber <[email protected]> | 2013-01-28 19:43:01 +0100 |
---|---|---|
committer | Fredrik Gustafsson <[email protected]> | 2013-02-04 09:52:34 +0100 |
commit | bae8d6a4f357f8c9be0661e80a9d163fba56ee7c (patch) | |
tree | 479fdc435b2a50d9a930b86643b76441ded16c92 | |
parent | 9239bca48cad7653b43f89f71c49b3d0be682c51 (diff) | |
download | otp-bae8d6a4f357f8c9be0661e80a9d163fba56ee7c.tar.gz otp-bae8d6a4f357f8c9be0661e80a9d163fba56ee7c.tar.bz2 otp-bae8d6a4f357f8c9be0661e80a9d163fba56ee7c.zip |
jinterface: new limited OutputStream implementation without the need to resize
(saves memory re-allocations)
-rw-r--r-- | lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java | 14 |
1 files changed, 7 insertions, 7 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 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; } } } |