diff options
author | Nico Kruber <[email protected]> | 2013-01-25 15:16:52 +0100 |
---|---|---|
committer | Fredrik Gustafsson <[email protected]> | 2013-02-04 09:52:34 +0100 |
commit | 8e5e2a11f069935644ff5404ffd8758773834f29 (patch) | |
tree | 4a1ed04bc6b7c09753852108d591630e7f6763e7 /lib/jinterface | |
parent | 9061aa49cf578c3b79b79ed8564cc061847d2a73 (diff) | |
download | otp-8e5e2a11f069935644ff5404ffd8758773834f29.tar.gz otp-8e5e2a11f069935644ff5404ffd8758773834f29.tar.bz2 otp-8e5e2a11f069935644ff5404ffd8758773834f29.zip |
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.
Diffstat (limited to 'lib/jinterface')
-rw-r--r-- | lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java | 38 |
1 files changed, 28 insertions, 10 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 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); + } } } |