From ebac02fad272b2f7ba56ce61ea0cc91fa3c2658f Mon Sep 17 00:00:00 2001 From: zweibieren Date: Tue, 9 Jan 2018 13:46:00 -0500 Subject: [PATCH 1/6] Update UncompressInputStream.java Fixed bugs where final bytes of file were not decoded in available() and the EOF condition for mainloop. Added some comments Indented some unindented lines Added method uncompress(InputStream,OutputStream) and called it from main(String[]) and from uncompress(String, FileOutputStream) Rewrote skip method Amended logging code in uncompress(String, FileOutputStream) --- .../nbio/core/util/UncompressInputStream.java | 171 +++++++++++------- 1 file changed, 101 insertions(+), 70 deletions(-) diff --git a/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java b/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java index a14d40ee53..7dc17885c4 100644 --- a/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java +++ b/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java @@ -75,10 +75,24 @@ * @author Ronald Tschalar * @author Unidata Program Center * @author Richard Holland - making LZW_MAGIC package-visible. + * + * @version 0.3-5 2008/01/19 + * @author Fred Hansen (zweibieren@yahoo.com) + * Fixed available() and the EOF condition for mainloop. + * Also added some comments. + * + * @version 1.0 2018/01/08 + * @author Fred Hansen (zweibieren@yahoo.com) + * added uncompress(InputStream,OutputStream) + * and called it from main(String[]) + * and uncompress(String, FileOutputStream) + * normalize indentation + * rewrite skip method + * amend logging code in uncompress(String, FileOutputStream) */ public class UncompressInputStream extends FilterInputStream { - - private final static Logger logger = LoggerFactory.getLogger(UncompressInputStream.class); + private final static Logger logger + = LoggerFactory.getLogger(UncompressInputStream.class); /** * @param is the input stream to decompress @@ -90,10 +104,9 @@ public UncompressInputStream(InputStream is) throws IOException { } - byte[] one = new byte[1]; - @Override -public synchronized int read() throws IOException { + public synchronized int read() throws IOException { + byte[] one = new byte[1]; int b = read(one, 0, 1); if (b == 1) return (one[0] & 0xff); @@ -108,7 +121,7 @@ public synchronized int read() throws IOException { private int[] tab_prefix; private byte[] tab_suffix; - private int[] zeros = new int[256]; + final private int[] zeros = new int[256]; private byte[] stack; // various state @@ -123,20 +136,27 @@ public synchronized int read() throws IOException { private int stackp; private int free_ent; - // input buffer - private byte[] data = new byte[10000]; - private int bit_pos = 0, end = 0, got = 0; + /* input buffer + The input stream must be considered in chunks + Each chunk is of length eight times the current code length. + Thus the chunk contains eight codes; NOT on byte boundaries. + */ + final private byte[] data = new byte[10000]; + private int + bit_pos = 0, // current bitwise location in bitstream + end = 0, // index of next byte to fill in data + got = 0; // number of bytes gotten by most recent read() private boolean eof = false; private static final int EXTRA = 64; @Override -public synchronized int read(byte[] buf, int off, int len) + public synchronized int read(byte[] buf, int off, int len) throws IOException { if (eof) return -1; int start = off; -/* Using local copies of various variables speeds things up by as + /* Using local copies of various variables speeds things up by as * much as 30% ! */ int[] l_tab_prefix = tab_prefix; @@ -153,9 +173,7 @@ public synchronized int read(byte[] buf, int off, int len) byte[] l_data = data; int l_bit_pos = bit_pos; - -// empty stack if stuff still left - + // empty stack if stuff still left int s_size = l_stack.length - l_stackp; if (s_size > 0) { int num = (s_size >= len) ? len : s_size; @@ -170,17 +188,15 @@ public synchronized int read(byte[] buf, int off, int len) return off - start; } - -// loop, filling local buffer until enough data has been decompressed - + // loop, filling local buffer until enough data has been decompressed main_loop: do { if (end < EXTRA) fill(); - int bit_in = (got > 0) ? (end - end % l_n_bits) << 3 : - (end << 3) - (l_n_bits - 1); + int bit_end = (got > 0) + ? (end - end % l_n_bits) << 3 // set to a "chunk" boundary + : (end << 3) - (l_n_bits - 1); // no more data, set to last code - while (l_bit_pos < bit_in) { - // handle 1-byte reads correctly + while (l_bit_pos < bit_end) { // handle 1-byte reads correctly if (len == 0) { n_bits = l_n_bits; maxcode = l_maxcode; @@ -326,7 +342,10 @@ public synchronized int read(byte[] buf, int off, int len) } l_bit_pos = resetbuf(l_bit_pos); - } while (got > 0); + } while + // old code: (got>0) fails if code width expands near EOF + (got > 0 // usually true + || l_bit_pos < (end << 3) - (l_n_bits - 1)); // last few bytes n_bits = l_n_bits; maxcode = l_maxcode; @@ -346,7 +365,7 @@ public synchronized int read(byte[] buf, int off, int len) * Moves the unread data in the buffer to the beginning and resets * the pointers. */ - private final int resetbuf(int bit_pos) { + private int resetbuf(int bit_pos) { int pos = bit_pos >> 3; System.arraycopy(data, pos, data, 0, end - pos); end -= pos; @@ -354,29 +373,28 @@ private final int resetbuf(int bit_pos) { } - private final void fill() throws IOException { + private void fill() throws IOException { got = in.read(data, end, data.length - 1 - end); if (got > 0) end += got; } @Override -public synchronized long skip(long num) throws IOException { - byte[] tmp = new byte[(int) num]; - int got = read(tmp, 0, (int) num); - - if (got > 0) - return got; - else - return 0L; + public synchronized long skip(long num) throws IOException { + return Math.max(0, + read(new byte[(int) num], 0, (int) num)); } @Override -public synchronized int available() throws IOException { + public synchronized int available() throws IOException { if (eof) return 0; - - return in.available(); + // the old code was: return in.available(); + // it fails because this.read() can return bytes + // even after in.available() is zero + // -- zweibieren + int avail = in.available(); + return (avail == 0) ? 1 : avail; } @@ -389,8 +407,7 @@ public synchronized int available() throws IOException { private static final int HDR_BLOCK_MODE = 0x80; private void parse_header() throws IOException { -// read in and check magic number - + // read in and check magic number int t = in.read(); if (t < 0) throw new EOFException("Failed to read magic number"); int magic = (t & 0xff) << 8; @@ -402,9 +419,7 @@ private void parse_header() throws IOException { "magic number 0x" + Integer.toHexString(magic) + ")"); - -// read in header byte - + // read in header byte int header = in.read(); if (header < 0) throw new EOFException("Failed to read header"); @@ -425,9 +440,7 @@ private void parse_header() throws IOException { logger.debug("block mode: {}", block_mode); logger.debug("max bits: {}", maxbits); - -// initialize stuff - + // initialize stuff maxmaxcode = 1 << maxbits; n_bits = INIT_BITS; maxcode = (1 << n_bits) - 1; @@ -451,59 +464,77 @@ private void parse_header() throws IOException { * @return false */ @Override -public boolean markSupported() { + public boolean markSupported() { return false; } - static public void uncompress( String fileInName, FileOutputStream out) throws IOException { + /** + * Read a named file and uncompress it. + * @param fileInName Name of compressed file. + * @param out A destination for the result. It is closed after data is sent. + * @return number of bytes sent to the output stream, + * @throws IOException for any error + */ + public static long uncompress(String fileInName, FileOutputStream out) + throws IOException { long start = System.currentTimeMillis(); - - InputStream in = new UncompressInputStream( new FileInputStream(fileInName)); - - // int total = 0; - byte[] buffer = new byte[100000]; - while (true) { - int bytesRead = in.read(buffer); - if (bytesRead == -1) break; - out.write(buffer, 0, bytesRead); - // total += bytesRead; + long total; + try (InputStream fin = new FileInputStream(fileInName)) { + total = uncompress(fin, out); } - in.close(); out.close(); if (debugTiming) { long end = System.currentTimeMillis(); - // logger.debug("Decompressed " + total + " bytes"); - logger.warn("Time: {} seconds", (end - start) / 1000); + logger.info("Decompressed {} bytes", total); + UncompressInputStream.logger.info("Time: {} seconds", (end - start) / 1000); } + return total; } + /** + * Read an input stream and uncompress it to an output stream. + * @param in the incoming InputStream. It is NOT closed. + * @param out the destination OutputStream. It is NOT closed. + * @return number of bytes sent to the output stream + * @throws IOException for any error + */ + public static long uncompress(InputStream in, OutputStream out) + throws IOException { + UncompressInputStream ucis = new UncompressInputStream(in); + long total = 0; + byte[] buffer = new byte[100000]; + while (true) { + int bytesRead = ucis.read(buffer); + if (bytesRead == -1) break; + out.write(buffer, 0, bytesRead); + total += bytesRead; + } + return total; + } private static final boolean debugTiming = false; + /** + * Reads a file, uncompresses it, and sends the result to stdout. + * Also writes trivial statistics to stderr. + * @param args An array with one String element, the name of the file to read. + * @throws IOException for any failure + */ public static void main(String[] args) throws Exception { if (args.length != 1) { logger.info("Usage: UncompressInputStream "); System.exit(1); } - - InputStream in = - new UncompressInputStream(new FileInputStream(args[0])); - - byte[] buf = new byte[100000]; - int tot = 0; long beg = System.currentTimeMillis(); - while (true) { - int got = in.read(buf); - if (got < 0) break; - System.out.write(buf, 0, got); - tot += got; + long tot; + try (InputStream in = new FileInputStream(args[0])) { + tot = uncompress(in, System.out); } long end = System.currentTimeMillis(); logger.info("Decompressed {} bytes", tot); logger.info("Time: {} seconds", (end - beg) / 1000); - in.close(); } } From 339f8108b4f33a866f02684df1003ce284fa92dc Mon Sep 17 00:00:00 2001 From: zweibieren Date: Tue, 9 Jan 2018 13:49:46 -0500 Subject: [PATCH 2/6] Update UncompressInputStream.java tweaked skip() --- .../java/org/biojava/nbio/core/util/UncompressInputStream.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java b/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java index 7dc17885c4..1b8e6a5032 100644 --- a/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java +++ b/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java @@ -381,8 +381,7 @@ private void fill() throws IOException { @Override public synchronized long skip(long num) throws IOException { - return Math.max(0, - read(new byte[(int) num], 0, (int) num)); + return Math.max(0, read(new byte[(int) num]); } From d5d23e6ec4edd154f4639134f608e80767a9e20d Mon Sep 17 00:00:00 2001 From: Fred Hansen Date: Tue, 9 Jan 2018 15:03:18 -0500 Subject: [PATCH 3/6] Update UncompressInputStream.java Typo in skip() - fix compile error --- .../java/org/biojava/nbio/core/util/UncompressInputStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java b/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java index 1b8e6a5032..e6ff770447 100644 --- a/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java +++ b/biojava-core/src/main/java/org/biojava/nbio/core/util/UncompressInputStream.java @@ -381,7 +381,7 @@ private void fill() throws IOException { @Override public synchronized long skip(long num) throws IOException { - return Math.max(0, read(new byte[(int) num]); + return Math.max(0, read(new byte[(int) num])); } From 79a5633114e869366c0c6eeac0513cbbefcb0c50 Mon Sep 17 00:00:00 2001 From: Jose Manuel Duarte Date: Wed, 24 Jan 2018 17:04:13 -0800 Subject: [PATCH 4/6] Adding test for PR #718 --- .../core/util/TestUncompressInputStream.java | 34 ++++++++++++++++++ .../nbio/core/util/compress_test.txt.lzc | Bin 0 -> 34 bytes 2 files changed, 34 insertions(+) create mode 100644 biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java create mode 100644 biojava-core/src/test/resources/org/biojava/nbio/core/util/compress_test.txt.lzc diff --git a/biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java b/biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java new file mode 100644 index 0000000000..2c6e5659f2 --- /dev/null +++ b/biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java @@ -0,0 +1,34 @@ +package org.biojava.nbio.core.util; + +import org.junit.Test; +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +public class TestUncompressInputStream { + + /** + * The file compress_text.txt.lzc is the output of: + * + * cat compress_test.txt | compress > compress_test.txt.lzc + * + * The original compress_test.txt contains text {@value #TEXT_IN_FILE} + */ + private static final String TEST_FILE = "org/biojava/nbio/core/util/compress_test.txt.lzc"; + private static final String TEXT_IN_FILE = "Test of biojava uncompress."; + + @Test + public void testUncompression() throws Exception { + + InputStream is = this.getClass().getClassLoader().getResourceAsStream(TEST_FILE); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + UncompressInputStream.uncompress(is, baos); + String decompressedText = baos.toString(); + + assertEquals(TEXT_IN_FILE, decompressedText); + + } + +} diff --git a/biojava-core/src/test/resources/org/biojava/nbio/core/util/compress_test.txt.lzc b/biojava-core/src/test/resources/org/biojava/nbio/core/util/compress_test.txt.lzc new file mode 100644 index 0000000000000000000000000000000000000000..6e8bd09c8a69507611374bce5dc2cdb64b45b979 GIT binary patch literal 34 qcmb22J0axMnT5=cdL;#tF6{{vKISJOP Date: Thu, 25 Jan 2018 15:50:15 -0500 Subject: [PATCH 5/6] bigger test files --- .../biojava/nbio/core/util/build-copy.xml.Z | Bin 0 -> 3764 bytes .../org/biojava/nbio/core/util/build.xml | 189 ++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 biojava-core/src/test/resources/org/biojava/nbio/core/util/build-copy.xml.Z create mode 100644 biojava-core/src/test/resources/org/biojava/nbio/core/util/build.xml diff --git a/biojava-core/src/test/resources/org/biojava/nbio/core/util/build-copy.xml.Z b/biojava-core/src/test/resources/org/biojava/nbio/core/util/build-copy.xml.Z new file mode 100644 index 0000000000000000000000000000000000000000..fa52cf245db230bff4b9302b65ed8bcf9eea00a7 GIT binary patch literal 3764 zcmV;l4omSLosc|!;Az7Kgq<>S=A?P!riX|aE?|fNQO3=fH)PVdIpeSj6(cQhi1^{7 zfdU>OTC@O>M|3WhsOgI&0~JJ?o?+aO0b%nEoQi+_!jbbvO_?!0FN3&4v|v`w zr)`FTS~vyR7GimfE`7SEo3&*eCed6555kF(&4j7LhK|GJ%bQOW6K2lNG;$oe5Fr+X zcMv=z&lw{p4IDZP-S`kPm<^dZEPR&93m-)0&2s9v5&Ba!Y162IFq~w@$>9PIFNjB; zdFV0J-FG~s!IoD&nL$)=BDf*OZUEBpf?$NTAYofB48(>AFFfd7Vl9|t(sv+G0hmo` z1=W*v2gwmeL3D&6#~G9fbq7#mpkW`4V$1{`b3K*879BI)w;GW~j**#Ee!ch|UQwCh zRa&Rj(V!b3zy-o%b$r$199JDB<9vSg_+yYq`h`~=JLUxD7-B}HrFU6*Nu!ium;u#S zW}y|6Mn3rnBpq|O30Rai=>UR@3c%=H3!{}Gq8pc4s_CejTFFLHn+58XUm9JO#*Tc= zmR^~0ao8IOyL~1Hdh`AH9j9;P^=YW27Q0lQQ5AZqWuT%5s~)m4L@TcU-I^;xyw2h4 zrk%FH6dV_dYV3EWqKc)UeyI_L9bt^I)LG!N5v?2Bf(1vgba)GIxZ{$G>}JX$1cJKk zy8A6r^VZ9$P5QERVG9avRFt?dmK&9-G!oqIyAI1c=Cl!?QPfaHDSWT62NwmgMk1F% zv3C{&Y^cB*8@#W_vzi(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;Mb&(8mEUkr`^Y$RJvE3m#yNNrGRRxj(Uoo2&MrRie$=yn3{<5 zX|0`_q_BsFkr!G%VtYmkuG!jq>`J8gdLdT2re)?pV($tkSu!l)9*mYMuc4tqH50WmdAL=dq_+ujJ@R)XA$kYGHEHLO%Li z=9;a2n25!qAjdi;4k0HY1qWR;q0uTpQKA_YN}J89W5Oz*Mk^k4(5W1|uTv-|(;E#f{0Of2sjcH`r{BqW1L>!3xtl)Z11XrYip!)f)VTMlWg{E8 e7E!Kz)vsm89h>W|Ws0~d#H*; + + + + + Builds, tests, and runs the project Indexer. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 494110294d58fabda20d06a9a26d221efebe152f Mon Sep 17 00:00:00 2001 From: Fred Hansen Date: Thu, 25 Jan 2018 15:52:04 -0500 Subject: [PATCH 6/6] add bigger test; fixed the little test --- .../core/util/TestUncompressInputStream.java | 86 +++++++++++-------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java b/biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java index 2c6e5659f2..1351f336fe 100644 --- a/biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java +++ b/biojava-core/src/test/java/org/biojava/nbio/core/util/TestUncompressInputStream.java @@ -1,34 +1,52 @@ -package org.biojava.nbio.core.util; - -import org.junit.Test; -import static org.junit.Assert.*; - -import java.io.ByteArrayOutputStream; -import java.io.InputStream; - -public class TestUncompressInputStream { - - /** - * The file compress_text.txt.lzc is the output of: - * - * cat compress_test.txt | compress > compress_test.txt.lzc - * - * The original compress_test.txt contains text {@value #TEXT_IN_FILE} - */ - private static final String TEST_FILE = "org/biojava/nbio/core/util/compress_test.txt.lzc"; - private static final String TEXT_IN_FILE = "Test of biojava uncompress."; - - @Test - public void testUncompression() throws Exception { - - InputStream is = this.getClass().getClassLoader().getResourceAsStream(TEST_FILE); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - UncompressInputStream.uncompress(is, baos); - String decompressedText = baos.toString(); - - assertEquals(TEXT_IN_FILE, decompressedText); - - } - -} +package org.biojava.nbio.core.util; + +import java.io.BufferedInputStream; +import org.junit.Test; +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import org.junit.Assert; + +public class TestUncompressInputStream { + + /** + * The file compress_text.txt.lzc is the output of: + * + * cat compress_test.txt | compress > compress_test.txt.lzc + * + * The original compress_test.txt contains text {@value #TEXT_IN_FILE} + */ + private static final String TEST_FILE = "org/biojava/nbio/core/util/compress_test.txt.lzc"; + private static final String TEXT_IN_FILE = "Test of biojava uncompress.\n"; + + private static final String BIGGER_TEST_FILE = "org/biojava/nbio/core/util/build-copy.xml.Z"; + private static final String ORIG_OF_BIGGER_TEST_FILE = "org/biojava/nbio/core/util/build.xml"; + + @Test + public void testUncompression() throws Exception { + + InputStream is = this.getClass().getClassLoader().getResourceAsStream(TEST_FILE); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + UncompressInputStream.uncompress(is, baos); + String decompressedText = baos.toString(); + + assertEquals(TEXT_IN_FILE, decompressedText); + + is = this.getClass().getClassLoader().getResourceAsStream(BIGGER_TEST_FILE); + baos = new ByteArrayOutputStream(); + UncompressInputStream.uncompress(is, baos); + + ByteArrayOutputStream obaos = new ByteArrayOutputStream(); + try (BufferedInputStream oin = new BufferedInputStream( + this.getClass().getClassLoader() + .getResourceAsStream(ORIG_OF_BIGGER_TEST_FILE));) { + byte[] buf = new byte[100000]; + int len; + while ((len = oin.read(buf)) >= 0) + obaos.write(buf, 0, len); + } + + Assert.assertArrayEquals(baos.toByteArray(), obaos.toByteArray()); + } +} \ No newline at end of file