Package net.jpountz.lz4
Class LZ4Factory
- java.lang.Object
-
- net.jpountz.lz4.LZ4Factory
-
public final class LZ4Factory extends java.lang.ObjectEntry 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 instanceis guaranteed to work on your JVM, as a consequence it is advised to use thefastestInstance()orfastestJavaInstance()to pull aLZ4Factoryinstance.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
LZ4Factoryinstance in a static field. - a
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description LZ4Decompressordecompressor()Deprecated.LZ4CompressorfastCompressor()Returns a blazing fastLZ4Compressor.LZ4FastDecompressorfastDecompressor()Returns aLZ4FastDecompressorinstance.static LZ4FactoryfastestInstance()Returns the fastest availableLZ4Factoryinstance.static LZ4FactoryfastestJavaInstance()Returns the fastest availableLZ4Factoryinstance which does not rely on JNI bindings.LZ4CompressorhighCompressor()Returns aLZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently.LZ4CompressorhighCompressor(int compressionLevel)Returns aLZ4Compressorwhich requires more memory thanfastCompressor()and is slower but compresses more efficiently.static voidmain(java.lang.String[] args)Prints the fastest instance.static LZ4FactorynativeInsecureInstance()Deprecated.Never decompress untrusted inputs with this instance.static LZ4FactorynativeInstance()Returns aLZ4Factoryinstance that returns compressors and decompressors that are native bindings to the original C library.LZ4SafeDecompressorsafeDecompressor()Returns aLZ4SafeDecompressorinstance.static LZ4FactorysafeInstance()Returns aLZ4Factoryinstance that returns compressors and decompressors that are written with Java's official API.java.lang.StringtoString()LZ4UnknownSizeDecompressorunknownSizeDecompressor()Deprecated.static LZ4FactoryunsafeInsecureInstance()Deprecated.Never decompress untrusted inputs with this instance.static LZ4FactoryunsafeInstance()Deprecated.Note: It is not yet clear which Unsafe-based implementations are secure.
-
-
-
Method Detail
-
nativeInstance
public static LZ4Factory 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 public static LZ4Factory 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
public static 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 public static LZ4Factory 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 public static LZ4Factory 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
public static 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
public static LZ4Factory 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
public LZ4Compressor fastCompressor()
Returns a blazing fastLZ4Compressor.- Returns:
- a blazing fast
LZ4Compressor
-
highCompressor
public 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
public LZ4Compressor highCompressor(int compressionLevel)
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
public LZ4FastDecompressor fastDecompressor()
Returns aLZ4FastDecompressorinstance. Use of this method is deprecated for thenative instance.- Returns:
- a
LZ4FastDecompressorinstance - See Also:
nativeInstance()
-
safeDecompressor
public LZ4SafeDecompressor safeDecompressor()
Returns aLZ4SafeDecompressorinstance.- Returns:
- a
LZ4SafeDecompressorinstance
-
unknownSizeDecompressor
public LZ4UnknownSizeDecompressor unknownSizeDecompressor()
Deprecated.Returns aLZ4UnknownSizeDecompressorinstance.- Returns:
- a
LZ4UnknownSizeDecompressorinstance
-
decompressor
public LZ4Decompressor decompressor()
Deprecated.Returns aLZ4Decompressorinstance.- Returns:
- a
LZ4Decompressorinstance
-
main
public static void main(java.lang.String[] args)
Prints the fastest instance.- Parameters:
args- no argument required
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-