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              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              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          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          final InputStream is = new FileInputStream(input);
81          try {
82              final CompressorInputStream in = new CompressorStreamFactory()
83                  .createCompressorInputStream("bzip2", is);
84              try {
85                  assertEquals('a', in.read());
86                  assertEquals(-1, in.read());
87              } finally {
88                  in.close();
89              }
90          } finally {
91              is.close();
92          }
93      }
94  
95      @Test
96      public void testConcatenatedStreamsReadFully() throws Exception {
97          final File input = getFile("multiple.bz2");
98          final InputStream is = new FileInputStream(input);
99          try {
100             final CompressorInputStream in =
101                 new BZip2CompressorInputStream(is, true);
102             try {
103                 assertEquals('a', in.read());
104                 assertEquals('b', in.read());
105                 assertEquals(0, in.available());
106                 assertEquals(-1, in.read());
107             } finally {
108                 in.close();
109             }
110         } finally {
111             is.close();
112         }
113     }
114 
115     @Test
116     public void testCOMPRESS131() throws Exception {
117         final File input = getFile("COMPRESS-131.bz2");
118         final InputStream is = new FileInputStream(input);
119         try {
120             final CompressorInputStream in =
121                 new BZip2CompressorInputStream(is, true);
122             try {
123                 int l = 0;
124                 while(in.read() != -1) {
125                     l++;
126                 }
127                 assertEquals(539, l);
128             } finally {
129                 in.close();
130             }
131         } finally {
132             is.close();
133         }
134     }
135 
136 }