1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *      http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * © CrossWire Bible Society, 2014 - 2016
18   *
19   */
20  package org.crosswire.common.compress;
21  
22  import java.io.BufferedInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import org.apache.commons.compress.compressors.CompressorInputStream;
28  import org.apache.commons.compress.compressors.CompressorOutputStream;
29  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
30  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
31  import org.apache.commons.compress.utils.IOUtils;
32  
33  /**
34   * BZip2 manages the compression and uncompression of BZip2 data.
35   * 
36   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
37   * @author DM Smith
38   */
39  public class BZip2 extends AbstractCompressor {
40      /**
41       * Create a BZip2 that is capable of transforming the input.
42       * 
43       * @param input
44       *            to compress or uncompress.
45       */
46      public BZip2(InputStream input) {
47          super(input);
48      }
49  
50      /* (non-Javadoc)
51       * @see org.crosswire.common.compress.Compressor#compress()
52       */
53      public ByteArrayOutputStream compress() throws IOException {
54          BufferedInputStream in = new BufferedInputStream(input);
55          ByteArrayOutputStream bos = new ByteArrayOutputStream();
56          CompressorOutputStream out = new BZip2CompressorOutputStream(bos);
57          IOUtils.copy(in, out);
58          in.close();
59          out.flush();
60          out.close();
61          return bos;
62      }
63  
64      /* (non-Javadoc)
65       * @see org.crosswire.common.compress.Compressor#uncompress()
66       */
67      public ByteArrayOutputStream uncompress() throws IOException {
68          return uncompress(BUF_SIZE);
69      }
70  
71      /* (non-Javadoc)
72       * @see org.crosswire.common.compress.Compressor#uncompress(int)
73       */
74      public ByteArrayOutputStream uncompress(int expectedLength) throws IOException {
75          ByteArrayOutputStream out = new ByteArrayOutputStream(expectedLength);
76          CompressorInputStream in = new BZip2CompressorInputStream(input);
77          IOUtils.copy(in, out);
78          in.close();
79          out.flush();
80          out.close();
81          return out;
82      }
83  
84  }
85