Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.commons.compress.compressors.xz; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.OutputStream; |
| 23 | import org.tukaani.xz.LZMA2Options; |
| 24 | import org.tukaani.xz.XZOutputStream; |
| 25 | |
| 26 | import org.apache.commons.compress.compressors.CompressorOutputStream; |
| 27 | |
| 28 | /** |
| 29 | * XZ compressor. |
Sebastian Bazley | ddbca72 | 2013-03-11 14:14:47 +0000 | [diff] [blame] | 30 | * @since 1.4 |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 31 | */ |
| 32 | public class XZCompressorOutputStream extends CompressorOutputStream { |
| 33 | private final XZOutputStream out; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new XZ compressor using the default LZMA2 options. |
| 37 | * This is equivalent to <code>XZCompressorOutputStream(6)</code>. |
| 38 | */ |
| 39 | public XZCompressorOutputStream(OutputStream outputStream) |
| 40 | throws IOException { |
| 41 | out = new XZOutputStream(outputStream, new LZMA2Options()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a new XZ compressor using the specified LZMA2 preset level. |
| 46 | * <p> |
| 47 | * The presets 0-3 are fast presets with medium compression. |
| 48 | * The presets 4-6 are fairly slow presets with high compression. |
| 49 | * The default preset is 6. |
| 50 | * <p> |
| 51 | * The presets 7-9 are like the preset 6 but use bigger dictionaries |
| 52 | * and have higher compressor and decompressor memory requirements. |
| 53 | * Unless the uncompressed size of the file exceeds 8 MiB, |
| 54 | * 16 MiB, or 32 MiB, it is waste of memory to use the |
| 55 | * presets 7, 8, or 9, respectively. |
| 56 | */ |
| 57 | public XZCompressorOutputStream(OutputStream outputStream, int preset) |
| 58 | throws IOException { |
| 59 | out = new XZOutputStream(outputStream, new LZMA2Options(preset)); |
| 60 | } |
| 61 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 62 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 63 | public void write(int b) throws IOException { |
| 64 | out.write(b); |
| 65 | } |
| 66 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 67 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 68 | public void write(byte[] buf, int off, int len) throws IOException { |
| 69 | out.write(buf, off, len); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Flushes the encoder and calls <code>outputStream.flush()</code>. |
| 74 | * All buffered pending data will then be decompressible from |
| 75 | * the output stream. Calling this function very often may increase |
| 76 | * the compressed file size a lot. |
| 77 | */ |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 78 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 79 | public void flush() throws IOException { |
| 80 | out.flush(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Finishes compression without closing the underlying stream. |
| 85 | * No more data can be written to this stream after finishing. |
| 86 | */ |
| 87 | public void finish() throws IOException { |
| 88 | out.finish(); |
| 89 | } |
| 90 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 91 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 92 | public void close() throws IOException { |
| 93 | out.close(); |
| 94 | } |
| 95 | } |