net.jpountz.xxhash
Class XXHashFactory
- java.lang.Object
-
- net.jpountz.xxhash.XXHashFactory
-
public final class XXHashFactory extends java.lang.Object
Entry point to getXXHash32
andStreamingXXHash32
instances.This class has 3 instances
- a
native
instance which is a JNI binding to the original LZ4 C implementation. - a
safe Java
instance which is a pure Java port of the original C library, - an
unsafe Java
instance which is a Java port using the unofficialUnsafe
API.
Only the
safe instance
is guaranteed to work on your JVM, as a consequence it is advised to use thefastestInstance()
orfastestJavaInstance()
to pull aXXHashFactory
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
XXHashFactory
instance in a static field. - a
-
-
Method Summary
Methods Modifier and Type Method and Description static XXHashFactory
fastestInstance()
Returns the fastest availableXXHashFactory
instance.static XXHashFactory
fastestJavaInstance()
Returns the fastest availableXXHashFactory
instance which does not rely on JNI bindings.XXHash32
hash32()
Returns aXXHash32
instance.XXHash64
hash64()
Returns aXXHash64
instance.static void
main(java.lang.String[] args)
Prints the fastest instance.static XXHashFactory
nativeInstance()
Returns aXXHashFactory
that returnsXXHash32
instances that are native bindings to the original C API.StreamingXXHash32
newStreamingHash32(int seed)
Return a newStreamingXXHash32
instance.StreamingXXHash64
newStreamingHash64(long seed)
Return a newStreamingXXHash64
instance.static XXHashFactory
safeInstance()
Returns aXXHashFactory
that returnsXXHash32
instances that are written with Java's official API.java.lang.String
toString()
static XXHashFactory
unsafeInstance()
-
-
-
Method Detail
-
nativeInstance
public static XXHashFactory nativeInstance()
Returns aXXHashFactory
that returnsXXHash32
instances that are native bindings to the original C API.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.
- Returns:
-
a
XXHashFactory
that returnsXXHash32
instances that are native bindings to the original C API.
-
safeInstance
public static XXHashFactory safeInstance()
Returns aXXHashFactory
that returnsXXHash32
instances that are written with Java's official API.- Returns:
-
a
XXHashFactory
that returnsXXHash32
instances that are written with Java's official API.
-
unsafeInstance
public static XXHashFactory unsafeInstance()
- Returns:
-
a
XXHashFactory
that returnsXXHash32
instances that may useUnsafe
to speed up hashing.
-
fastestJavaInstance
public static XXHashFactory fastestJavaInstance()
Returns the fastest availableXXHashFactory
instance which does not rely on JNI bindings. It first tries to load theunsafe instance
, and then thesafe Java instance
if the JVM doesn't have a workingUnsafe
.- Returns:
-
the fastest available
XXHashFactory
instance which does not rely on JNI bindings.
-
fastestInstance
public static XXHashFactory fastestInstance()
Returns the fastest availableXXHashFactory
instance. If the class loader is the system class loader and if thenative instance
loads successfully, then thenative instance
is returned, otherwise thefastest Java instance
is returned.Please read
javadocs of nativeInstance()
before using this method.- Returns:
-
the fastest available
XXHashFactory
instance.
-
newStreamingHash32
public StreamingXXHash32 newStreamingHash32(int seed)
Return a newStreamingXXHash32
instance.- Parameters:
-
seed
- the seed to use - Returns:
-
a
StreamingXXHash32
instance
-
newStreamingHash64
public StreamingXXHash64 newStreamingHash64(long seed)
Return a newStreamingXXHash64
instance.- Parameters:
-
seed
- the seed to use - Returns:
-
a
StreamingXXHash64
instance
-
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:
-
toString
in classjava.lang.Object
-
-