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 { 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 * 041 * @param outStream the stream to write to 042 * @throws IOException if zstd-jni does 043 */ 044 public ZstdCompressorOutputStream(final OutputStream outStream) throws IOException { 045 this.encOS = new ZstdOutputStream(outStream); 046 } 047 048 /** 049 * Wraps the given stream into a zstd-jni ZstdOutputStream using the default values for {@code closeFrameOnFlush} and {@code useChecksum}. 050 * 051 * @param outStream the stream to write to 052 * @param level value for zstd-jni's level argument 053 * @throws IOException if zstd-jni does 054 * @since 1.18 055 */ 056 public ZstdCompressorOutputStream(final OutputStream outStream, final int level) throws IOException { 057 this.encOS = 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 public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush) throws IOException { 070 this.encOS = new ZstdOutputStream(outStream, level); 071 this.encOS.setCloseFrameOnFlush(closeFrameOnFlush); 072 } 073 074 /** 075 * Wraps the given stream into a zstd-jni ZstdOutputStream. 076 * 077 * @param outStream the stream to write to 078 * @param level value for zstd-jni's level argument 079 * @param closeFrameOnFlush value for zstd-jni's closeFrameOnFlush argument 080 * @param useChecksum value for zstd-jni's useChecksum argument 081 * @throws IOException if zstd-jni does 082 * @since 1.18 083 */ 084 public ZstdCompressorOutputStream(final OutputStream outStream, final int level, final boolean closeFrameOnFlush, final boolean useChecksum) 085 throws IOException { 086 this.encOS = new ZstdOutputStream(outStream, level); 087 this.encOS.setCloseFrameOnFlush(closeFrameOnFlush); 088 this.encOS.setChecksum(useChecksum); 089 } 090 091 @Override 092 public void close() throws IOException { 093 encOS.close(); 094 } 095 096 @Override 097 public void flush() throws IOException { 098 encOS.flush(); 099 } 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}