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 java.io.IOException;
21  import java.io.OutputStream;
22  
23  import org.apache.commons.compress.compressors.CompressorOutputStream;
24  
25  import com.github.luben.zstd.ZstdOutputStream;
26  
27  /**
28   * {@link CompressorOutputStream} implementation to create Zstandard encoded stream. Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard
29   * JNI</a>
30   *
31   * @since 1.16
32   */
33  public class ZstdCompressorOutputStream extends CompressorOutputStream<ZstdOutputStream> {
34  
35      /**
36       * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code level}, {@code
37       * closeFrameOnFlush} and {@code useChecksum}.
38       *
39       * @param outStream the stream to write to
40       * @throws IOException if zstd-jni does
41       */
42      @SuppressWarnings("resource") // Caller closes
43      public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException {
44          super(new ZstdOutputStream(outStream));
45      }
46  
47      /**
48       * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush} and {@code useChecksum}.
49       *
50       * @param outStream the stream to write to
51       * @param level     value for zstd-jni's level argument
52       * @throws IOException if zstd-jni does
53       * @since 1.18
54       */
55      @SuppressWarnings("resource") // Caller closes
56      public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException {
57          super(new ZstdOutputStream(outStream, level));
58      }
59  
60      /**
61       * Wraps the given stream into a zstd-jni ZstdOutputStream using the default value for {@code useChecksum}.
62       *
63       * @param outStream         the stream to write to
64       * @param level             value for zstd-jni's level argument
65       * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
66       * @throws IOException if zstd-jni does
67       * @since 1.18
68       */
69      @SuppressWarnings("resource") // Caller closes
70      public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush) throws IOException {
71          super(new ZstdOutputStream(outStream, level));
72          out().setCloseFrameOnFlush(closeFrameOnFlush);
73      }
74  
75      /**
76       * Wraps the given stream into a zstd-jni ZstdOutputStream.
77       *
78       * @param outStream         the stream to write to
79       * @param level             value for zstd-jni's level argument
80       * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
81       * @param useChecksum       value for zstd-jni's useChecksum argument
82       * @throws IOException if zstd-jni does
83       * @since 1.18
84       */
85      @SuppressWarnings("resource") // Caller closes
86      public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush, final boolean useChecksum)
87              throws IOException {
88          super(new ZstdOutputStream(outStream, level));
89          out()
90              .setCloseFrameOnFlush(closeFrameOnFlush)
91              .setChecksum(useChecksum);
92      }
93  
94      @Override
95      public String toString() {
96          return out.toString();
97      }
98  
99      @Override
100     public void write(final byte[] buf, final int off, final int len) throws IOException {
101         out.write(buf, off, len);
102     }
103 
104 }