Package net.jpountz.lz4
Class LZ4Factory
java.lang.Object
net.jpountz.lz4.LZ4Factory
Entry point for the LZ4 API.
This class has 3 instances
- a
nativeinstance which is a JNI binding to the original LZ4 C implementation. - a
safe Javainstance which is a pure Java port of the original C library, - an
unsafe Javainstance which is a Java port using the unofficialUnsafeAPI.
Only the safe instance is guaranteed to work on your
JVM, as a consequence it is advised to use the fastestInstance() or
fastestJavaInstance() to pull a LZ4Factory instance.
All methods from this class are very costly, so you should get an instance
once, and then reuse it whenever possible. This is typically done by storing
a LZ4Factory instance in a static field.
-
Method Summary
Modifier and TypeMethodDescriptionDeprecated.Returns a blazing fastLZ4Compressor.Returns aLZ4FastDecompressorinstance.static LZ4FactoryReturns the fastest availableLZ4Factoryinstance.static LZ4FactoryReturns the fastest availableLZ4Factoryinstance which does not rely on JNI bindings.Returns aLZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently.highCompressor(int compressionLevel) Returns aLZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently.static voidPrints the fastest instance.static LZ4FactoryDeprecated.Never decompress untrusted inputs with this instance.static LZ4FactoryReturns aLZ4Factoryinstance that returns compressors and decompressors that are native bindings to the original C library.Returns aLZ4SafeDecompressorinstance.static LZ4FactoryReturns aLZ4Factoryinstance that returns compressors and decompressors that are written with Java's official API.toString()Deprecated.static LZ4FactoryDeprecated.Never decompress untrusted inputs with this instance.static LZ4FactoryDeprecated.Note: It is not yet clear which Unsafe-based implementations are secure.
-
Method Details
-
nativeInstance
Returns aLZ4Factoryinstance that returns compressors and decompressors that are native bindings to the original C library.Please note that this instance has some traps you should be aware of:
- Upon loading this instance, files will be written to the temporary directory of the system. Although these files are supposed to be deleted when the JVM exits, they might remain on systems that don't support removal of files being used such as Windows.
- The instance can only be loaded once per JVM. This can be a problem if your application uses multiple class loaders (such as most servlet containers): this instance will only be available to the children of the class loader which has loaded it. As a consequence, it is advised to either not use this instance in webapps or to put this library in the lib directory of your servlet container so that it is loaded by the system class loader.
- From lz4-java version 1.6.0, a
LZ4FastDecompressorinstance returned byfastDecompressor()of this instance is SLOWER than aLZ4SafeDecompressorinstance returned bysafeDecompressor(), due to a change in the original LZ4 C implementation. The corresponding C API function is deprecated. Hence use offastDecompressor()is deprecated for this instance.
- Returns:
- a
LZ4Factoryinstance that returns compressors and decompressors that are native bindings to the original C library
-
nativeInsecureInstance
Deprecated.Never decompress untrusted inputs with this instance. PrefernativeInstance().Insecure variant ofnativeInstance(). The JNI-basedLZ4FastDecompressoris not secure for untrusted inputs, sonativeInstance()will instead return the slower safe java implementation fromfastDecompressor(). If that implementation is too slow for you, it is recommended to move tosafeDecompressor(), which is actually faster even than the JNIfastDecompressor(). Only if that is not an option for you, and you can guarantee no untrusted inputs will be decompressed, should you use this method.- Returns:
- An insecure, JNI-backed LZ4Factory
-
safeInstance
Returns aLZ4Factoryinstance that returns compressors and decompressors that are written with Java's official API.- Returns:
- a
LZ4Factoryinstance that returns compressors and decompressors that are written with Java's official API.
-
unsafeInstance
Deprecated.Note: It is not yet clear which Unsafe-based implementations are secure. Out of caution, this method currently returns thesafeInstance(). In a future version, when security has been assessed, this method may return to Unsafe.Returns aLZ4Factoryinstance that returns compressors and decompressors that may useUnsafeto speed up compression and decompression.- Returns:
- a
LZ4Factoryinstance that returns compressors and decompressors that may useUnsafeto speed up compression and decompression.
-
unsafeInsecureInstance
Deprecated.Never decompress untrusted inputs with this instance. PreferunsafeInstance().Insecure variant ofunsafeInstance(). The Unsafe-basedLZ4FastDecompressoris not secure for untrusted inputs, sounsafeInstance()will instead return the slower safe java implementation fromfastDecompressor(). If that implementation is too slow for you, it is recommended to move tosafeDecompressor(). Only if that is not an option for you, and you can guarantee no untrusted inputs will be decompressed, should you use this method.- Returns:
- An insecure, Unsafe-backed LZ4Factory
-
fastestJavaInstance
Returns the fastest availableLZ4Factoryinstance which does not rely on JNI bindings. It first tries to load theunsafe instance, and then thesafe Java instanceif the JVM doesn't have a workingUnsafe.- Returns:
- the fastest available
LZ4Factoryinstance which does not rely on JNI bindings.
-
fastestInstance
Returns the fastest availableLZ4Factoryinstance. If the class loader is the system class loader and if thenative instanceloads successfully, then thenative instanceis returned, otherwise thefastest Java instanceis returned.Please read
javadocs of nativeInstance()before using this method.- Returns:
- the fastest available
LZ4Factoryinstance
-
fastCompressor
Returns a blazing fastLZ4Compressor.- Returns:
- a blazing fast
LZ4Compressor
-
highCompressor
Returns aLZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently.- Returns:
- a
LZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently.
-
highCompressor
Returns aLZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently. The compression level can be customized.For current implementations, the following is true about compression level:
- It should be in range [1, 17]
- A compression level higher than 17 would be treated as 17.
- A compression level lower than 1 would be treated as 9.
- Parameters:
compressionLevel- the compression level between [1, 17]; the higher the level, the higher the compression ratio- Returns:
- a
LZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently.
-
fastDecompressor
Returns aLZ4FastDecompressorinstance. Use of this method is deprecated for thenative instance.- Returns:
- a
LZ4FastDecompressorinstance - See Also:
-
safeDecompressor
Returns aLZ4SafeDecompressorinstance.- Returns:
- a
LZ4SafeDecompressorinstance
-
unknownSizeDecompressor
Deprecated.Returns aLZ4UnknownSizeDecompressorinstance.- Returns:
- a
LZ4UnknownSizeDecompressorinstance
-
decompressor
Deprecated.Returns aLZ4Decompressorinstance.- Returns:
- a
LZ4Decompressorinstance
-
main
Prints the fastest instance.- Parameters:
args- no argument required
-
toString
-
fastDecompressor()