View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.compress.compressors.zstandard;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  
30  import org.apache.commons.compress.AbstractTest;
31  import org.apache.commons.compress.compressors.CompressorInputStream;
32  import org.apache.commons.compress.compressors.CompressorOutputStream;
33  import org.apache.commons.compress.compressors.CompressorStreamFactory;
34  import org.apache.commons.io.IOUtils;
35  import org.junit.jupiter.api.Test;
36  
37  public class ZstdRoundtripTest extends AbstractTest {
38  
39      private interface OutputStreamCreator {
40          ZstdCompressorOutputStream wrap(FileOutputStream os) throws IOException;
41      }
42  
43      private void roundtrip(final OutputStreamCreator oc) throws IOException {
44          final Path input = getPath("bla.tar");
45          final File output = newTempFile(input.getFileName() + ".zstd");
46          try (FileOutputStream os = new FileOutputStream(output);
47                  ZstdCompressorOutputStream zos = oc.wrap(os)) {
48              Files.copy(input, zos);
49          }
50          try (ZstdCompressorInputStream zis = new ZstdCompressorInputStream(Files.newInputStream(output.toPath()))) {
51              final byte[] expected = Files.readAllBytes(input);
52              final byte[] actual = IOUtils.toByteArray(zis);
53              assertArrayEquals(expected, actual);
54          }
55      }
56  
57      @Test
58      public void testDirectRoundtrip() throws Exception {
59          roundtrip(ZstdCompressorOutputStream::new);
60      }
61  
62      @Test
63      public void testFactoryRoundtrip() throws Exception {
64          final Path input = getPath("bla.tar");
65          final File output = newTempFile(input.getFileName() + ".zstd");
66          try (OutputStream os = Files.newOutputStream(output.toPath());
67                  CompressorOutputStream zos = new CompressorStreamFactory().createCompressorOutputStream("zstd", os)) {
68              Files.copy(input, zos);
69          }
70          try (InputStream inputStream = Files.newInputStream(output.toPath());
71                  CompressorInputStream zis = new CompressorStreamFactory().createCompressorInputStream("zstd", inputStream)) {
72              final byte[] expected = Files.readAllBytes(input);
73              final byte[] actual = IOUtils.toByteArray(zis);
74              assertArrayEquals(expected, actual);
75          }
76      }
77  
78      @Test
79      public void testRoundtripWithChecksum() throws Exception {
80          roundtrip(os -> new ZstdCompressorOutputStream(os, 3, false, true));
81      }
82  
83      @Test
84      public void testRoundtripWithCloseFrameOnFlush() throws Exception {
85          roundtrip(os -> new ZstdCompressorOutputStream(os, 3, true));
86      }
87  
88      @Test
89      public void testRoundtripWithCustomLevel() throws Exception {
90          roundtrip(os -> new ZstdCompressorOutputStream(os, 1));
91      }
92  
93  }