ZstdCompressorOutputStream.java

  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. package org.apache.commons.compress.compressors.zstandard;

  18. import java.io.IOException;
  19. import java.io.OutputStream;

  20. import org.apache.commons.compress.compressors.CompressorOutputStream;

  21. import com.github.luben.zstd.ZstdOutputStream;

  22. /**
  23.  * {@link CompressorOutputStream} implementation to create Zstandard encoded stream. Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard
  24.  * JNI</a>
  25.  *
  26.  * @since 1.16
  27.  */
  28. public class ZstdCompressorOutputStream extends CompressorOutputStream<ZstdOutputStream> {

  29.     /**
  30.      * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code level}, {@code
  31.      * closeFrameOnFlush} and {@code useChecksum}.
  32.      *
  33.      * @param outStream the stream to write to
  34.      * @throws IOException if zstd-jni does
  35.      */
  36.     @SuppressWarnings("resource") // Caller closes
  37.     public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException {
  38.         super(new ZstdOutputStream(outStream));
  39.     }

  40.     /**
  41.      * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush} and {@code useChecksum}.
  42.      *
  43.      * @param outStream the stream to write to
  44.      * @param level     value for zstd-jni's level argument
  45.      * @throws IOException if zstd-jni does
  46.      * @since 1.18
  47.      */
  48.     @SuppressWarnings("resource") // Caller closes
  49.     public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException {
  50.         super(new ZstdOutputStream(outStream, level));
  51.     }

  52.     /**
  53.      * Wraps the given stream into a zstd-jni ZstdOutputStream using the default value for {@code useChecksum}.
  54.      *
  55.      * @param outStream         the stream to write to
  56.      * @param level             value for zstd-jni's level argument
  57.      * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
  58.      * @throws IOException if zstd-jni does
  59.      * @since 1.18
  60.      */
  61.     @SuppressWarnings("resource") // Caller closes
  62.     public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush) throws IOException {
  63.         super(new ZstdOutputStream(outStream, level));
  64.         out().setCloseFrameOnFlush(closeFrameOnFlush);
  65.     }

  66.     /**
  67.      * Wraps the given stream into a zstd-jni ZstdOutputStream.
  68.      *
  69.      * @param outStream         the stream to write to
  70.      * @param level             value for zstd-jni's level argument
  71.      * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
  72.      * @param useChecksum       value for zstd-jni's useChecksum argument
  73.      * @throws IOException if zstd-jni does
  74.      * @since 1.18
  75.      */
  76.     @SuppressWarnings("resource") // Caller closes
  77.     public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush, final boolean useChecksum)
  78.             throws IOException {
  79.         super(new ZstdOutputStream(outStream, level));
  80.         out()
  81.             .setCloseFrameOnFlush(closeFrameOnFlush)
  82.             .setChecksum(useChecksum);
  83.     }

  84.     @Override
  85.     public String toString() {
  86.         return out.toString();
  87.     }

  88.     @Override
  89.     public void write(final byte[] buf, final int off, final int len) throws IOException {
  90.         out.write(buf, off, len);
  91.     }

  92. }