View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.compressors;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  
29  import org.apache.commons.compress.AbstractTestCase;
30  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
31  import org.apache.commons.compress.utils.IOUtils;
32  import org.junit.Test;
33  
34  public final class BZip2TestCase extends AbstractTestCase {
35  
36      @Test
37      public void testBzipCreation()  throws Exception {
38          File output = null;
39          final File input = getFile("test.txt");
40          {
41              output = new File(dir, "test.txt.bz2");
42              final OutputStream out = new FileOutputStream(output);
43              final CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("bzip2", out);
44              final FileInputStream in = new FileInputStream(input);
45              IOUtils.copy(in, cos);
46              cos.close();
47              in.close();
48          }
49  
50          final File decompressed = new File(dir, "decompressed.txt");
51          {
52              final File toDecompress = output;
53              final InputStream is = new FileInputStream(toDecompress);
54              final CompressorInputStream in =
55                  new CompressorStreamFactory().createCompressorInputStream("bzip2", is);
56              final FileOutputStream os = new FileOutputStream(decompressed);
57              IOUtils.copy(in, os);
58              is.close();
59              os.close();
60          }
61  
62          assertEquals(input.length(),decompressed.length());
63      }
64  
65      @Test
66      public void testBzip2Unarchive() throws Exception {
67          final File input = getFile("bla.txt.bz2");
68          final File output = new File(dir, "bla.txt");
69          final InputStream is = new FileInputStream(input);
70          final CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("bzip2", is);
71          final FileOutputStream os = new FileOutputStream(output);
72          IOUtils.copy(in, os);
73          is.close();
74          os.close();
75      }
76  
77      @Test
78      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
79          final File input = getFile("multiple.bz2");
80          try (InputStream is = new FileInputStream(input)) {
81              try (CompressorInputStream in = new CompressorStreamFactory()
82                      .createCompressorInputStream("bzip2", is)) {
83                  assertEquals('a', in.read());
84                  assertEquals(-1, in.read());
85              }
86          }
87      }
88  
89      @Test
90      public void testConcatenatedStreamsReadFully() throws Exception {
91          final File input = getFile("multiple.bz2");
92          try (InputStream is = new FileInputStream(input)) {
93              try (CompressorInputStream in = new BZip2CompressorInputStream(is, true)) {
94                  assertEquals('a', in.read());
95                  assertEquals('b', in.read());
96                  assertEquals(0, in.available());
97                  assertEquals(-1, in.read());
98              }
99          }
100     }
101 
102     @Test
103     public void testCOMPRESS131() throws Exception {
104         final File input = getFile("COMPRESS-131.bz2");
105         try (InputStream is = new FileInputStream(input)) {
106             try (CompressorInputStream in = new BZip2CompressorInputStream(is, true)) {
107                 int l = 0;
108                 while (in.read() != -1) {
109                     l++;
110                 }
111                 assertEquals(539, l);
112             }
113         }
114     }
115 
116 }