Class LZ4JNIFastResetCompressor
- All Implemented Interfaces:
AutoCloseable
LZ4_compress_fast_extState_fastReset.
This compressor pre-allocates an LZ4_stream_t once and reuses it for every compression
call through LZ4_compress_fast_extState_fastReset. This avoids the expensive full
state initialization that LZ4_compress_default() performs on every call.
Each compression call is independent, making the output identical to LZ4JNICompressor
for the same acceleration 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 (LZ4JNIFastResetCompressor compressor = factory.fastResetCompressor()) {
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 Summary
Modifier and TypeMethodDescriptionfinal voidclose()Closes this compressor and releases native resources.final intcompress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) Compressessrc[srcOff:srcOff+srcLen]intodest[destOff:destOff+maxDestLen].final intcompress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) Compressessrc[srcOff:srcOff+srcLen]intodest[destOff:destOff+maxDestLen].protected intcompressNative(long ptr, byte[] srcArr, ByteBuffer srcBuf, int srcOff, int srcLen, byte[] destArr, ByteBuffer destBuf, int destOff, int maxDestLen) protected voidfreeState(long ptr) intReturns the acceleration factor.final booleanisClosed()toString()Methods inherited from class net.jpountz.lz4.LZ4Compressor
compress, compress, compress, compress, compress, maxCompressedLength
-
Method Details
-
getAcceleration
public int getAcceleration()Returns the acceleration factor.- Returns:
- acceleration factor (1 = default)
-
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
- Overrides:
toStringin classLZ4Compressor
-
compress
public final int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) Compressessrc[srcOff:srcOff+srcLen]intodest[destOff:destOff+maxDestLen].- Specified by:
compressin classLZ4Compressor- Parameters:
src- source datasrcOff- the start offset in srcsrcLen- the number of bytes to compressdest- destination bufferdestOff- the start offset in destmaxDestLen- the maximum number of bytes to write in dest- Returns:
- the compressed size
- Throws:
LZ4Exception- if maxDestLen is too smallIllegalStateException- 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) Compressessrc[srcOff:srcOff+srcLen]intodest[destOff:destOff+maxDestLen].Both buffers must be either direct or array-backed.
ByteBufferpositions remain unchanged.- Specified by:
compressin classLZ4Compressor- Parameters:
src- source datasrcOff- the start offset in srcsrcLen- the number of bytes to compressdest- destination bufferdestOff- the start offset in destmaxDestLen- the maximum number of bytes to write in dest- Returns:
- the compressed size
- Throws:
LZ4Exception- if maxDestLen is too smallIllegalArgumentException- if src or dest is neither array-backed nor directIllegalStateException- 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 throwIllegalStateException.- Specified by:
closein interfaceAutoCloseable- Throws:
IllegalStateException- if the compressor is in use by another thread
-