    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 com.github.luben.zstd.ZstdOutputStream;
    025import org.apache.commons.compress.compressors.CompressorOutputStream;
    026
    027/**
    028 * {@link CompressorOutputStream} implementation to create Zstandard encoded stream.
    029 * Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard JNI</a>
    030 *
    031 * @since 1.16
    032 */
    033public class ZstdCompressorOutputStream extends CompressorOutputStream {
    034
    035    private final ZstdOutputStream encOS;
    036
    037    /**
    038     * Wraps the given stream into a zstd-jni ZstdOutputStream.
    039     * @param outStream the stream to write to
    040     * @param level value for zstd-jni's level argument
    041     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    042     * @param useChecksum value for zstd-jni's useChecksum argument
    043     * @throws IOException if zstd-jni does
    044     * @since 1.18
    045     */
    046    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush,
    047        final boolean useChecksum) throws IOException {
    048        this.encOS = new ZstdOutputStream(outStream, level);
    049        this.encOS.setCloseFrameOnFlush(closeFrameOnFlush);
    050        this.encOS.setChecksum(useChecksum);
    051    }
    052
    053    /**
    054     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default value for {@code useChecksum}.
    055     * @param outStream the stream to write to
    056     * @param level value for zstd-jni's level argument
    057     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    058     * @throws IOException if zstd-jni does
    059     * @since 1.18
    060     */
    061    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush)
    062        throws IOException {
    063        this.encOS = new ZstdOutputStream(outStream, level);
    064        this.encOS.setCloseFrameOnFlush(closeFrameOnFlush);
    065    }
    066
    067    /**
    068     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush}
    069     * and {@code useChecksum}.
    070     * @param outStream the stream to write to
    071     * @param level value for zstd-jni's level argument
    072     * @throws IOException if zstd-jni does
    073     * @since 1.18
    074     */
    075    public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException {
    076        this.encOS = new ZstdOutputStream(outStream, level);
    077    }
    078
    079    /**
    080     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code level}, {@code
    081     * closeFrameOnFlush} and {@code useChecksum}.
    082     * @param outStream the stream to write to
    083     * @throws IOException if zstd-jni does
    084     */
    085    public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException {
    086        this.encOS = new ZstdOutputStream(outStream);
    087    }
    088
    089    @Override
    090    public void close() throws IOException {
    091        encOS.close();
    092    }
    093
    094    @Override
    095    public void write(final int b) throws IOException {
    096        encOS.write(b);
    097    }
    098
    099    @Override
    100    public void write(final byte[] buf, final int off, final int len) throws IOException {
    101        encOS.write(buf, off, len);
    102    }
    103
    104    @Override
    105    public String toString() {
    106        return encOS.toString();
    107    }
    108
    109    @Override
    110    public void flush() throws IOException {
    111        encOS.flush();
    112    }
    113}