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; 023 024import org.apache.commons.compress.compressors.CompressorOutputStream; 025import org.tukaani.xz.LZMA2Options; 026import org.tukaani.xz.XZOutputStream; 027 028/** 029 * XZ compressor. 030 * 031 * <em>Calling flush()</em> 032 * <p> 033 * Calling {@link #flush()} flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output 034 * stream. Calling this function very often may increase the compressed file size a lot. 035 * </p> 036 * 037 * @since 1.4 038 */ 039public class XZCompressorOutputStream extends CompressorOutputStream<XZOutputStream> { 040 041 /** 042 * Creates a new XZ compressor using the default LZMA2 options. This is equivalent to {@code XZCompressorOutputStream(outputStream, 6)}. 043 * 044 * @param outputStream the stream to wrap 045 * @throws IOException on error 046 */ 047 @SuppressWarnings("resource") // Caller closes 048 public XZCompressorOutputStream(final OutputStream outputStream) throws IOException { 049 super(new XZOutputStream(outputStream, new LZMA2Options())); 050 } 051 052 /** 053 * Creates a new XZ compressor using the specified LZMA2 preset level. 054 * <p> 055 * The presets 0-3 are fast presets with medium compression. The presets 4-6 are fairly slow presets with high compression. The default preset is 6. 056 * <p> 057 * The presets 7-9 are like the preset 6 but use bigger dictionaries and have higher compressor and decompressor memory requirements. Unless the 058 * uncompressed size of the file exceeds 8 MiB, 16 MiB, or 32 MiB, it is waste of memory to use the presets 7, 8, or 9, respectively. 059 * 060 * @param outputStream the stream to wrap 061 * @param preset the preset 062 * @throws IOException on error 063 */ 064 @SuppressWarnings("resource") // Caller closes 065 public XZCompressorOutputStream(final OutputStream outputStream, final int preset) throws IOException { 066 super(new XZOutputStream(outputStream, new LZMA2Options(preset))); 067 } 068 069 /** 070 * Finishes compression without closing the underlying stream. No more data can be written to this stream after finishing. 071 * 072 * @throws IOException on error 073 */ 074 @SuppressWarnings("resource") // instance variable access 075 public void finish() throws IOException { 076 out().finish(); 077 } 078 079 @Override 080 public void write(final byte[] buf, final int off, final int len) throws IOException { 081 out.write(buf, off, len); 082 } 083 084}