public abstract class ChunkEncoder
extends java.lang.Object
implements java.io.Closeable
Note that instances are stateful and hence not thread-safe; one instance is meant to be used for processing a sequence of chunks where total length is known.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
_encodeBuffer
Buffer in which encoded content is stored during processing
|
protected int |
_hashModulo |
protected int[] |
_hashTable
Hash table contains lookup based on 3-byte sequence; key is hash
of such triplet, value is offset in buffer.
|
protected byte[] |
_headerBuffer
Small buffer passed to LZFChunk, needed for writing chunk header
|
protected BufferRecycler |
_recycler |
protected static int |
MAX_HASH_SIZE |
protected static int |
MAX_OFF |
protected static int |
MAX_REF |
protected static int |
MIN_BLOCK_TO_COMPRESS |
protected static int |
MIN_HASH_SIZE |
protected static int |
TAIL_LENGTH
How many tail bytes are we willing to just copy as is, to simplify
loop end checks? 4 is bare minimum, may be raised to 8?
|
Modifier | Constructor and Description |
---|---|
protected |
ChunkEncoder(int totalLength)
Uses a ThreadLocal soft-referenced BufferRecycler instance.
|
protected |
ChunkEncoder(int totalLength,
boolean bogus)
Alternate constructor used when we want to avoid allocation encoding
buffer, in cases where caller wants full control over allocations.
|
protected |
ChunkEncoder(int totalLength,
BufferRecycler bufferRecycler) |
protected |
ChunkEncoder(int totalLength,
BufferRecycler bufferRecycler,
boolean bogus)
Alternate constructor used when we want to avoid allocation encoding
buffer, in cases where caller wants full control over allocations.
|
Modifier and Type | Method and Description |
---|---|
int |
appendEncodedChunk(byte[] input,
int inputPtr,
int inputLen,
byte[] outputBuffer,
int outputPos)
Alternate chunk compression method that will append encoded chunk in
pre-allocated buffer.
|
int |
appendEncodedIfCompresses(byte[] input,
double maxResultRatio,
int inputPtr,
int inputLen,
byte[] outputBuffer,
int outputPos)
Method similar to
appendEncodedChunk(byte[], int, int, byte[], int) , but that will only append
encoded chunk if it compresses down to specified ratio (also considering header that
will be needed); otherwise will
return -1 without appending anything. |
private static int |
calcHashLen(int chunkSize) |
void |
close()
Method to close once encoder is no longer in use.
|
void |
encodeAndWriteChunk(byte[] data,
int offset,
int len,
java.io.OutputStream out)
Method for encoding individual chunk, writing it to given output stream.
|
boolean |
encodeAndWriteChunkIfCompresses(byte[] data,
int offset,
int inputLen,
java.io.OutputStream out,
double resultRatio)
Method for encoding individual chunk, writing it to given output stream,
if (and only if!) it compresses enough.
|
LZFChunk |
encodeChunk(byte[] data,
int offset,
int len)
Method for compressing (or not) individual chunks
|
LZFChunk |
encodeChunkIfCompresses(byte[] data,
int offset,
int inputLen,
double maxResultRatio)
Method for compressing individual chunk, if (and only if) it compresses down
to specified ratio or less.
|
BufferRecycler |
getBufferRecycler() |
protected int |
hash(int h) |
protected abstract int |
tryCompress(byte[] in,
int inPos,
int inEnd,
byte[] out,
int outPos)
Main workhorse method that will try to compress given chunk, and return
end position (offset to byte after last included byte).
|
protected static final int MIN_BLOCK_TO_COMPRESS
protected static final int MIN_HASH_SIZE
protected static final int MAX_HASH_SIZE
protected static final int MAX_OFF
protected static final int MAX_REF
protected static final int TAIL_LENGTH
protected final BufferRecycler _recycler
protected int[] _hashTable
protected final int _hashModulo
protected byte[] _encodeBuffer
protected byte[] _headerBuffer
protected ChunkEncoder(int totalLength)
totalLength
- Total encoded length; used for calculating size
of hash table to useprotected ChunkEncoder(int totalLength, BufferRecycler bufferRecycler)
totalLength
- Total encoded length; used for calculating size
of hash table to usebufferRecycler
- Buffer recycler instance, for usages where the
caller manages the recycler instancesprotected ChunkEncoder(int totalLength, boolean bogus)
protected ChunkEncoder(int totalLength, BufferRecycler bufferRecycler, boolean bogus)
private static int calcHashLen(int chunkSize)
public final void close()
encodeChunk(byte[], int, int)
will failclose
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public LZFChunk encodeChunk(byte[] data, int offset, int len)
public LZFChunk encodeChunkIfCompresses(byte[] data, int offset, int inputLen, double maxResultRatio)
maxResultRatio
- Value between 0.05 and 1.10 to indicate maximum relative size of
the result to use, in order to append encoded chunkpublic int appendEncodedChunk(byte[] input, int inputPtr, int inputLen, byte[] outputBuffer, int outputPos)
LZFEncoder.estimateMaxWorkspaceSize(int)
to calculate
necessary buffer size.public int appendEncodedIfCompresses(byte[] input, double maxResultRatio, int inputPtr, int inputLen, byte[] outputBuffer, int outputPos)
appendEncodedChunk(byte[], int, int, byte[], int)
, but that will only append
encoded chunk if it compresses down to specified ratio (also considering header that
will be needed); otherwise will
return -1
without appending anything.maxResultRatio
- Value between 0.05 and 1.10 to indicate maximum relative size of
the result to use, in order to append encoded chunkpublic void encodeAndWriteChunk(byte[] data, int offset, int len, java.io.OutputStream out) throws java.io.IOException
java.io.IOException
public boolean encodeAndWriteChunkIfCompresses(byte[] data, int offset, int inputLen, java.io.OutputStream out, double resultRatio) throws java.io.IOException
java.io.IOException
public BufferRecycler getBufferRecycler()
protected abstract int tryCompress(byte[] in, int inPos, int inEnd, byte[] out, int outPos)
result - originalOutPost
is the actual length of compressed chunk (without header)protected final int hash(int h)