Class CompressorStreamFactory
- All Implemented Interfaces:
CompressorStreamProvider
Factory to create Compressor[In|Out]putStreams from names. To add other implementations you should extend CompressorStreamFactory and override the appropriate methods (and call their implementation from super of course).
Example (Compressing a file):final OutputStream out = Files.newOutputStream(output.toPath()); CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.BZIP2, out); IOUtils.copy(Files.newInputStream(input.toPath()), cos); cos.close();Example (Decompressing a file):
final InputStream is = Files.newInputStream(input.toPath()); CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.BZIP2, is); IOUtils.copy(in, Files.newOutputStream(output.toPath())); in.close();
- This class is immutable
- provided that the deprecated method setDecompressConcatenated is not used.
- This class is thread-safe
- even if the deprecated method setDecompressConcatenated is used
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Constant (value "br") used to identify the BROTLI compression algorithm.static final String
Constant (value "bzip2") used to identify the BZIP2 compression algorithm.static final String
Constant (value "deflate") used to identify the Deflate compress method.static final String
Constant (value "deflate64") used to identify the Deflate64 compress method.static final String
Constant (value "gz") used to identify the GZIP compression algorithm.static final String
Constant (value "lz4-block") used to identify the block LZ4 compression method.static final String
Constant (value "lz4-framed") used to identify the frame LZ4 compression method.static final String
Constant (value "lzma") used to identify the LZMA compression method.static final String
Constant (value "pack200") used to identify the PACK200 compression algorithm.static final String
Constant (value "snappy-framed") used to identify the "framed" Snappy compression method.static final String
Constant (value "snappy-raw") used to identify the "raw" Snappy compression method.static final String
Constant (value "xz") used to identify the XZ compression method.static final String
Constant (value "z") used to identify the traditional UNIX compress method.static final String
Constant (value "zstd") used to identify the Zstandard compression algorithm. -
Constructor Summary
ConstructorDescriptionConstructs an instance with the decompress Concatenated option set to false.CompressorStreamFactory
(boolean decompressUntilEOF) Constructs an instance with the provided decompress Concatenated option.CompressorStreamFactory
(boolean decompressUntilEOF, int memoryLimitInKb) Constructs an instance with the provided decompress Concatenated option. -
Method Summary
Modifier and TypeMethodDescriptionCreates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream.createCompressorInputStream
(InputStream in, Set<String> compressorNames) Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream while limiting the detected type to the provided set of compressor names.createCompressorInputStream
(String name, InputStream in) Creates a compressor input stream from a compressor name and an input stream.createCompressorInputStream
(String name, InputStream in, boolean actualDecompressConcatenated) Creates a compressor input stream from a compressor name and an input stream.createCompressorOutputStream
(String name, OutputStream out) Creates a compressor output stream from a compressor name and an output stream.static String
detect
(InputStream inputStream) Detects the type of compressor stream.static SortedMap<String,
CompressorStreamProvider> Constructs a new sorted map from input stream provider names to provider objects.static SortedMap<String,
CompressorStreamProvider> Constructs a new sorted map from output stream provider names to provider objects.static String
static String
getBzip2()
static String
static String
static String
getGzip()
Gets all the input stream compressor names for this providerstatic String
static String
static String
getLzma()
Gets all the output stream compressor names for this providerstatic String
static CompressorStreamFactory
static String
static String
static String
getXz()
static String
getZ()
static String
void
setDecompressConcatenated
(boolean decompressConcatenated) Deprecated.
-
Field Details
-
BROTLI
Constant (value "br") used to identify the BROTLI compression algorithm.- Since:
- 1.14
- See Also:
-
BZIP2
Constant (value "bzip2") used to identify the BZIP2 compression algorithm.- Since:
- 1.1
- See Also:
-
GZIP
Constant (value "gz") used to identify the GZIP compression algorithm.- Since:
- 1.1
- See Also:
-
PACK200
Constant (value "pack200") used to identify the PACK200 compression algorithm.- Since:
- 1.3
- See Also:
-
XZ
Constant (value "xz") used to identify the XZ compression method.- Since:
- 1.4
- See Also:
-
LZMA
Constant (value "lzma") used to identify the LZMA compression method.- Since:
- 1.6
- See Also:
-
SNAPPY_FRAMED
Constant (value "snappy-framed") used to identify the "framed" Snappy compression method.- Since:
- 1.7
- See Also:
-
SNAPPY_RAW
Constant (value "snappy-raw") used to identify the "raw" Snappy compression method. Not supported as an output stream type.- Since:
- 1.7
- See Also:
-
Z
Constant (value "z") used to identify the traditional UNIX compress method. Not supported as an output stream type.- Since:
- 1.7
- See Also:
-
DEFLATE
Constant (value "deflate") used to identify the Deflate compress method.- Since:
- 1.9
- See Also:
-
DEFLATE64
Constant (value "deflate64") used to identify the Deflate64 compress method.- Since:
- 1.16
- See Also:
-
LZ4_BLOCK
Constant (value "lz4-block") used to identify the block LZ4 compression method.- Since:
- 1.14
- See Also:
-
LZ4_FRAMED
Constant (value "lz4-framed") used to identify the frame LZ4 compression method.- Since:
- 1.14
- See Also:
-
ZSTANDARD
Constant (value "zstd") used to identify the Zstandard compression algorithm. Not supported as an output stream type.- Since:
- 1.16
- See Also:
-
-
Constructor Details
-
CompressorStreamFactory
public CompressorStreamFactory()Constructs an instance with the decompress Concatenated option set to false. -
CompressorStreamFactory
Constructs an instance with the provided decompress Concatenated option.- Parameters:
decompressUntilEOF
- if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.- Since:
- 1.10
-
CompressorStreamFactory
Constructs an instance with the provided decompress Concatenated option.- Parameters:
decompressUntilEOF
- if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.memoryLimitInKb
- Some streams require allocation of potentially significant byte arrays/tables, and they can offer checks to prevent OOMs on corrupt files. Set the maximum allowed memory allocation in KBs.- Since:
- 1.14
-
-
Method Details
-
detect
Detects the type of compressor stream.- Parameters:
inputStream
- input stream- Returns:
- type of compressor stream detected
- Throws:
CompressorException
- if no compressor stream type was detected or if something else went wrongIllegalArgumentException
- if stream is null or does not support mark- Since:
- 1.14
-
findAvailableCompressorInputStreamProviders
public static SortedMap<String,CompressorStreamProvider> findAvailableCompressorInputStreamProviders()Constructs a new sorted map from input stream provider names to provider objects.The map returned by this method will have one entry for each provider for which support is available in the current Java virtual machine. If two or more supported provider have the same name then the resulting map will contain just one of them; which one it will contain is not specified.
The invocation of this method, and the subsequent use of the resulting map, may cause time-consuming disk or network I/O operations to occur. This method is provided for applications that need to enumerate all of the available providers, for example to allow user provider selection.
This method may return different results at different times if new providers are dynamically made available to the current Java virtual machine.
- Returns:
- An immutable, map from names to provider objects
- Since:
- 1.13
-
findAvailableCompressorOutputStreamProviders
public static SortedMap<String,CompressorStreamProvider> findAvailableCompressorOutputStreamProviders()Constructs a new sorted map from output stream provider names to provider objects.The map returned by this method will have one entry for each provider for which support is available in the current Java virtual machine. If two or more supported provider have the same name then the resulting map will contain just one of them; which one it will contain is not specified.
The invocation of this method, and the subsequent use of the resulting map, may cause time-consuming disk or network I/O operations to occur. This method is provided for applications that need to enumerate all of the available providers, for example to allow user provider selection.
This method may return different results at different times if new providers are dynamically made available to the current Java virtual machine.
- Returns:
- An immutable, map from names to provider objects
- Since:
- 1.13
-
getBrotli
-
getBzip2
-
getDeflate
-
getDeflate64
- Returns:
- the constant
DEFLATE64
- Since:
- 1.16
-
getGzip
-
getLZ4Block
-
getLZ4Framed
-
getLzma
-
getPack200
-
getSingleton
-
getSnappyFramed
-
getSnappyRaw
-
getXz
-
getZ
-
getZstandard
-
createCompressorInputStream
Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream. The InputStream must support marks, like BufferedInputStream.- Parameters:
in
- the input stream- Returns:
- the compressor input stream
- Throws:
CompressorException
- if the compressor name is not knownIllegalArgumentException
- if the stream is null or does not support mark- Since:
- 1.1
-
createCompressorInputStream
public CompressorInputStream createCompressorInputStream(InputStream in, Set<String> compressorNames) throws CompressorException Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream while limiting the detected type to the provided set of compressor names. The InputStream must support marks, like BufferedInputStream.- Parameters:
in
- the input streamcompressorNames
- compressor names to limit autodetection- Returns:
- the compressor input stream
- Throws:
CompressorException
- if the autodetected compressor is not in the provided set of compressor namesIllegalArgumentException
- if the stream is null or does not support mark- Since:
- 1.25.0
-
createCompressorInputStream
public CompressorInputStream createCompressorInputStream(String name, InputStream in) throws CompressorException Creates a compressor input stream from a compressor name and an input stream.- Parameters:
name
- of the compressor, i.e. "gz", "bzip2", "xz", "lzma", "pack200", "snappy-raw", "snappy-framed", "z", "lz4-block", "lz4-framed", "zstd", "deflate64" or "deflate"in
- the input stream- Returns:
- compressor input stream
- Throws:
CompressorException
- if the compressor name is not known or not available, or if there's an IOException or MemoryLimitException thrown during initializationIllegalArgumentException
- if the name or input stream is null
-
createCompressorInputStream
public CompressorInputStream createCompressorInputStream(String name, InputStream in, boolean actualDecompressConcatenated) throws CompressorException Description copied from interface:CompressorStreamProvider
Creates a compressor input stream from a compressor name and an input stream.- Specified by:
createCompressorInputStream
in interfaceCompressorStreamProvider
- Parameters:
name
- of the compressor, i.e. "gz", "bzip2", "xz", "lzma", "pack200", "snappy-raw", "snappy-framed", "z" or "deflate"in
- the input streamactualDecompressConcatenated
- if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and XZ formats only.- Returns:
- compressor input stream
- Throws:
CompressorException
- if the compressor name is not known
-
createCompressorOutputStream
public CompressorOutputStream<?> createCompressorOutputStream(String name, OutputStream out) throws CompressorException Creates a compressor output stream from a compressor name and an output stream.- Specified by:
createCompressorOutputStream
in interfaceCompressorStreamProvider
- Parameters:
name
- the compressor name, i.e. "gz", "bzip2", "xz", "pack200", "snappy-framed", "lz4-block", "lz4-framed", "zstd" or "deflate"out
- the output stream- Returns:
- the compressor output stream
- Throws:
CompressorException
- if the archiver name is not knownIllegalArgumentException
- if the archiver name or stream is null
-
getCompressorInputStreamProviders
-
getCompressorOutputStreamProviders
-
getDecompressUntilEOF
-
getInputStreamCompressorNames
Description copied from interface:CompressorStreamProvider
Gets all the input stream compressor names for this provider- Specified by:
getInputStreamCompressorNames
in interfaceCompressorStreamProvider
- Returns:
- all the input compressor names for this provider
-
getOutputStreamCompressorNames
Description copied from interface:CompressorStreamProvider
Gets all the output stream compressor names for this provider- Specified by:
getOutputStreamCompressorNames
in interfaceCompressorStreamProvider
- Returns:
- all the output compressor names for this provider
-
setDecompressConcatenated
Deprecated.1.10 use theCompressorStreamFactory(boolean)
constructor insteadSets whether to decompress the full input or only the first stream in formats supporting multiple concatenated input streams.This setting applies to the gzip, bzip2 and XZ formats only.
- Parameters:
decompressConcatenated
- if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream- Throws:
IllegalStateException
- if the constructorCompressorStreamFactory(boolean)
was used to create the factory- Since:
- 1.5
-
CompressorStreamFactory(boolean)
constructor instead