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  import java.lang.management.ManagementFactory;
29  
30  import org.apache.commons.compress.AbstractTestCase;
31  import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
32  import org.apache.commons.compress.utils.IOUtils;
33  import org.junit.Test;
34  
35  public final class XZTestCase extends AbstractTestCase {
36  
37      @Test
38      public void testXZCreation()  throws Exception {
39          final long max = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax();
40          System.out.println("XZTestCase: HeapMax="+max+" bytes "+(double)max/(1024*1024)+" MB");
41          final File input = getFile("test1.xml");
42          final File output = new File(dir, "test1.xml.xz");
43          try (OutputStream out = new FileOutputStream(output)) {
44              try (CompressorOutputStream cos = new CompressorStreamFactory()
45                      .createCompressorOutputStream("xz", out)) {
46                  IOUtils.copy(new FileInputStream(input), cos);
47              }
48          }
49      }
50  
51      @Test
52      public void testXZUnarchive() throws Exception {
53          final File input = getFile("bla.tar.xz");
54          final File output = new File(dir, "bla.tar");
55          try (InputStream is = new FileInputStream(input)) {
56              final CompressorInputStream in = new CompressorStreamFactory()
57                      .createCompressorInputStream("xz", is);
58              FileOutputStream out = null;
59              try {
60                  out = new FileOutputStream(output);
61                  IOUtils.copy(in, out);
62              } finally {
63                  if (out != null) {
64                      out.close();
65                  }
66                  in.close();
67              }
68          }
69      }
70  
71      @Test
72      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
73          final File input = getFile("multiple.xz");
74          try (InputStream is = new FileInputStream(input)) {
75              try (CompressorInputStream in = new CompressorStreamFactory()
76                      .createCompressorInputStream("xz", is)) {
77                  assertEquals('a', in.read());
78                  assertEquals(-1, in.read());
79              }
80          }
81      }
82  
83      @Test
84      public void testConcatenatedStreamsReadFully() throws Exception {
85          final File input = getFile("multiple.xz");
86          try (InputStream is = new FileInputStream(input)) {
87              try (CompressorInputStream in = new XZCompressorInputStream(is, true)) {
88                  assertEquals('a', in.read());
89                  assertEquals('b', in.read());
90                  assertEquals(0, in.available());
91                  assertEquals(-1, in.read());
92              }
93          }
94      }
95  }