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