/* * SingleXZInputStream * * Author: Lasse Collin * * This file has been put into the public domain. * You can do whatever you want with this file. */ package org.tukaani.xz; import java.io.InputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.EOFException; import org.tukaani.xz.common.DecoderUtil; import org.tukaani.xz.common.StreamFlags; import org.tukaani.xz.index.IndexHash; import org.tukaani.xz.check.Check; /** * Decompresses exactly one XZ Stream in streamed mode (no seeking). * The decompression stops after the first XZ Stream has been decompressed, * and the read position in the input stream is left at the first byte * after the end of the XZ Stream. This can be useful when XZ data has * been stored inside some other file format or protocol. *

* Unless you know what you are doing, don't use this class to decompress * standalone .xz files. For that purpose, use XZInputStream. * *

When uncompressed size is known beforehand

*

* If you are decompressing complete XZ streams and your application knows * exactly how much uncompressed data there should be, it is good to try * reading one more byte by calling read() and checking * that it returns -1. This way the decompressor will parse the * file footers and verify the integrity checks, giving the caller more * confidence that the uncompressed data is valid. * * @see XZInputStream */ public class SingleXZInputStream extends InputStream { private InputStream in; private final ArrayCache arrayCache; private final int memoryLimit; private final StreamFlags streamHeaderFlags; private final Check check; private final boolean verifyCheck; private BlockInputStream blockDecoder = null; private final IndexHash indexHash = new IndexHash(); private boolean endReached = false; private IOException exception = null; private final byte[] tempBuf = new byte[1]; /** * Reads the Stream Header into a buffer. * This is a helper function for the constructors. */ private static byte[] readStreamHeader(InputStream in) throws IOException { byte[] streamHeader = new byte[DecoderUtil.STREAM_HEADER_SIZE]; new DataInputStream(in).readFully(streamHeader); return streamHeader; } /** * Creates a new XZ decompressor that decompresses exactly one * XZ Stream from in without a memory usage limit. *

* This constructor reads and parses the XZ Stream Header (12 bytes) * from in. The header of the first Block is not read * until read is called. * * @param in input stream from which XZ-compressed * data is read * * @throws XZFormatException * input is not in the XZ format * * @throws CorruptedInputException * XZ header CRC32 doesn't match * * @throws UnsupportedOptionsException * XZ header is valid but specifies options * not supported by this implementation * * @throws EOFException * less than 12 bytes of input was available * from in * * @throws IOException may be thrown by in */ public SingleXZInputStream(InputStream in) throws IOException { this(in, -1); } /** * Creates a new XZ decompressor that decompresses exactly one * XZ Stream from in without a memory usage limit. *

* This is identical to SingleXZInputStream(InputStream) * except that this also takes the arrayCache argument. * * @param in input stream from which XZ-compressed * data is read * * @param arrayCache cache to be used for allocating large arrays * * @throws XZFormatException * input is not in the XZ format * * @throws CorruptedInputException * XZ header CRC32 doesn't match * * @throws UnsupportedOptionsException * XZ header is valid but specifies options * not supported by this implementation * * @throws EOFException * less than 12 bytes of input was available * from in * * @throws IOException may be thrown by in * * @since 1.7 */ public SingleXZInputStream(InputStream in, ArrayCache arrayCache) throws IOException { this(in, -1, arrayCache); } /** * Creates a new XZ decompressor that decompresses exactly one * XZ Stream from in with an optional memory usage limit. *

* This is identical to SingleXZInputStream(InputStream) * except that this also takes the memoryLimit argument. * * @param in input stream from which XZ-compressed * data is read * * @param memoryLimit memory usage limit in kibibytes (KiB) * or -1 to impose no * memory usage limit * * @throws XZFormatException * input is not in the XZ format * * @throws CorruptedInputException * XZ header CRC32 doesn't match * * @throws UnsupportedOptionsException * XZ header is valid but specifies options * not supported by this implementation * * @throws EOFException * less than 12 bytes of input was available * from in * * @throws IOException may be thrown by in */ public SingleXZInputStream(InputStream in, int memoryLimit) throws IOException { this(in, memoryLimit, true); } /** * Creates a new XZ decompressor that decompresses exactly one * XZ Stream from in with an optional memory usage limit. *

* This is identical to SingleXZInputStream(InputStream) * except that this also takes the memoryLimit and * arrayCache arguments. * * @param in input stream from which XZ-compressed * data is read * * @param memoryLimit memory usage limit in kibibytes (KiB) * or -1 to impose no * memory usage limit * * @param arrayCache cache to be used for allocating large arrays * * @throws XZFormatException * input is not in the XZ format * * @throws CorruptedInputException * XZ header CRC32 doesn't match * * @throws UnsupportedOptionsException * XZ header is valid but specifies options * not supported by this implementation * * @throws EOFException * less than 12 bytes of input was available * from in * * @throws IOException may be thrown by in * * @since 1.7 */ public SingleXZInputStream(InputStream in, int memoryLimit, ArrayCache arrayCache) throws IOException { this(in, memoryLimit, true, arrayCache); } /** * Creates a new XZ decompressor that decompresses exactly one * XZ Stream from in with an optional memory usage limit * and ability to disable verification of integrity checks. *

* This is identical to SingleXZInputStream(InputStream,int) * except that this also takes the verifyCheck argument. *

* Note that integrity check verification should almost never be disabled. * Possible reasons to disable integrity check verification: *

*

* verifyCheck only affects the integrity check of * the actual compressed data. The CRC32 fields in the headers * are always verified. * * @param in input stream from which XZ-compressed * data is read * * @param memoryLimit memory usage limit in kibibytes (KiB) * or -1 to impose no * memory usage limit * * @param verifyCheck if true, the integrity checks * will be verified; this should almost never * be set to false * * @throws XZFormatException * input is not in the XZ format * * @throws CorruptedInputException * XZ header CRC32 doesn't match * * @throws UnsupportedOptionsException * XZ header is valid but specifies options * not supported by this implementation * * @throws EOFException * less than 12 bytes of input was available * from in * * @throws IOException may be thrown by in * * @since 1.6 */ public SingleXZInputStream(InputStream in, int memoryLimit, boolean verifyCheck) throws IOException { this(in, memoryLimit, verifyCheck, ArrayCache.getDefaultCache()); } /** * Creates a new XZ decompressor that decompresses exactly one * XZ Stream from in with an optional memory usage limit * and ability to disable verification of integrity checks. *

* This is identical to * SingleXZInputStream(InputStream,int,boolean) * except that this also takes the arrayCache argument. * * @param in input stream from which XZ-compressed * data is read * * @param memoryLimit memory usage limit in kibibytes (KiB) * or -1 to impose no * memory usage limit * * @param verifyCheck if true, the integrity checks * will be verified; this should almost never * be set to false * * @param arrayCache cache to be used for allocating large arrays * * @throws XZFormatException * input is not in the XZ format * * @throws CorruptedInputException * XZ header CRC32 doesn't match * * @throws UnsupportedOptionsException * XZ header is valid but specifies options * not supported by this implementation * * @throws EOFException * less than 12 bytes of input was available * from in * * @throws IOException may be thrown by in * * @since 1.7 */ public SingleXZInputStream(InputStream in, int memoryLimit, boolean verifyCheck, ArrayCache arrayCache) throws IOException { this(in, memoryLimit, verifyCheck, readStreamHeader(in), arrayCache); } SingleXZInputStream(InputStream in, int memoryLimit, boolean verifyCheck, byte[] streamHeader, ArrayCache arrayCache) throws IOException { this.arrayCache = arrayCache; this.in = in; this.memoryLimit = memoryLimit; this.verifyCheck = verifyCheck; streamHeaderFlags = DecoderUtil.decodeStreamHeader(streamHeader); check = Check.getInstance(streamHeaderFlags.checkType); } /** * Gets the ID of the integrity check used in this XZ Stream. * * @return the Check ID specified in the XZ Stream Header */ public int getCheckType() { return streamHeaderFlags.checkType; } /** * Gets the name of the integrity check used in this XZ Stream. * * @return the name of the check specified in the XZ Stream Header */ public String getCheckName() { return check.getName(); } /** * Decompresses the next byte from this input stream. *

* Reading lots of data with read() from this input stream * may be inefficient. Wrap it in {@link java.io.BufferedInputStream} * if you need to read lots of data one byte at a time. * * @return the next decompressed byte, or -1 * to indicate the end of the compressed stream * * @throws CorruptedInputException * @throws UnsupportedOptionsException * @throws MemoryLimitException * * @throws XZIOException if the stream has been closed * * @throws EOFException * compressed input is truncated or corrupt * * @throws IOException may be thrown by in */ public int read() throws IOException { return read(tempBuf, 0, 1) == -1 ? -1 : (tempBuf[0] & 0xFF); } /** * Decompresses into an array of bytes. *

* If len is zero, no bytes are read and 0 * is returned. Otherwise this will try to decompress len * bytes of uncompressed data. Less than len bytes may * be read only in the following situations: *

* * @param buf target buffer for uncompressed data * @param off start offset in buf * @param len maximum number of uncompressed bytes to read * * @return number of bytes read, or -1 to indicate * the end of the compressed stream * * @throws CorruptedInputException * @throws UnsupportedOptionsException * @throws MemoryLimitException * * @throws XZIOException if the stream has been closed * * @throws EOFException * compressed input is truncated or corrupt * * @throws IOException may be thrown by in */ public int read(byte[] buf, int off, int len) throws IOException { if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length) throw new IndexOutOfBoundsException(); if (len == 0) return 0; if (in == null) throw new XZIOException("Stream closed"); if (exception != null) throw exception; if (endReached) return -1; int size = 0; try { while (len > 0) { if (blockDecoder == null) { try { blockDecoder = new BlockInputStream( in, check, verifyCheck, memoryLimit, -1, -1, arrayCache); } catch (IndexIndicatorException e) { indexHash.validate(in); validateStreamFooter(); endReached = true; return size > 0 ? size : -1; } } int ret = blockDecoder.read(buf, off, len); if (ret > 0) { size += ret; off += ret; len -= ret; } else if (ret == -1) { indexHash.add(blockDecoder.getUnpaddedSize(), blockDecoder.getUncompressedSize()); blockDecoder = null; } } } catch (IOException e) { exception = e; if (size == 0) throw e; } return size; } private void validateStreamFooter() throws IOException { byte[] buf = new byte[DecoderUtil.STREAM_HEADER_SIZE]; new DataInputStream(in).readFully(buf); StreamFlags streamFooterFlags = DecoderUtil.decodeStreamFooter(buf); if (!DecoderUtil.areStreamFlagsEqual(streamHeaderFlags, streamFooterFlags) || indexHash.getIndexSize() != streamFooterFlags.backwardSize) throw new CorruptedInputException( "XZ Stream Footer does not match Stream Header"); } /** * Returns the number of uncompressed bytes that can be read * without blocking. The value is returned with an assumption * that the compressed input data will be valid. If the compressed * data is corrupt, CorruptedInputException may get * thrown before the number of bytes claimed to be available have * been read from this input stream. * * @return the number of uncompressed bytes that can be read * without blocking */ public int available() throws IOException { if (in == null) throw new XZIOException("Stream closed"); if (exception != null) throw exception; return blockDecoder == null ? 0 : blockDecoder.available(); } /** * Closes the stream and calls in.close(). * If the stream was already closed, this does nothing. *

* This is equivalent to close(true). * * @throws IOException if thrown by in.close() */ public void close() throws IOException { close(true); } /** * Closes the stream and optionally calls in.close(). * If the stream was already closed, this does nothing. * If close(false) has been called, a further * call of close(true) does nothing (it doesn't call * in.close()). *

* If you don't want to close the underlying InputStream, * there is usually no need to worry about closing this stream either; * it's fine to do nothing and let the garbage collector handle it. * However, if you are using {@link ArrayCache}, close(false) * can be useful to put the allocated arrays back to the cache without * closing the underlying InputStream. *

* Note that if you successfully reach the end of the stream * (read returns -1), the arrays are * automatically put back to the cache by that read call. In * this situation close(false) is redundant (but harmless). * * @throws IOException if thrown by in.close() * * @since 1.7 */ public void close(boolean closeInput) throws IOException { if (in != null) { if (blockDecoder != null) { blockDecoder.close(); blockDecoder = null; } try { if (closeInput) in.close(); } finally { in = null; } } } }