- java.lang.Object
-
- kala.compress.compressors.CompressorStreamFactory
-
- All Implemented Interfaces:
CompressorStreamProvider
public class CompressorStreamFactory extends java.lang.Object implements CompressorStreamProvider
Creates a 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();
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCompressorStreamFactory.BuiltinCompressorThis is an internal class and should not be used directly.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Set<java.lang.String>ALL_NAMESstatic java.lang.StringBROTLIConstant (value "br") used to identify the BROTLI compression algorithm.private static CompressorStreamFactory.BuiltinCompressor[]BUILTIN_COMPRESSORSstatic java.lang.StringBZIP2Constant (value "bzip2") used to identify the BZIP2 compression algorithm.private java.util.SortedMap<java.lang.String,CompressorStreamProvider>compressorInputStreamProvidersprivate java.util.SortedMap<java.lang.String,CompressorStreamProvider>compressorOutputStreamProvidersprivate booleandecompressConcatenatedIf true, decompress until the end of the input.static CompressorStreamFactoryDEFAULTstatic java.lang.StringDEFLATEConstant (value "deflate") used to identify the Deflate compress method.static java.lang.StringDEFLATE64Constant (value "deflate64") used to identify the Deflate64 compress method.static java.lang.StringGZIPConstant (value "gz") used to identify the GZIP compression algorithm.private java.util.Set<java.lang.String>inputStreamCompressorNamesstatic java.lang.StringLZ4_BLOCKConstant (value "lz4-block") used to identify the block LZ4 compression method.static java.lang.StringLZ4_FRAMEDConstant (value "lz4-framed") used to identify the frame LZ4 compression method.static java.lang.StringLZMAConstant (value "lzma") used to identify the LZMA compression method.private intmemoryLimitInKbprivate static java.util.Set<java.lang.String>OUTPUT_NAMESprivate java.util.Set<java.lang.String>outputStreamCompressorNamesstatic java.lang.StringPACK200Constant (value "pack200") used to identify the PACK200 compression algorithm.static java.lang.StringSNAPPY_FRAMEDConstant (value "snappy-framed") used to identify the "framed" Snappy compression method.static java.lang.StringSNAPPY_RAWConstant (value "snappy-raw") used to identify the "raw" Snappy compression method.static java.lang.StringXZConstant (value "xz") used to identify the XZ compression method.static java.lang.StringZConstant (value "z") used to identify the traditional UNIX compress method.static java.lang.StringZSTANDARDConstant (value "zstd") used to identify the Zstandard compression algorithm.
-
Constructor Summary
Constructors Constructor Description CompressorStreamFactory()Constructs 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompressorInputStreamcreateCompressorInputStream(java.io.InputStream in)Creates a compressor input stream from an input stream, auto-detecting the compressor type from the first few bytes of the stream.CompressorInputStreamcreateCompressorInputStream(java.io.InputStream in, java.util.Set<java.lang.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.CompressorInputStreamcreateCompressorInputStream(java.lang.String name, java.io.InputStream in)Creates a compressor input stream from a compressor name and an input stream.CompressorInputStreamcreateCompressorInputStream(java.lang.String name, java.io.InputStream in, boolean actualDecompressConcatenated)Creates a compressor input stream from a compressor name and an input stream.CompressorOutputStream<?>createCompressorOutputStream(java.lang.String name, java.io.OutputStream out)Creates a compressor output stream from a compressor name and an output stream.static java.lang.Stringdetect(java.io.InputStream inputStream)Detects the type of compressor stream.(package private) static java.lang.Stringdetect(java.io.InputStream inputStream, java.util.Set<java.lang.String> compressorNames)Detects the type of compressor stream while limiting the type to the provided set of compressor names.booleangetDecompressConcatenated()java.util.Set<java.lang.String>getInputStreamCompressorNames()Gets all the input stream compressor names for this providerjava.util.Set<java.lang.String>getOutputStreamCompressorNames()Gets all the output stream compressor names for this providerprivate static java.lang.StringtoKey(java.lang.String name)CompressorStreamFactorywithInstalledProviders()CompressorStreamFactorywithInstalledProviders(java.lang.ClassLoader classLoader)CompressorStreamFactorywithProviders(java.lang.Iterable<? extends CompressorStreamProvider> providers)
-
-
-
Field Detail
-
BROTLI
public static final java.lang.String BROTLI
Constant (value "br") used to identify the BROTLI compression algorithm.- Since:
- 1.14
- See Also:
- Constant Field Values
-
BZIP2
public static final java.lang.String BZIP2
Constant (value "bzip2") used to identify the BZIP2 compression algorithm.- Since:
- 1.1
- See Also:
- Constant Field Values
-
GZIP
public static final java.lang.String GZIP
Constant (value "gz") used to identify the GZIP compression algorithm.- Since:
- 1.1
- See Also:
- Constant Field Values
-
PACK200
public static final java.lang.String PACK200
Constant (value "pack200") used to identify the PACK200 compression algorithm.- Since:
- 1.3
- See Also:
- Constant Field Values
-
XZ
public static final java.lang.String XZ
Constant (value "xz") used to identify the XZ compression method.- Since:
- 1.4
- See Also:
- Constant Field Values
-
LZMA
public static final java.lang.String LZMA
Constant (value "lzma") used to identify the LZMA compression method.- Since:
- 1.6
- See Also:
- Constant Field Values
-
SNAPPY_FRAMED
public static final java.lang.String SNAPPY_FRAMED
Constant (value "snappy-framed") used to identify the "framed" Snappy compression method.- Since:
- 1.7
- See Also:
- Constant Field Values
-
SNAPPY_RAW
public static final java.lang.String 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:
- Constant Field Values
-
Z
public static final java.lang.String Z
Constant (value "z") used to identify the traditional UNIX compress method. Not supported as an output stream type.- Since:
- 1.7
- See Also:
- Constant Field Values
-
DEFLATE
public static final java.lang.String DEFLATE
Constant (value "deflate") used to identify the Deflate compress method.- Since:
- 1.9
- See Also:
- Constant Field Values
-
DEFLATE64
public static final java.lang.String DEFLATE64
Constant (value "deflate64") used to identify the Deflate64 compress method.- Since:
- 1.16
- See Also:
- Constant Field Values
-
LZ4_BLOCK
public static final java.lang.String LZ4_BLOCK
Constant (value "lz4-block") used to identify the block LZ4 compression method.- Since:
- 1.14
- See Also:
- Constant Field Values
-
LZ4_FRAMED
public static final java.lang.String LZ4_FRAMED
Constant (value "lz4-framed") used to identify the frame LZ4 compression method.- Since:
- 1.14
- See Also:
- Constant Field Values
-
ZSTANDARD
public static final java.lang.String ZSTANDARD
Constant (value "zstd") used to identify the Zstandard compression algorithm. Not supported as an output stream type.- Since:
- 1.16
- See Also:
- Constant Field Values
-
BUILTIN_COMPRESSORS
private static final CompressorStreamFactory.BuiltinCompressor[] BUILTIN_COMPRESSORS
-
ALL_NAMES
private static final java.util.Set<java.lang.String> ALL_NAMES
-
OUTPUT_NAMES
private static final java.util.Set<java.lang.String> OUTPUT_NAMES
-
DEFAULT
public static final CompressorStreamFactory DEFAULT
- Since:
- 1.27.1-0
-
compressorInputStreamProviders
private final java.util.SortedMap<java.lang.String,CompressorStreamProvider> compressorInputStreamProviders
-
compressorOutputStreamProviders
private final java.util.SortedMap<java.lang.String,CompressorStreamProvider> compressorOutputStreamProviders
-
inputStreamCompressorNames
private final java.util.Set<java.lang.String> inputStreamCompressorNames
-
outputStreamCompressorNames
private final java.util.Set<java.lang.String> outputStreamCompressorNames
-
decompressConcatenated
private final boolean 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
-
memoryLimitInKb
private final int memoryLimitInKb
-
-
Constructor Detail
-
CompressorStreamFactory
public CompressorStreamFactory()
Constructs an instance with the decompress Concatenated option set to false.
-
CompressorStreamFactory
public CompressorStreamFactory(boolean decompressUntilEOF)
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
public CompressorStreamFactory(boolean decompressUntilEOF, int memoryLimitInKb)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 Detail
-
detect
public static java.lang.String detect(java.io.InputStream inputStream) throws CompressorExceptionDetects 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 wrongjava.lang.IllegalArgumentException- if stream is null or does not support mark- Since:
- 1.14
-
detect
static java.lang.String detect(java.io.InputStream inputStream, java.util.Set<java.lang.String> compressorNames) throws CompressorExceptionDetects the type of compressor stream while limiting the type to the provided set of compressor names.- Parameters:
inputStream- input streamcompressorNames- compressor names to limit autodetection- Returns:
- type of compressor stream detected
- Throws:
CompressorException- if no compressor stream type was detected or if something else went wrongjava.lang.IllegalArgumentException- if stream is null or does not support mark
-
toKey
private static java.lang.String toKey(java.lang.String name)
-
createCompressorInputStream
public CompressorInputStream createCompressorInputStream(java.io.InputStream in) throws CompressorException
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 knownjava.lang.IllegalArgumentException- if the stream is null or does not support mark- Since:
- 1.1
-
createCompressorInputStream
public CompressorInputStream createCompressorInputStream(java.io.InputStream in, java.util.Set<java.lang.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 namesjava.lang.IllegalArgumentException- if the stream is null or does not support mark- Since:
- 1.25.0
-
createCompressorInputStream
public CompressorInputStream createCompressorInputStream(java.lang.String name, java.io.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 initializationjava.lang.IllegalArgumentException- if the name or input stream is null
-
createCompressorInputStream
public CompressorInputStream createCompressorInputStream(java.lang.String name, java.io.InputStream in, boolean actualDecompressConcatenated) throws CompressorException
Description copied from interface:CompressorStreamProviderCreates a compressor input stream from a compressor name and an input stream.- Specified by:
createCompressorInputStreamin interfaceCompressorStreamProvider- Parameters:
name- of the compressor, i.e. , , , , , , , orin- 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(java.lang.String name, java.io.OutputStream out) throws CompressorException
Creates a compressor output stream from a compressor name and an output stream.- Specified by:
createCompressorOutputStreamin 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 knownjava.lang.IllegalArgumentException- if the archiver name or stream is null
-
getDecompressConcatenated
public boolean getDecompressConcatenated()
- Since:
- 1.27.1-0
-
getInputStreamCompressorNames
public java.util.Set<java.lang.String> getInputStreamCompressorNames()
Description copied from interface:CompressorStreamProviderGets all the input stream compressor names for this provider- Specified by:
getInputStreamCompressorNamesin interfaceCompressorStreamProvider- Returns:
- all the input compressor names for this provider
-
getOutputStreamCompressorNames
public java.util.Set<java.lang.String> getOutputStreamCompressorNames()
Description copied from interface:CompressorStreamProviderGets all the output stream compressor names for this provider- Specified by:
getOutputStreamCompressorNamesin interfaceCompressorStreamProvider- Returns:
- all the output compressor names for this provider
-
withInstalledProviders
@Experimental public CompressorStreamFactory withInstalledProviders()
- Since:
- 1.27.1-0
-
withInstalledProviders
@Experimental public CompressorStreamFactory withInstalledProviders(java.lang.ClassLoader classLoader)
- Since:
- 1.27.1-0
-
withProviders
@Experimental public CompressorStreamFactory withProviders(java.lang.Iterable<? extends CompressorStreamProvider> providers)
- Since:
- 1.27.1-0
-
-