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.InputStream; |
| 23 | import org.tukaani.xz.XZ; |
| 24 | import org.tukaani.xz.SingleXZInputStream; |
| 25 | import org.tukaani.xz.XZInputStream; |
| 26 | |
| 27 | import org.apache.commons.compress.compressors.CompressorInputStream; |
| 28 | |
| 29 | /** |
| 30 | * XZ decompressor. |
Sebastian Bazley | ddbca72 | 2013-03-11 14:14:47 +0000 | [diff] [blame] | 31 | * @since 1.4 |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 32 | */ |
| 33 | public class XZCompressorInputStream extends CompressorInputStream { |
| 34 | private final InputStream in; |
| 35 | |
| 36 | /** |
| 37 | * Checks if the signature matches what is expected for a .xz file. |
| 38 | * |
| 39 | * @param signature the bytes to check |
| 40 | * @param length the number of bytes to check |
| 41 | * @return true if signature matches the .xz magic bytes, false otherwise |
| 42 | */ |
| 43 | public static boolean matches(byte[] signature, int length) { |
Sebastian Bazley | a0e435e | 2012-03-31 10:59:47 +0000 | [diff] [blame] | 44 | if (length < XZ.HEADER_MAGIC.length) { |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 45 | return false; |
Sebastian Bazley | a0e435e | 2012-03-31 10:59:47 +0000 | [diff] [blame] | 46 | } |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 47 | |
Sebastian Bazley | a0e435e | 2012-03-31 10:59:47 +0000 | [diff] [blame] | 48 | for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i) { |
| 49 | if (signature[i] != XZ.HEADER_MAGIC[i]) { |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 50 | return false; |
Sebastian Bazley | a0e435e | 2012-03-31 10:59:47 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a new input stream that decompresses XZ-compressed data |
Stefan Bodewig | fa886ac | 2011-11-09 15:43:47 +0000 | [diff] [blame] | 59 | * from the specified input stream. This doesn't support |
| 60 | * concatenated .xz files. |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 61 | * |
| 62 | * @param inputStream where to read the compressed data |
| 63 | * |
| 64 | * @throws IOException if the input is not in the .xz format, |
| 65 | * the input is corrupt or truncated, the .xz |
| 66 | * headers specify options that are not supported |
| 67 | * by this implementation, or the underlying |
| 68 | * <code>inputStream</code> throws an exception |
| 69 | */ |
| 70 | public XZCompressorInputStream(InputStream inputStream) |
| 71 | throws IOException { |
Stefan Bodewig | fa886ac | 2011-11-09 15:43:47 +0000 | [diff] [blame] | 72 | this(inputStream, false); |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a new input stream that decompresses XZ-compressed data |
| 77 | * from the specified input stream. |
| 78 | * |
| 79 | * @param inputStream where to read the compressed data |
| 80 | * @param decompressConcatenated |
| 81 | * if true, decompress until the end of the |
| 82 | * input; if false, stop after the first .xz |
| 83 | * stream and leave the input position to point |
| 84 | * to the next byte after the .xz stream |
| 85 | * |
| 86 | * @throws IOException if the input is not in the .xz format, |
| 87 | * the input is corrupt or truncated, the .xz |
| 88 | * headers specify options that are not supported |
| 89 | * by this implementation, or the underlying |
| 90 | * <code>inputStream</code> throws an exception |
| 91 | */ |
| 92 | public XZCompressorInputStream(InputStream inputStream, |
| 93 | boolean decompressConcatenated) |
| 94 | throws IOException { |
Sebastian Bazley | a0e435e | 2012-03-31 10:59:47 +0000 | [diff] [blame] | 95 | if (decompressConcatenated) { |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 96 | in = new XZInputStream(inputStream); |
Sebastian Bazley | a0e435e | 2012-03-31 10:59:47 +0000 | [diff] [blame] | 97 | } else { |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 98 | in = new SingleXZInputStream(inputStream); |
Sebastian Bazley | a0e435e | 2012-03-31 10:59:47 +0000 | [diff] [blame] | 99 | } |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 102 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 103 | public int read() throws IOException { |
| 104 | int ret = in.read(); |
| 105 | count(ret == -1 ? -1 : 1); |
| 106 | return ret; |
| 107 | } |
| 108 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 109 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 110 | public int read(byte[] buf, int off, int len) throws IOException { |
| 111 | int ret = in.read(buf, off, len); |
| 112 | count(ret); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 116 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 117 | public long skip(long n) throws IOException { |
| 118 | return in.skip(n); |
| 119 | } |
| 120 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 121 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 122 | public int available() throws IOException { |
| 123 | return in.available(); |
| 124 | } |
| 125 | |
Sebastian Bazley | 8862a48 | 2011-11-09 18:26:00 +0000 | [diff] [blame] | 126 | @Override |
Stefan Bodewig | aa73926 | 2011-11-02 16:13:41 +0000 | [diff] [blame] | 127 | public void close() throws IOException { |
| 128 | in.close(); |
| 129 | } |
| 130 | } |