Package net.jpountz.lz4
Class LZ4CompressorWithLength
java.lang.Object
net.jpountz.lz4.LZ4CompressorWithLength
Covenience class to include the length of the original decompressed data
in the output compressed data, so that the user does not need to save
the length at anywhere else. The compressed data must be decompressed by
LZ4DecompressorWithLength
and is NOT compatible with any other
decompressors in lz4-java or any other lz4 tools. This class deliberately
does not extend LZ4Compressor
because they are not interchangable.-
Constructor Summary
ConstructorsConstructorDescriptionLZ4CompressorWithLength
(LZ4Compressor compressor) Creates a new compressor that includes the length of the original decompressed data in the output compressed data. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
compress
(byte[] src) Convenience method, equivalent to callingcompress(src, 0, src.length)
.int
compress
(byte[] src, byte[] dest) Convenience method, equivalent to callingcompress(src, 0, src.length, dest, 0)
.byte[]
compress
(byte[] src, int srcOff, int srcLen) Convenience method which returnssrc[srcOff:srcOff+srcLen]
compressed.int
compress
(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff) Convenience method, equivalent to callingcompress(src, srcOff, srcLen, dest, destOff, dest.length - destOff)
.int
compress
(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) Compressessrc[srcOff:srcOff+srcLen]
intodest[destOff:destOff+maxDestLen]
and returns the compressed length.int
compress
(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) Compressessrc[srcOff:srcOff+srcLen]
intodest[destOff:destOff+maxDestLen]
and returns the compressed length.void
compress
(ByteBuffer src, ByteBuffer dest) Compressessrc
intodest
.int
maxCompressedLength
(int length) Returns the maximum compressed length for an input of sizelength
.
-
Constructor Details
-
LZ4CompressorWithLength
Creates a new compressor that includes the length of the original decompressed data in the output compressed data.- Parameters:
compressor
- compressor to use
-
-
Method Details
-
maxCompressedLength
public int maxCompressedLength(int length) Returns the maximum compressed length for an input of sizelength
.- Parameters:
length
- the input size in bytes- Returns:
- the maximum compressed length in bytes
-
compress
public byte[] compress(byte[] src) Convenience method, equivalent to callingcompress(src, 0, src.length)
.- Parameters:
src
- the source data- Returns:
- the compressed data
-
compress
public byte[] compress(byte[] src, int srcOff, int srcLen) Convenience method which returnssrc[srcOff:srcOff+srcLen]
compressed.Warning: this method has an important overhead due to the fact that it needs to allocate a buffer to compress into, and then needs to resize this buffer to the actual compressed length.
Here is how this method is implemented:
final int maxCompressedLength = maxCompressedLength(srcLen); final byte[] compressed = new byte[maxCompressedLength]; final int compressedLength = compress(src, srcOff, srcLen, compressed, 0); return Arrays.copyOf(compressed, compressedLength);
- Parameters:
src
- the source datasrcOff
- the start offset in srcsrcLen
- the number of bytes to compress- Returns:
- the compressed data
-
compress
public int compress(byte[] src, byte[] dest) Convenience method, equivalent to callingcompress(src, 0, src.length, dest, 0)
.- Parameters:
src
- the source datadest
- the destination buffer- Returns:
- the compressed size
- Throws:
LZ4Exception
- if dest is too small
-
compress
public int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff) Convenience method, equivalent to callingcompress(src, srcOff, srcLen, dest, destOff, dest.length - destOff)
.- Parameters:
src
- the source datasrcOff
- the start offset in srcsrcLen
- the number of bytes to compressdest
- the destination bufferdestOff
- the start offset in dest- Returns:
- the compressed size
- Throws:
LZ4Exception
- if dest is too small
-
compress
public int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) Compressessrc[srcOff:srcOff+srcLen]
intodest[destOff:destOff+maxDestLen]
and returns the compressed length. This method will throw aLZ4Exception
if this compressor is unable to compress the input into less thanmaxDestLen
bytes. To prevent this exception to be thrown, you should make sure thatmaxDestLen >= maxCompressedLength(srcLen)
.- Parameters:
src
- the source datasrcOff
- the start offset in srcsrcLen
- the number of bytes to compressdest
- the 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 small
-
compress
- Parameters:
src
- the source datadest
- the destination buffer- Throws:
LZ4Exception
- if dest is too small
-
compress
public int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) Compressessrc[srcOff:srcOff+srcLen]
intodest[destOff:destOff+maxDestLen]
and returns the compressed length. This method will throw aLZ4Exception
if this compressor is unable to compress the input into less thanmaxDestLen
bytes. To prevent this exception to be thrown, you should make sure thatmaxDestLen >= maxCompressedLength(srcLen)
.ByteBuffer
positions remain unchanged.- Parameters:
src
- the source datasrcOff
- the start offset in srcsrcLen
- the number of bytes to compressdest
- the 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 small
-