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.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.BufferedInputStream;
25  import java.io.File;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.nio.file.Files;
29  import java.util.Random;
30  
31  import org.apache.commons.compress.AbstractTest;
32  import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream;
33  import org.apache.commons.io.IOUtils;
34  import org.junit.jupiter.api.Test;
35  
36  public final class FramedSnappyTest extends AbstractTest {
37  
38      @Test
39      public void testDefaultExtraction() throws Exception {
40          testUnarchive(FramedSnappyCompressorInputStream::new);
41      }
42  
43      @Test
44      public void testDefaultExtractionViaFactory() throws Exception {
45          testUnarchive(is -> new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.SNAPPY_FRAMED, is));
46      }
47  
48      @Test
49      public void testDefaultExtractionViaFactoryAutodetection() throws Exception {
50          testUnarchive(is -> new CompressorStreamFactory().createCompressorInputStream(is));
51      }
52  
53      @Test
54      public void testRoundtrip() throws Exception {
55          testRoundtrip(getFile("test.txt"));
56          testRoundtrip(getFile("bla.tar"));
57          testRoundtrip(getFile("COMPRESS-256.7z"));
58      }
59  
60      private void testRoundtrip(final File input) throws Exception {
61          final File outputSz = newTempFile(input.getName() + ".sz");
62          try (OutputStream os = Files.newOutputStream(outputSz.toPath());
63                  CompressorOutputStream sos = new CompressorStreamFactory().createCompressorOutputStream("snappy-framed", os)) {
64              Files.copy(input.toPath(), sos);
65          }
66          try (InputStream is = Files.newInputStream(input.toPath());
67                  CompressorInputStream sis = new CompressorStreamFactory().createCompressorInputStream("snappy-framed",
68                          Files.newInputStream(outputSz.toPath()))) {
69              final byte[] expected = IOUtils.toByteArray(is);
70              final byte[] actual = IOUtils.toByteArray(sis);
71              assertArrayEquals(expected, actual);
72          }
73      }
74  
75      @Test
76      public void testRoundtripWithOneBigWrite() throws Exception {
77          final Random r = new Random();
78          final File input = newTempFile("bigChunkTest");
79          try (OutputStream fs = Files.newOutputStream(input.toPath())) {
80              for (int i = 0; i < 1 << 17; i++) {
81                  fs.write(r.nextInt(256));
82              }
83          }
84          final File outputSz = newTempFile(input.getName() + ".sz");
85          try (InputStream is = Files.newInputStream(input.toPath());
86                  OutputStream os = Files.newOutputStream(outputSz.toPath());
87                  CompressorOutputStream sos = new CompressorStreamFactory().createCompressorOutputStream("snappy-framed", os)) {
88              final byte[] b = IOUtils.toByteArray(is);
89              sos.write(b[0]);
90              sos.write(b, 1, b.length - 1); // must be split into multiple compressed chunks
91          }
92          try (InputStream is = Files.newInputStream(input.toPath());
93                  CompressorInputStream sis = new CompressorStreamFactory().createCompressorInputStream("snappy-framed",
94                          Files.newInputStream(outputSz.toPath()))) {
95              final byte[] expected = IOUtils.toByteArray(is);
96              final byte[] actual = IOUtils.toByteArray(sis);
97              assertArrayEquals(expected, actual);
98          }
99      }
100 
101     private void testUnarchive(final StreamWrapper<CompressorInputStream> wrapper) throws Exception {
102         final File input = getFile("bla.tar.sz");
103         final File output = newTempFile("bla.tar");
104         try (InputStream is = Files.newInputStream(input.toPath())) {
105             // the intermediate BufferedInputStream is there for mark
106             // support in the autodetection test
107             try (CompressorInputStream in = wrapper.wrap(new BufferedInputStream(is))) {
108                 Files.copy(in, output.toPath());
109                 assertEquals(995, in.getBytesRead());
110             }
111         }
112         final File original = getFile("bla.tar");
113         try (InputStream written = Files.newInputStream(output.toPath())) {
114             try (InputStream orig = Files.newInputStream(original.toPath())) {
115                 assertArrayEquals(IOUtils.toByteArray(written), IOUtils.toByteArray(orig));
116             }
117         }
118     }
119 }