001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.compressors.xz; 020 021import java.io.IOException; 022import java.io.OutputStream; 023import org.tukaani.xz.LZMA2Options; 024import org.tukaani.xz.XZOutputStream; 025 026import org.apache.commons.compress.compressors.CompressorOutputStream; 027 028/** 029 * XZ compressor. 030 * @since 1.4 031 */ 032public class XZCompressorOutputStream extends CompressorOutputStream { 033 private final XZOutputStream out; 034 035 /** 036 * Creates a new XZ compressor using the default LZMA2 options. 037 * This is equivalent to <code>XZCompressorOutputStream(6)</code>. 038 */ 039 public XZCompressorOutputStream(OutputStream outputStream) 040 throws IOException { 041 out = new XZOutputStream(outputStream, new LZMA2Options()); 042 } 043 044 /** 045 * Creates a new XZ compressor using the specified LZMA2 preset level. 046 * <p> 047 * The presets 0-3 are fast presets with medium compression. 048 * The presets 4-6 are fairly slow presets with high compression. 049 * The default preset is 6. 050 * <p> 051 * The presets 7-9 are like the preset 6 but use bigger dictionaries 052 * and have higher compressor and decompressor memory requirements. 053 * Unless the uncompressed size of the file exceeds 8 MiB, 054 * 16 MiB, or 32 MiB, it is waste of memory to use the 055 * presets 7, 8, or 9, respectively. 056 */ 057 public XZCompressorOutputStream(OutputStream outputStream, int preset) 058 throws IOException { 059 out = new XZOutputStream(outputStream, new LZMA2Options(preset)); 060 } 061 062 @Override 063 public void write(int b) throws IOException { 064 out.write(b); 065 } 066 067 @Override 068 public void write(byte[] buf, int off, int len) throws IOException { 069 out.write(buf, off, len); 070 } 071 072 /** 073 * Flushes the encoder and calls <code>outputStream.flush()</code>. 074 * All buffered pending data will then be decompressible from 075 * the output stream. Calling this function very often may increase 076 * the compressed file size a lot. 077 */ 078 @Override 079 public void flush() throws IOException { 080 out.flush(); 081 } 082 083 /** 084 * Finishes compression without closing the underlying stream. 085 * No more data can be written to this stream after finishing. 086 */ 087 public void finish() throws IOException { 088 out.finish(); 089 } 090 091 @Override 092 public void close() throws IOException { 093 out.close(); 094 } 095}