From 8e5e2a11f069935644ff5404ffd8758773834f29 Mon Sep 17 00:00:00 2001 From: Nico Kruber Date: Fri, 25 Jan 2013 15:16:52 +0100 Subject: jinterface: don't compress small erlang terms < 5 bytes Compression always has at least 5 bytes (the compressed tag + original size) so we can't get a smaller external term if the original term is already smaller than 5 bytes. --- .../com/ericsson/otp/erlang/OtpOutputStream.java | 38 ++++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'lib/jinterface') 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 85100c1911..8c3f48968e 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java @@ -789,16 +789,34 @@ public class OtpOutputStream extends ByteArrayOutputStream { */ public void write_compressed(final OtpErlangObject o) { final OtpOutputStream oos = new OtpOutputStream(o); - write1(OtpExternal.compressedTag); - write4BE(oos.size()); - final java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream( - this); - try { - oos.writeTo(dos); - dos.close(); - } catch (final IOException e) { - throw new java.lang.IllegalArgumentException( - "Intermediate stream failed for Erlang object " + o); + /* + * similar to erts_term_to_binary() in external.c: + * We don't want to compress if compression actually increases the size. + * Since compression uses 5 extra bytes (COMPRESSED tag + size), don't + * compress if the original term is smaller. + */ + if (oos.size() < 5) { + try { + oos.writeTo(this); + // if the term is written as a compressed term, the output + // stream is closed, so we do this here, too + this.close(); + } catch (IOException e) { + throw new java.lang.IllegalArgumentException( + "Intermediate stream failed for Erlang object " + o); + } + } else { + write1(OtpExternal.compressedTag); + write4BE(oos.size()); + final java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream( + this); + try { + oos.writeTo(dos); + dos.close(); // note: closes this, too! + } catch (final IOException e) { + throw new java.lang.IllegalArgumentException( + "Intermediate stream failed for Erlang object " + o); + } } } -- cgit v1.2.3