Class XZInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
Use this to decompress regular standalone .xz files. This reads from its input stream until the end of the input or until an error occurs. This supports decompressing concatenated .xz files.
Typical use cases
Getting an input stream to decompress a .xz file:
InputStream infile = new FileInputStream("foo.xz");
XZInputStream inxz = new XZInputStream(infile);
It's important to keep in mind that decompressor memory usage depends on the settings used to compress the file. The worst-case memory usage of XZInputStream is currently 1.5 GiB. Still, very few files will require more than about 65 MiB because that's how much decompressing a file created with the highest preset level will need, and only a few people use settings other than the predefined presets.
It is possible to specify a memory usage limit for XZInputStream.
If decompression requires more memory than the specified limit,
MemoryLimitException will be thrown when reading from the stream.
For example, the following sets the memory usage limit to 100 MiB:
InputStream infile = new FileInputStream("foo.xz");
XZInputStream inxz = new XZInputStream(infile, 100 * 1024);
When uncompressed size is known beforehand
If you are decompressing complete files 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. (This advice seems to apply to
java.util.zip.GZIPInputStream too.)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new XZ decompressor without a memory usage limit.XZInputStream(InputStream in, int memoryLimit) Creates a new XZ decompressor with an optional memory usage limit.XZInputStream(InputStream in, int memoryLimit, boolean verifyCheck) Creates a new XZ decompressor with an optional memory usage limit and ability to disable verification of integrity checks.XZInputStream(InputStream in, int memoryLimit, boolean verifyCheck, ArrayCache arrayCache) Creates a new XZ decompressor with an optional memory usage limit and ability to disable verification of integrity checks.XZInputStream(InputStream in, int memoryLimit, ArrayCache arrayCache) Creates a new XZ decompressor with an optional memory usage limit.XZInputStream(InputStream in, ArrayCache arrayCache) Creates a new XZ decompressor without a memory usage limit. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of uncompressed bytes that can be read without blocking.voidclose()Closes the stream and callsin.close().voidclose(boolean closeInput) Closes the stream and optionally callsin.close().intread()Decompresses the next byte from this input stream.intread(byte[] buf, int off, int len) Decompresses into an array of bytes.Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
XZInputStream
Creates a new XZ decompressor 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 untilreadis called.- Parameters:
in- input stream from which XZ-compressed data is read- Throws:
XZFormatException- input is not in the XZ formatCorruptedInputException- XZ header CRC32 doesn't matchUnsupportedOptionsException- XZ header is valid but specifies options not supported by this implementationEOFException- less than 12 bytes of input was available frominIOException- may be thrown byin
-
XZInputStream
Creates a new XZ decompressor without a memory usage limit.This is identical to
XZInputStream(InputStream)except that this takes also thearrayCacheargument.- Parameters:
in- input stream from which XZ-compressed data is readarrayCache- cache to be used for allocating large arrays- Throws:
XZFormatException- input is not in the XZ formatCorruptedInputException- XZ header CRC32 doesn't matchUnsupportedOptionsException- XZ header is valid but specifies options not supported by this implementationEOFException- less than 12 bytes of input was available frominIOException- may be thrown byin- Since:
- 1.7
-
XZInputStream
Creates a new XZ decompressor with an optional memory usage limit.This is identical to
XZInputStream(InputStream)except that this takes also thememoryLimitargument.- Parameters:
in- input stream from which XZ-compressed data is readmemoryLimit- memory usage limit in kibibytes (KiB) or-1to impose no memory usage limit- Throws:
XZFormatException- input is not in the XZ formatCorruptedInputException- XZ header CRC32 doesn't matchUnsupportedOptionsException- XZ header is valid but specifies options not supported by this implementationEOFException- less than 12 bytes of input was available frominIOException- may be thrown byin
-
XZInputStream
Creates a new XZ decompressor with an optional memory usage limit.This is identical to
XZInputStream(InputStream)except that this takes also thememoryLimitandarrayCachearguments.- Parameters:
in- input stream from which XZ-compressed data is readmemoryLimit- memory usage limit in kibibytes (KiB) or-1to impose no memory usage limitarrayCache- cache to be used for allocating large arrays- Throws:
XZFormatException- input is not in the XZ formatCorruptedInputException- XZ header CRC32 doesn't matchUnsupportedOptionsException- XZ header is valid but specifies options not supported by this implementationEOFException- less than 12 bytes of input was available frominIOException- may be thrown byin- Since:
- 1.7
-
XZInputStream
Creates a new XZ decompressor with an optional memory usage limit and ability to disable verification of integrity checks.This is identical to
XZInputStream(InputStream,int)except that this takes also theverifyCheckargument.Note that integrity check verification should almost never be disabled. Possible reasons to disable integrity check verification:
- Trying to recover data from a corrupt .xz file.
- Speeding up decompression. This matters mostly with SHA-256 or with files that have compressed extremely well. It's recommended that integrity checking isn't disabled for performance reasons unless the file integrity is verified externally in some other way.
verifyCheckonly affects the integrity check of the actual compressed data. The CRC32 fields in the headers are always verified.- Parameters:
in- input stream from which XZ-compressed data is readmemoryLimit- memory usage limit in kibibytes (KiB) or-1to impose no memory usage limitverifyCheck- iftrue, the integrity checks will be verified; this should almost never be set tofalse- Throws:
XZFormatException- input is not in the XZ formatCorruptedInputException- XZ header CRC32 doesn't matchUnsupportedOptionsException- XZ header is valid but specifies options not supported by this implementationEOFException- less than 12 bytes of input was available frominIOException- may be thrown byin- Since:
- 1.6
-
XZInputStream
public XZInputStream(InputStream in, int memoryLimit, boolean verifyCheck, ArrayCache arrayCache) throws IOException Creates a new XZ decompressor with an optional memory usage limit and ability to disable verification of integrity checks.This is identical to
XZInputStream(InputStream,int,boolean)except that this takes also thearrayCacheargument.- Parameters:
in- input stream from which XZ-compressed data is readmemoryLimit- memory usage limit in kibibytes (KiB) or-1to impose no memory usage limitverifyCheck- iftrue, the integrity checks will be verified; this should almost never be set tofalsearrayCache- cache to be used for allocating large arrays- Throws:
XZFormatException- input is not in the XZ formatCorruptedInputException- XZ header CRC32 doesn't matchUnsupportedOptionsException- XZ header is valid but specifies options not supported by this implementationEOFException- less than 12 bytes of input was available frominIOException- may be thrown byin- Since:
- 1.7
-
-
Method Details
-
read
Decompresses the next byte from this input stream.Reading lots of data with
read()from this input stream may be inefficient. Wrap it inBufferedInputStreamif you need to read lots of data one byte at a time.- Specified by:
readin classInputStream- Returns:
- the next decompressed byte, or
-1to indicate the end of the compressed stream - Throws:
CorruptedInputExceptionUnsupportedOptionsExceptionMemoryLimitExceptionXZIOException- if the stream has been closedEOFException- compressed input is truncated or corruptIOException- may be thrown byin
-
read
Decompresses into an array of bytes.If
lenis zero, no bytes are read and0is returned. Otherwise this will try to decompresslenbytes of uncompressed data. Less thanlenbytes may be read only in the following situations:- The end of the compressed data was reached successfully.
- An error is detected after at least one but less
lenbytes have already been successfully decompressed. The next call with non-zerolenwill immediately throw the pending exception. - An exception is thrown.
- Overrides:
readin classInputStream- Parameters:
buf- target buffer for uncompressed dataoff- start offset inbuflen- maximum number of uncompressed bytes to read- Returns:
- number of bytes read, or
-1to indicate the end of the compressed stream - Throws:
CorruptedInputExceptionUnsupportedOptionsExceptionMemoryLimitExceptionXZIOException- if the stream has been closedEOFException- compressed input is truncated or corruptIOException- may be thrown byin
-
available
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,CorruptedInputExceptionmay get thrown before the number of bytes claimed to be available have been read from this input stream.- Overrides:
availablein classInputStream- Returns:
- the number of uncompressed bytes that can be read without blocking
- Throws:
IOException
-
close
Closes the stream and callsin.close(). If the stream was already closed, this does nothing.This is equivalent to
close(true).- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if thrown byin.close()
-
close
Closes the stream and optionally callsin.close(). If the stream was already closed, this does nothing. Ifclose(false)has been called, a further call ofclose(true)does nothing (it doesn't callin.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 usingArrayCache,close(false)can be useful to put the allocated arrays back to the cache without closing the underlyingInputStream.Note that if you successfully reach the end of the stream (
readreturns-1), the arrays are automatically put back to the cache by thatreadcall. In this situationclose(false)is redundant (but harmless).- Throws:
IOException- if thrown byin.close()- Since:
- 1.7
-