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          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          final OutputStream out = new FileOutputStream(output);
44          try {
45              final CompressorOutputStream cos = new CompressorStreamFactory()
46                  .createCompressorOutputStream("xz", out);
47              try {
48                  IOUtils.copy(new FileInputStream(input), cos);
49              } finally {
50                  cos.close();
51              }
52          } finally {
53              out.close();
54          }
55      }
56  
57      @Test
58      public void testXZUnarchive() throws Exception {
59          final File input = getFile("bla.tar.xz");
60          final File output = new File(dir, "bla.tar");
61          final InputStream is = new FileInputStream(input);
62          try {
63              final CompressorInputStream in = new CompressorStreamFactory()
64                  .createCompressorInputStream("xz", is);
65              FileOutputStream out = null;
66              try {
67                  out = new FileOutputStream(output);
68                  IOUtils.copy(in, out);
69              } finally {
70                  if (out != null) {
71                      out.close();
72                  }
73                  in.close();
74              }
75          } finally {
76              is.close();
77          }
78      }
79  
80      @Test
81      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
82          final File input = getFile("multiple.xz");
83          final InputStream is = new FileInputStream(input);
84          try {
85              final CompressorInputStream in = new CompressorStreamFactory()
86                  .createCompressorInputStream("xz", is);
87              try {
88                  assertEquals('a', in.read());
89                  assertEquals(-1, in.read());
90              } finally {
91                  in.close();
92              }
93          } finally {
94              is.close();
95          }
96      }
97  
98      @Test
99      public void testConcatenatedStreamsReadFully() throws Exception {
100         final File input = getFile("multiple.xz");
101         final InputStream is = new FileInputStream(input);
102         try {
103             final CompressorInputStream in =
104                 new XZCompressorInputStream(is, true);
105             try {
106                 assertEquals('a', in.read());
107                 assertEquals('b', in.read());
108                 assertEquals(0, in.available());
109                 assertEquals(-1, in.read());
110             } finally {
111                 in.close();
112             }
113         } finally {
114             is.close();
115         }
116     }
117 }