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 import java.io.IOException; 21 import java.io.OutputStream; 22 23 import org.apache.commons.compress.compressors.CompressorOutputStream; 24 25 import com.github.luben.zstd.ZstdOutputStream; 26 27 /** 28 * {@link CompressorOutputStream} implementation to create Zstandard encoded stream. Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard 29 * 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 using the default values for {@code level}, {@code 39 * closeFrameOnFlush} and {@code useChecksum}. 40 * 41 * @param outStream the stream to write to 42 * @throws IOException if zstd-jni does 43 */ 44 public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException { 45 this.encOS = new ZstdOutputStream(outStream); 46 } 47 48 /** 49 * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush} and {@code useChecksum}. 50 * 51 * @param outStream the stream to write to 52 * @param level value for zstd-jni's level argument 53 * @throws IOException if zstd-jni does 54 * @since 1.18 55 */ 56 public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException { 57 this.encOS = new ZstdOutputStream(outStream, level); 58 } 59 60 /** 61 * Wraps the given stream into a zstd-jni ZstdOutputStream using the default value for {@code useChecksum}. 62 * 63 * @param outStream the stream to write to 64 * @param level value for zstd-jni's level argument 65 * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument 66 * @throws IOException if zstd-jni does 67 * @since 1.18 68 */ 69 public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush) throws IOException { 70 this.encOS = new ZstdOutputStream(outStream, level); 71 this.encOS.setCloseFrameOnFlush(closeFrameOnFlush); 72 } 73 74 /** 75 * Wraps the given stream into a zstd-jni ZstdOutputStream. 76 * 77 * @param outStream the stream to write to 78 * @param level value for zstd-jni's level argument 79 * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument 80 * @param useChecksum value for zstd-jni's useChecksum argument 81 * @throws IOException if zstd-jni does 82 * @since 1.18 83 */ 84 public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush, final boolean useChecksum) 85 throws IOException { 86 this.encOS = new ZstdOutputStream(outStream, level); 87 this.encOS.setCloseFrameOnFlush(closeFrameOnFlush); 88 this.encOS.setChecksum(useChecksum); 89 } 90 91 @Override 92 public void close() throws IOException { 93 encOS.close(); 94 } 95 96 @Override 97 public void flush() throws IOException { 98 encOS.flush(); 99 } 100 101 @Override 102 public String toString() { 103 return encOS.toString(); 104 } 105 106 @Override 107 public void write(final byte[] buf, final int off, final int len) throws IOException { 108 encOS.write(buf, off, len); 109 } 110 111 @Override 112 public void write(final int b) throws IOException { 113 encOS.write(b); 114 } 115 }