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.lang.management.ManagementFactory;
27  import java.nio.file.Files;
28  
29  import org.apache.commons.compress.AbstractTest;
30  import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
31  import org.junit.jupiter.api.Test;
32  
33  public final class XZTest extends AbstractTest {
34  
35      @Test
36      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
37          final File input = getFile("multiple.xz");
38          try (InputStream is = Files.newInputStream(input.toPath())) {
39              try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("xz", is)) {
40                  assertEquals('a', in.read());
41                  assertEquals(-1, in.read());
42              }
43          }
44      }
45  
46      @Test
47      public void testConcatenatedStreamsReadFully() throws Exception {
48          final File input = getFile("multiple.xz");
49          try (InputStream is = Files.newInputStream(input.toPath())) {
50              try (CompressorInputStream in = new XZCompressorInputStream(is, true)) {
51                  assertEquals('a', in.read());
52                  assertEquals('b', in.read());
53                  assertEquals(0, in.available());
54                  assertEquals(-1, in.read());
55              }
56          }
57      }
58  
59      @Test
60      public void testXZCreation() throws Exception {
61          final long max = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax();
62          System.out.println("XZTestCase: HeapMax=" + max + " bytes " + (double) max / (1024 * 1024) + " MB");
63          final File input = getFile("test1.xml");
64          final File output = newTempFile("test1.xml.xz");
65          try (OutputStream out = Files.newOutputStream(output.toPath())) {
66              try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("xz", out)) {
67                  Files.copy(input.toPath(), cos);
68              }
69          }
70      }
71  
72      @Test
73      public void testXZUnarchive() throws Exception {
74          final File input = getFile("bla.tar.xz");
75          final File output = newTempFile("bla.tar");
76          try (InputStream is = Files.newInputStream(input.toPath())) {
77              try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("xz", is)) {
78                  Files.copy(in, output.toPath());
79              }
80          }
81      }
82  }