Class LZ4JNIHCFastResetCompressor

java.lang.Object
net.jpountz.lz4.LZ4Compressor
net.jpountz.lz4.LZ4JNIHCFastResetCompressor
All Implemented Interfaces:
AutoCloseable

public final class LZ4JNIHCFastResetCompressor extends LZ4Compressor
An optimized LZ4 HC compressor that uses native LZ4_compress_HC_extStateHC_fastReset.

This compressor pre-allocates an LZ4_streamHC_t once and reuses it for every compression call through LZ4_compress_HC_extStateHC_fastReset. This avoids the expensive full state initialization that LZ4_compress_HC() performs on every call.

Each compression call is independent, making the output identical to LZ4HCJNICompressor for the same compression level when operating on array-backed or direct buffers. ByteBuffer inputs must be array-backed or direct.

Thread Safety: This class is NOT thread-safe. Each instance holds mutable native state and must be used by only one thread at a time. Concurrent use or close attempts fail fast with IllegalStateException.

Resource Management: This class holds native memory that must be freed. Always use try-with-resources or explicitly call AutoCloseable.close().

Example usage:


 LZ4Factory factory = LZ4Factory.nativeInstance();
 try (LZ4JNIHCFastResetCompressor compressor = factory.highFastResetCompressor()) {
     byte[] compressed = new byte[compressor.maxCompressedLength(data.length)];
     int compressedLen = compressor.compress(data, 0, data.length, compressed, 0, compressed.length);
     // ... use compressed[0..compressedLen-1]
 }
 
See Also:
  • Method Details

    • getCompressionLevel

      public int getCompressionLevel()
      Returns the compression level.
      Returns:
      compression level (default = 9)
    • compressNative

      protected int compressNative(long ptr, byte[] srcArr, ByteBuffer srcBuf, int srcOff, int srcLen, byte[] destArr, ByteBuffer destBuf, int destOff, int maxDestLen)
    • freeState

      protected void freeState(long ptr)
    • toString

      public String toString()
      Overrides:
      toString in class LZ4Compressor
    • compress

      public final int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen)
      Compresses src[srcOff:srcOff+srcLen] into dest[destOff:destOff+maxDestLen].
      Specified by:
      compress in class LZ4Compressor
      Parameters:
      src - source data
      srcOff - the start offset in src
      srcLen - the number of bytes to compress
      dest - destination buffer
      destOff - the start offset in dest
      maxDestLen - the maximum number of bytes to write in dest
      Returns:
      the compressed size
      Throws:
      LZ4Exception - if maxDestLen is too small
      IllegalStateException - if the compressor has been closed or is already in use
    • compress

      public final int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen)
      Compresses src[srcOff:srcOff+srcLen] into dest[destOff:destOff+maxDestLen].

      Both buffers must be either direct or array-backed. ByteBuffer positions remain unchanged.

      Specified by:
      compress in class LZ4Compressor
      Parameters:
      src - source data
      srcOff - the start offset in src
      srcLen - the number of bytes to compress
      dest - destination buffer
      destOff - the start offset in dest
      maxDestLen - the maximum number of bytes to write in dest
      Returns:
      the compressed size
      Throws:
      LZ4Exception - if maxDestLen is too small
      IllegalArgumentException - if src or dest is neither array-backed nor direct
      IllegalStateException - if the compressor has been closed or is already in use
    • isClosed

      public final boolean isClosed()
    • close

      public final void close()
      Closes this compressor and releases native resources. After calling this method, all compress methods will throw IllegalStateException.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IllegalStateException - if the compressor is in use by another thread