    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.
    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 using the default values for {@code level}, {@code
    039     * closeFrameOnFlush} and {@code useChecksum}.
    040     * @param outStream the stream to write to
    041     * @throws IOException if zstd-jni does
    042     */
    043    public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException {
    044        this.encOS = new ZstdOutputStream(outStream);
    045    }
    046
    047    /**
    048     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush}
    049     * and {@code useChecksum}.
    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    public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException {
    056        this.encOS = new ZstdOutputStream(outStream, level);
    057    }
    058
    059    /**
    060     * Wraps the given stream into a zstd-jni ZstdOutputStream using the default value for {@code useChecksum}.
    061     * @param outStream the stream to write to
    062     * @param level value for zstd-jni's level argument
    063     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    064     * @throws IOException if zstd-jni does
    065     * @since 1.18
    066     */
    067    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush)
    068        throws IOException {
    069        this.encOS = new ZstdOutputStream(outStream, level);
    070        this.encOS.setCloseFrameOnFlush(closeFrameOnFlush);
    071    }
    072
    073    /**
    074     * Wraps the given stream into a zstd-jni ZstdOutputStream.
    075     * @param outStream the stream to write to
    076     * @param level value for zstd-jni's level argument
    077     * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument
    078     * @param useChecksum value for zstd-jni's useChecksum argument
    079     * @throws IOException if zstd-jni does
    080     * @since 1.18
    081     */
    082    public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush,
    083        final boolean useChecksum) throws IOException {
    084        this.encOS = new ZstdOutputStream(outStream, level);
    085        this.encOS.setCloseFrameOnFlush(closeFrameOnFlush);
    086        this.encOS.setChecksum(useChecksum);
    087    }
    088
    089    @Override
    090    public void close() throws IOException {
    091        encOS.close();
    092    }
    093
    094    @Override
    095    public void flush() throws IOException {
    096        encOS.flush();
    097    }
    098
    099    @Override
    100    public String toString() {
    101        return encOS.toString();
    102    }
    103
    104    @Override
    105    public void write(final byte[] buf, final int off, final int len) throws IOException {
    106        encOS.write(buf, off, len);
    107    }
    108
    109    @Override
    110    public void write(final int b) throws IOException {
    111        encOS.write(b);
    112    }
    113}