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