blob: c6e4e9ec4759532493f2ee93766d22b47663c1e6 [file] [log] [blame]
Stefan Bodewigaa739262011-11-02 16:13:41 +00001/*
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 */
19package org.apache.commons.compress.compressors.xz;
20
21import java.io.IOException;
22import java.io.InputStream;
23import org.tukaani.xz.XZ;
24import org.tukaani.xz.SingleXZInputStream;
25import org.tukaani.xz.XZInputStream;
26
27import org.apache.commons.compress.compressors.CompressorInputStream;
28
29/**
30 * XZ decompressor.
Sebastian Bazleyddbca722013-03-11 14:14:47 +000031 * @since 1.4
Stefan Bodewigaa739262011-11-02 16:13:41 +000032 */
33public 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 Bazleya0e435e2012-03-31 10:59:47 +000044 if (length < XZ.HEADER_MAGIC.length) {
Stefan Bodewigaa739262011-11-02 16:13:41 +000045 return false;
Sebastian Bazleya0e435e2012-03-31 10:59:47 +000046 }
Stefan Bodewigaa739262011-11-02 16:13:41 +000047
Sebastian Bazleya0e435e2012-03-31 10:59:47 +000048 for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i) {
49 if (signature[i] != XZ.HEADER_MAGIC[i]) {
Stefan Bodewigaa739262011-11-02 16:13:41 +000050 return false;
Sebastian Bazleya0e435e2012-03-31 10:59:47 +000051 }
52 }
Stefan Bodewigaa739262011-11-02 16:13:41 +000053
54 return true;
55 }
56
57 /**
58 * Creates a new input stream that decompresses XZ-compressed data
Stefan Bodewigfa886ac2011-11-09 15:43:47 +000059 * from the specified input stream. This doesn't support
60 * concatenated .xz files.
Stefan Bodewigaa739262011-11-02 16:13:41 +000061 *
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 Bodewigfa886ac2011-11-09 15:43:47 +000072 this(inputStream, false);
Stefan Bodewigaa739262011-11-02 16:13:41 +000073 }
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 Bazleya0e435e2012-03-31 10:59:47 +000095 if (decompressConcatenated) {
Stefan Bodewigaa739262011-11-02 16:13:41 +000096 in = new XZInputStream(inputStream);
Sebastian Bazleya0e435e2012-03-31 10:59:47 +000097 } else {
Stefan Bodewigaa739262011-11-02 16:13:41 +000098 in = new SingleXZInputStream(inputStream);
Sebastian Bazleya0e435e2012-03-31 10:59:47 +000099 }
Stefan Bodewigaa739262011-11-02 16:13:41 +0000100 }
101
Sebastian Bazley8862a482011-11-09 18:26:00 +0000102 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +0000103 public int read() throws IOException {
104 int ret = in.read();
105 count(ret == -1 ? -1 : 1);
106 return ret;
107 }
108
Sebastian Bazley8862a482011-11-09 18:26:00 +0000109 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +0000110 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 Bazley8862a482011-11-09 18:26:00 +0000116 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +0000117 public long skip(long n) throws IOException {
118 return in.skip(n);
119 }
120
Sebastian Bazley8862a482011-11-09 18:26:00 +0000121 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +0000122 public int available() throws IOException {
123 return in.available();
124 }
125
Sebastian Bazley8862a482011-11-09 18:26:00 +0000126 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +0000127 public void close() throws IOException {
128 in.close();
129 }
130}