1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.compress.compressors.zstandard; 19 20 21 import java.io.IOException; 22 import java.io.OutputStream; 23 24 import com.github.luben.zstd.ZstdOutputStream; 25 import org.apache.commons.compress.compressors.CompressorOutputStream; 26 27 /** 28 * {@link CompressorOutputStream} implementation to create Zstandard encoded stream. 29 * Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard JNI</a> 30 * 31 * @since 1.16 32 */ 33 public class ZstdCompressorOutputStream extends CompressorOutputStream { 34 35 private final ZstdOutputStream encOS; 36 37 /** 38 * Wraps the given stream into a zstd-jni ZstdOutputStream. 39 * @param outStream the stream to write to 40 * @param level value for zstd-jni's level argument 41 * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument 42 * @param useChecksum value for zstd-jni's useChecksum argument 43 * @throws IOException if zstd-jni does 44 * @since 1.18 45 */ 46 public ZstdCompressorOutputStream(final OutputStream outStream, int level, boolean closeFrameOnFlush, 47 boolean useChecksum) throws IOException { 48 this.encOS = new ZstdOutputStream(outStream, level, closeFrameOnFlush, useChecksum); 49 } 50 51 /** 52 * Wraps the given stream into a zstd-jni ZstdOutputStream using the default value for {@code useChecksum}. 53 * @param outStream the stream to write to 54 * @param level value for zstd-jni's level argument 55 * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument 56 * @throws IOException if zstd-jni does 57 * @since 1.18 58 */ 59 public ZstdCompressorOutputStream(final OutputStream outStream, int level, boolean closeFrameOnFlush) 60 throws IOException { 61 this.encOS = new ZstdOutputStream(outStream, level, closeFrameOnFlush); 62 } 63 64 /** 65 * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush} 66 * and {@code useChecksum}. 67 * @param outStream the stream to write to 68 * @param level value for zstd-jni's level argument 69 * @throws IOException if zstd-jni does 70 * @since 1.18 71 */ 72 public ZstdCompressorOutputStream(final OutputStream outStream, int level) throws IOException { 73 this.encOS = new ZstdOutputStream(outStream, level); 74 } 75 76 /** 77 * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code level}, {@code 78 * closeFrameOnFlush} and {@code useChecksum}. 79 * @param outStream the stream to write to 80 * @throws IOException if zstd-jni does 81 */ 82 public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException { 83 this.encOS = new ZstdOutputStream(outStream); 84 } 85 86 @Override 87 public void close() throws IOException { 88 encOS.close(); 89 } 90 91 @Override 92 public void write(final int b) throws IOException { 93 encOS.write(b); 94 } 95 96 @Override 97 public void write(final byte[] buf, final int off, final int len) throws IOException { 98 encOS.write(buf, off, len); 99 } 100 101 @Override 102 public String toString() { 103 return encOS.toString(); 104 } 105 106 @Override 107 public void flush() throws IOException { 108 encOS.flush(); 109 } 110 } 111