kr.pe.kwonnam.hibernate4memcached.util.Lz4CompressUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate4-memcached-core Show documentation
Show all versions of hibernate4-memcached-core Show documentation
hibernate4 memcached L2 cache implementation.
package kr.pe.kwonnam.hibernate4memcached.util;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;
import net.jpountz.lz4.LZ4SafeDecompressor;
/**
* Lz4 Compression Utils. L4z is much faster than gzip
*
* lz4-java
*
* @author KwonNam Son ([email protected])
*/
public class Lz4CompressUtils {
private static final LZ4Factory factory = LZ4Factory.fastestInstance();
public static byte[] compress(final byte[] src) {
if (src == null) {
throw new IllegalArgumentException("src must not be null.");
}
LZ4Compressor compressor = factory.fastCompressor();
return compressor.compress(src);
}
/**
* When the exact decompressed size is unknown.
* Decompress data size cannot be larger then maxDecompressedSize
*/
public static byte[] decompressSafe(final byte[] src, int maxDecompressedSize) {
if (src == null) {
throw new IllegalArgumentException("src must not be null.");
}
if (maxDecompressedSize <= 0) {
throw new IllegalArgumentException("maxDecompressedSize must be larger than 0 but " + maxDecompressedSize);
}
LZ4SafeDecompressor decompressor = factory.safeDecompressor();
return decompressor.decompress(src, maxDecompressedSize);
}
/**
* When the exact decompressed size is known, use this method to decompress. It's faster.
*
* @see net.jpountz.lz4.LZ4FastDecompressor
*/
public static byte[] decompressFast(byte[] src, int srcOffset, int exactDecompressedSize) {
if (src == null) {
throw new IllegalArgumentException("src must not be null.");
}
if (srcOffset < 0) {
throw new IllegalArgumentException("srcOffset must equal to or larger than 0 but " + srcOffset);
}
if (exactDecompressedSize < 0) {
throw new IllegalArgumentException("exactDecompressedSize must equal to or larger than 0 but " + exactDecompressedSize);
}
LZ4FastDecompressor decompressor = factory.fastDecompressor();
return decompressor.decompress(src, srcOffset, exactDecompressedSize);
}
/**
* @see #decompressFast(byte[], int, int)
*/
public static byte[] decompressFast(final byte[] src, int exactDecompressedSize) {
return decompressFast(src, 0, exactDecompressedSize);
}
}