    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.compress.compressors.zstandard;
    019
    020
    021import java.io.IOException;
    022import java.io.OutputStream;
    023
    024import org.apache.commons.compress.compressors.CompressorOutputStream;
    025
    026import com.github.luben.zstd.ZstdOutputStream;
    027
    028/**
    029 * {@link CompressorOutputStream} implementation to create Zstandard encoded stream.
    030 * Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard JNI</a>
    031 *
    032 * @since 1.16
    033 */
    034public class ZstdCompressorOutputStream extends CompressorOutputStream {
    035
    036    private final ZstdOutputStream encOS;
    037
    038    /**
    039     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code level}, {@code
    040     * closeFrameOnFlush} and {@code useChecksum}.
    041     * @param outStream the stream to write to
    042     * @throws IOException if zstd-jni does
    043     */
    044    public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException {
    045        this.encOS = new ZstdOutputStream(outStream);
    046    }
    047
    048    /**
    049     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush}
    050     * and {@code useChecksum}.
    051     * @param outStream the stream to write to
    052     * @param level value for zstd-jni's level argument
    053     * @throws IOException if zstd-jni does
    054     * @since 1.18
    055     */
    056    public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException {
    057        this.encOS = new ZstdOutputStream(outStream, level);
    058    }
    059
    060    /**
    061     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default value for {@code useChecksum}.
    062     * @param outStream the stream to write to
    063     * @param level value for zstd-jni's level argument
    064     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    065     * @throws IOException if zstd-jni does
    066     * @since 1.18
    067     */
    068    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush)
    069        throws IOException {
    070        this.encOS = new ZstdOutputStream(outStream, level);
    071        this.encOS.setCloseFrameOnFlush(closeFrameOnFlush);
    072    }
    073
    074    /**
    075     * Wraps the given stream into a zstd-jni ZstdOutputStream.
    076     * @param outStream the stream to write to
    077     * @param level value for zstd-jni's level argument
    078     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    079     * @param useChecksum value for zstd-jni's useChecksum argument
    080     * @throws IOException if zstd-jni does
    081     * @since 1.18
    082     */
    083    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush,
    084        final boolean useChecksum) throws IOException {
    085        this.encOS = new ZstdOutputStream(outStream, level);
    086        this.encOS.setCloseFrameOnFlush(closeFrameOnFlush);
    087        this.encOS.setChecksum(useChecksum);
    088    }
    089
    090    @Override
    091    public void close() throws IOException {
    092        encOS.close();
    093    }
    094
    095    @Override
    096    public void flush() throws IOException {
    097        encOS.flush();
    098    }
    099
    100    @Override
    101    public String toString() {
    102        return encOS.toString();
    103    }
    104
    105    @Override
    106    public void write(final byte[] buf, final int off, final int len) throws IOException {
    107        encOS.write(buf, off, len);
    108    }
    109
    110    @Override
    111    public void write(final int b) throws IOException {
    112        encOS.write(b);
    113    }
    114}