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.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.File;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.nio.file.Files;
27  
28  import org.apache.commons.compress.AbstractTest;
29  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
30  import org.junit.jupiter.api.Test;
31  
32  public final class BZip2Test extends AbstractTest {
33  
34      @Test
35      public void testBzip2Unarchive() throws Exception {
36          final File input = getFile("bla.txt.bz2");
37          final File output = newTempFile("bla.txt");
38          try (InputStream is = Files.newInputStream(input.toPath());
39                  CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("bzip2", is)) {
40              Files.copy(in, output.toPath());
41          }
42      }
43  
44      @Test
45      public void testBzipCreation() throws Exception {
46          File output;
47          final File input = getFile("test.txt");
48          {
49              output = newTempFile("test.txt.bz2");
50              try (OutputStream out = Files.newOutputStream(output.toPath());
51                      CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("bzip2", out)) {
52                  Files.copy(input.toPath(), cos);
53              }
54          }
55  
56          final File decompressed = newTempFile("decompressed.txt");
57          {
58              try (InputStream is = Files.newInputStream(output.toPath());
59                      CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("bzip2", is)) {
60                  Files.copy(in, decompressed.toPath());
61              }
62          }
63  
64          assertEquals(input.length(), decompressed.length());
65      }
66  
67      @Test
68      public void testCOMPRESS131() throws Exception {
69          final File input = getFile("COMPRESS-131.bz2");
70          try (InputStream is = Files.newInputStream(input.toPath())) {
71              try (CompressorInputStream in = new BZip2CompressorInputStream(is, true)) {
72                  int l = 0;
73                  while (in.read() != -1) {
74                      l++;
75                  }
76                  assertEquals(539, l);
77              }
78          }
79      }
80  
81      @Test
82      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
83          final File input = getFile("multiple.bz2");
84          try (InputStream is = Files.newInputStream(input.toPath())) {
85              try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("bzip2", is)) {
86                  assertEquals('a', in.read());
87                  assertEquals(-1, in.read());
88              }
89          }
90      }
91  
92      @Test
93      public void testConcatenatedStreamsReadFully() throws Exception {
94          final File input = getFile("multiple.bz2");
95          try (InputStream is = Files.newInputStream(input.toPath())) {
96              try (CompressorInputStream in = new BZip2CompressorInputStream(is, true)) {
97                  assertEquals('a', in.read());
98                  assertEquals('b', in.read());
99                  assertEquals(0, in.available());
100                 assertEquals(-1, in.read());
101             }
102         }
103     }
104 
105 }