    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
    020import java.io.IOException;
    021import java.io.OutputStream;
    022
    023import org.apache.commons.compress.compressors.CompressorOutputStream;
    024
    025import com.github.luben.zstd.ZstdOutputStream;
    026
    027/**
    028 * {@link CompressorOutputStream} implementation to create Zstandard encoded stream. Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard
    029 * JNI</a>
    030 *
    031 * @since 1.16
    032 */
    033public class ZstdCompressorOutputStream extends CompressorOutputStream<ZstdOutputStream> {
    034
    035    /**
    036     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code level}, {@code
    037     * closeFrameOnFlush} and {@code useChecksum}.
    038     *
    039     * @param outStream the stream to write to
    040     * @throws IOException if zstd-jni does
    041     */
    042    @SuppressWarnings("resource") // Caller closes
    043    public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException {
    044        super(new ZstdOutputStream(outStream));
    045    }
    046
    047    /**
    048     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush} and {@code useChecksum}.
    049     *
    050     * @param outStream the stream to write to
    051     * @param level     value for zstd-jni's level argument
    052     * @throws IOException if zstd-jni does
    053     * @since 1.18
    054     */
    055    @SuppressWarnings("resource") // Caller closes
    056    public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException {
    057        super(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     *
    063     * @param outStream         the stream to write to
    064     * @param level             value for zstd-jni's level argument
    065     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    066     * @throws IOException if zstd-jni does
    067     * @since 1.18
    068     */
    069    @SuppressWarnings("resource") // Caller closes
    070    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush) throws IOException {
    071        super(new ZstdOutputStream(outStream, level));
    072        out().setCloseFrameOnFlush(closeFrameOnFlush);
    073    }
    074
    075    /**
    076     * Wraps the given stream into a zstd-jni ZstdOutputStream.
    077     *
    078     * @param outStream         the stream to write to
    079     * @param level             value for zstd-jni's level argument
    080     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    081     * @param useChecksum       value for zstd-jni's useChecksum argument
    082     * @throws IOException if zstd-jni does
    083     * @since 1.18
    084     */
    085    @SuppressWarnings("resource") // Caller closes
    086    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush, final boolean useChecksum)
    087            throws IOException {
    088        super(new ZstdOutputStream(outStream, level));
    089        out()
    090            .setCloseFrameOnFlush(closeFrameOnFlush)
    091            .setChecksum(useChecksum);
    092    }
    093
    094    @Override
    095    public String toString() {
    096        return out.toString();
    097    }
    098
    099    @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}