Package com.github.luben.zstd
Class ZstdCompressCtx
- java.lang.Object
-
- com.github.luben.zstd.AutoCloseBase
-
- com.github.luben.zstd.ZstdCompressCtx
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class ZstdCompressCtx extends AutoCloseBase
-
-
Field Summary
Fields Modifier and Type Field Description private ZstdDictCompresscompression_dictprivate longnativePtr
-
Constructor Summary
Constructors Constructor Description ZstdCompressCtx()Create a context for faster compress operations One such context is required for each thread - put this in a ThreadLocal.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]compress(byte[] src)intcompress(byte[] dst, byte[] src)java.nio.ByteBuffercompress(java.nio.ByteBuffer srcBuf)Compresses the data in buffer 'srcBuf'intcompress(java.nio.ByteBuffer dstBuf, java.nio.ByteBuffer srcBuf)Compresses the data in buffer 'srcBuf'intcompressByteArray(byte[] dstBuff, int dstOffset, int dstSize, byte[] srcBuff, int srcOffset, int srcSize)Compresses byte array 'srcBuff' into byte array 'dstBuff' reusing this ZstdCompressCtx.private static longcompressByteArray0(long ptr, byte[] dst, int dstOffset, int dstSize, byte[] src, int srcOffset, int srcSize)intcompressDirectByteBuffer(java.nio.ByteBuffer dstBuff, int dstOffset, int dstSize, java.nio.ByteBuffer srcBuff, int srcOffset, int srcSize)Compresses buffer 'srcBuff' into buffer 'dstBuff' reusing this ZstdCompressCtx.private static longcompressDirectByteBuffer0(long ptr, java.nio.ByteBuffer dst, int dstOffset, int dstSize, java.nio.ByteBuffer src, int srcOffset, int srcSize)booleancompressDirectByteBufferStream(java.nio.ByteBuffer dst, java.nio.ByteBuffer src, EndDirective endOp)Compress as much of thesrcByteBufferinto thedstByteBufferas possible.private static longcompressDirectByteBufferStream0(long ptr, java.nio.ByteBuffer dst, int dstOffset, int dstSize, java.nio.ByteBuffer src, int srcSize, int srcOffset, int endOp)4 pieces of information are packed into the return value of this method, which must be treated as an unsigned long.(package private) voiddoClose()private voidensureOpen()private static voidfree(long ptr)ZstdFrameProgressiongetFrameProgression()Tells how much data has been ingested (read from input), consumed (input actually compressed) and produced (output) for current frame.private static ZstdFrameProgressiongetFrameProgression0(long ptr)private static longinit()private longloadCDict0(long ptr, byte[] dict)private longloadCDictFast0(long ptr, ZstdDictCompress dict)ZstdCompressCtxloadDict(byte[] dict)Load compression dictionary to be used for subsequently compressed frames.ZstdCompressCtxloadDict(ZstdDictCompress dict)Load compression dictionary to be used for subsequently compressed frames.voidreset()Clear all state and parameters from the compression context.private static longreset0(long ptr)ZstdCompressCtxsetChecksum(boolean checksumFlag)Enable or disable compression checksumsprivate static voidsetChecksum0(long ptr, boolean checksumFlag)ZstdCompressCtxsetContentSize(boolean contentSizeFlag)Enable or disable content sizeprivate static voidsetContentSize0(long ptr, boolean contentSizeFlag)ZstdCompressCtxsetDictID(boolean dictIDFlag)Enable or disable dictIDprivate static voidsetDictID0(long ptr, boolean dictIDFlag)ZstdCompressCtxsetLevel(int level)Set compression levelprivate static voidsetLevel0(long ptr, int level)ZstdCompressCtxsetLong(int windowLog)Enable or disable LongDistanceMatching and set the window sizeZstdCompressCtxsetMagicless(boolean magiclessFlag)Enable or disable magicless framesvoidsetPledgedSrcSize(long srcSize)Promise to compress a certain number of source bytes.private static longsetPledgedSrcSize0(long ptr, long srcSize)ZstdCompressCtxsetWorkers(int workers)-
Methods inherited from class com.github.luben.zstd.AutoCloseBase
acquireSharedLock, close, releaseSharedLock, storeFence
-
-
-
-
Field Detail
-
nativePtr
private long nativePtr
-
compression_dict
private ZstdDictCompress compression_dict
-
-
Method Detail
-
init
private static long init()
-
free
private static void free(long ptr)
-
doClose
void doClose()
- Specified by:
doClosein classAutoCloseBase
-
ensureOpen
private void ensureOpen()
-
setLevel
public ZstdCompressCtx setLevel(int level)
Set compression level- Parameters:
level- compression level, default:Zstd.defaultCompressionLevel()
-
setLevel0
private static void setLevel0(long ptr, int level)
-
setMagicless
public ZstdCompressCtx setMagicless(boolean magiclessFlag)
Enable or disable magicless frames- Parameters:
magiclessFlag- A 32-bits magic number is written at start of frame, default: false
-
setChecksum
public ZstdCompressCtx setChecksum(boolean checksumFlag)
Enable or disable compression checksums- Parameters:
checksumFlag- A 32-bits checksum of content is written at end of frame, default: false
-
setChecksum0
private static void setChecksum0(long ptr, boolean checksumFlag)
-
setWorkers
public ZstdCompressCtx setWorkers(int workers)
-
setContentSize
public ZstdCompressCtx setContentSize(boolean contentSizeFlag)
Enable or disable content size- Parameters:
contentSizeFlag- Content size will be written into frame header _whenever known_, default: true
-
setContentSize0
private static void setContentSize0(long ptr, boolean contentSizeFlag)
-
setDictID
public ZstdCompressCtx setDictID(boolean dictIDFlag)
Enable or disable dictID- Parameters:
dictIDFlag- When applicable, dictionary's ID is written into frame header, default: true
-
setDictID0
private static void setDictID0(long ptr, boolean dictIDFlag)
-
setLong
public ZstdCompressCtx setLong(int windowLog)
Enable or disable LongDistanceMatching and set the window size- Parameters:
windowLog- Maximum allowed back-reference distance, expressed as power of 2. This will set a memory budget for streaming decompression, with larger values requiring more memory and typically compressing more. Must be clamped between 10 and 32/64 but values greater than 27 may not be decompressable in all context as they require more memory. 0 disables LDM.
-
loadDict
public ZstdCompressCtx loadDict(ZstdDictCompress dict)
Load compression dictionary to be used for subsequently compressed frames.- Parameters:
dict- the dictionary or `null` to remove loaded dictionary
-
loadCDictFast0
private long loadCDictFast0(long ptr, ZstdDictCompress dict)
-
loadDict
public ZstdCompressCtx loadDict(byte[] dict)
Load compression dictionary to be used for subsequently compressed frames.- Parameters:
dict- the dictionary or `null` to remove loaded dictionary
-
loadCDict0
private long loadCDict0(long ptr, byte[] dict)
-
getFrameProgression
public ZstdFrameProgression getFrameProgression()
Tells how much data has been ingested (read from input), consumed (input actually compressed) and produced (output) for current frame.
-
getFrameProgression0
private static ZstdFrameProgression getFrameProgression0(long ptr)
-
reset
public void reset()
Clear all state and parameters from the compression context. This leaves the object in a state identical to a newly created compression context.
-
reset0
private static long reset0(long ptr)
-
setPledgedSrcSize
public void setPledgedSrcSize(long srcSize)
Promise to compress a certain number of source bytes. Knowing the number of bytes to compress up front helps to choose proper compression settings and size internal buffers. Additionally, the pledged size is stored in the header of the output stream, allowing decompressors to know how much uncompressed data to expect. Attempting to compress more or less than the pledged size will result in an error.
-
setPledgedSrcSize0
private static long setPledgedSrcSize0(long ptr, long srcSize)
-
compressDirectByteBufferStream
public boolean compressDirectByteBufferStream(java.nio.ByteBuffer dst, java.nio.ByteBuffer src, EndDirective endOp)Compress as much of thesrcByteBufferinto thedstByteBufferas possible.- Parameters:
dst- destination of compressed datasrc- buffer to compressendOp- directive for handling the end of the stream- Returns:
- true if all state has been flushed from internal buffers
-
compressDirectByteBufferStream0
private static long compressDirectByteBufferStream0(long ptr, java.nio.ByteBuffer dst, int dstOffset, int dstSize, java.nio.ByteBuffer src, int srcSize, int srcOffset, int endOp)4 pieces of information are packed into the return value of this method, which must be treated as an unsigned long. The highest bit is set if all data has been flushed from internal buffers. The next 31 bits are the new position of the destination buffer. The next bit is set if an error occurred. If an error occurred, the lowest 31 bits encode a zstd error code. Otherwise, the lowest 31 bits are the new position of the source buffer.
-
compressDirectByteBuffer
public int compressDirectByteBuffer(java.nio.ByteBuffer dstBuff, int dstOffset, int dstSize, java.nio.ByteBuffer srcBuff, int srcOffset, int srcSize)Compresses buffer 'srcBuff' into buffer 'dstBuff' reusing this ZstdCompressCtx. Destination buffer should be sized to handle worst cases situations (input data not compressible). Worst case size evaluation is provided by function ZSTD_compressBound(). This is a low-level function that does not take into account or affect the `limit` or `position` of source or destination buffers.- Parameters:
dstBuff- the destination buffer - must be directdstOffset- the start offset of 'dstBuff'dstSize- the size of 'dstBuff' (after 'dstOffset')srcBuff- the source buffer - must be directsrcOffset- the start offset of 'srcBuff'srcSize- the length of 'srcBuff' (after 'srcOffset')- Returns:
- the number of bytes written into buffer 'dstBuff'.
-
compressDirectByteBuffer0
private static long compressDirectByteBuffer0(long ptr, java.nio.ByteBuffer dst, int dstOffset, int dstSize, java.nio.ByteBuffer src, int srcOffset, int srcSize)
-
compressByteArray
public int compressByteArray(byte[] dstBuff, int dstOffset, int dstSize, byte[] srcBuff, int srcOffset, int srcSize)Compresses byte array 'srcBuff' into byte array 'dstBuff' reusing this ZstdCompressCtx. Destination buffer should be sized to handle worst cases situations (input data not compressible). Worst case size evaluation is provided by function ZSTD_compressBound().- Parameters:
dstBuff- the destination buffer (byte array)dstOffset- the start offset of 'dstBuff'dstSize- the size of 'dstBuff' (after 'dstOffset')srcBuff- the source buffer (byte array)srcOffset- the start offset of 'srcBuff'srcSize- the length of 'srcBuff' (after 'srcOffset')- Returns:
- the number of bytes written into buffer 'dstBuff'.
-
compressByteArray0
private static long compressByteArray0(long ptr, byte[] dst, int dstOffset, int dstSize, byte[] src, int srcOffset, int srcSize)
-
compress
public int compress(java.nio.ByteBuffer dstBuf, java.nio.ByteBuffer srcBuf)Compresses the data in buffer 'srcBuf'- Parameters:
dstBuf- the destination buffer - must be direct. It is assumed that the `position()` of this buffer marks the offset at which the compressed data are to be written, and that the `limit()` of this buffer is the maximum compressed data size to allow.When this method returns successfully, its `position()` will be set to its current `position()` plus the compressed size of the data.
srcBuf- the source buffer - must be direct. It is assumed that the `position()` of this buffer marks the beginning of the uncompressed data to be compressed, and that the `limit()` of this buffer marks its end.When this method returns successfully, its `position()` will be set to the initial `limit()`.
- Returns:
- the size of the compressed data
-
compress
public java.nio.ByteBuffer compress(java.nio.ByteBuffer srcBuf) throws ZstdExceptionCompresses the data in buffer 'srcBuf'- Parameters:
srcBuf- the source buffer - must be direct. It is assumed that the `position()` of the buffer marks the beginning of the uncompressed data to be compressed, and that the `limit()` of this buffer marks its end.When this method returns successfully, its `position()` will be set to its initial `limit()`.
- Returns:
- A newly allocated direct ByteBuffer containing the compressed data.
- Throws:
ZstdException
-
compress
public int compress(byte[] dst, byte[] src)
-
compress
public byte[] compress(byte[] src)
-
-