net.jpountz.lz4.LZ4SafeDecompressor Java Examples

The following examples show how to use net.jpountz.lz4.LZ4SafeDecompressor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Compressor.java    From vespa with Apache License 2.0 6 votes vote down vote up
public long warmup(double seconds) {
    byte [] input = new byte[0x4000];
    new Random().nextBytes(input);
    long timeDone = System.nanoTime() + (long)(seconds*1000000000);
    long compressedBytes = 0;
    byte [] decompressed = new byte [input.length];
    LZ4FastDecompressor fastDecompressor = factory.fastDecompressor();
    LZ4SafeDecompressor safeDecompressor = factory.safeDecompressor();
    LZ4Compressor fastCompressor = factory.fastCompressor();
    LZ4Compressor highCompressor = factory.highCompressor();
    while (System.nanoTime() < timeDone) {
        byte [] compressedFast = fastCompressor.compress(input);
        byte [] compressedHigh = highCompressor.compress(input);
        fastDecompressor.decompress(compressedFast, decompressed);
        fastDecompressor.decompress(compressedHigh, decompressed);
        safeDecompressor.decompress(compressedFast, decompressed);
        safeDecompressor.decompress(compressedHigh, decompressed);
        compressedBytes += compressedFast.length + compressedHigh.length;
    }
    return compressedBytes;
}
 
Example #2
Source File: LZ4Codec.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
    int decompressSize = buf.readInt();
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
    try {
        LZ4SafeDecompressor decompressor = factory.safeDecompressor();
        ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
        int pos = outBuffer.position();
        decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
        int compressedLength = outBuffer.position() - pos;
        out.writerIndex(compressedLength);
        return innerCodec.getValueDecoder().decode(out, state);
    } finally {
        out.release();
    }
}
 
Example #3
Source File: LZ4CompressorTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        if(args.length == 0){
            System.out.println("args[0] must be data file path");
            return;
        }
        LZ4Factory factory = LZ4Factory.fastestInstance();

        byte[] data = Files.toByteArray(new File(args[0]));
        final int decompressedLength = data.length;

        // compress data
        LZ4Compressor compressor = factory.fastCompressor();
        long start = System.currentTimeMillis();
        int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
        byte[] compressed = new byte[maxCompressedLength];
        int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);
        System.out.println("compress take:" + (System.currentTimeMillis() - start));
        System.out.println(compressedLength);

        // decompress data
        // - method 1: when the decompressed length is known
        LZ4FastDecompressor decompressor = factory.fastDecompressor();
        start = System.currentTimeMillis();
        byte[] restored = new byte[decompressedLength];
        int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
        System.out.println("decompress take:" + (System.currentTimeMillis() - start));
        System.out.println(decompressedLength);
        // compressedLength == compressedLength2

        // - method 2: when the compressed length is known (a little slower)
        // the destination buffer needs to be over-sized
        LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
        int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
    }
 
Example #4
Source File: LZ4CompressorTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        if(args.length == 0){
            System.out.println("args[0] must be data file path");
            return;
        }
        LZ4Factory factory = LZ4Factory.fastestInstance();

        byte[] data = Files.toByteArray(new File(args[0]));
        final int decompressedLength = data.length;

        // compress data
        LZ4Compressor compressor = factory.fastCompressor();
        long start = System.currentTimeMillis();
        int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
        byte[] compressed = new byte[maxCompressedLength];
        int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);
        System.out.println("compress take:" + (System.currentTimeMillis() - start));
        System.out.println(compressedLength);

        // decompress data
        // - method 1: when the decompressed length is known
        LZ4FastDecompressor decompressor = factory.fastDecompressor();
        start = System.currentTimeMillis();
        byte[] restored = new byte[decompressedLength];
        int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
        System.out.println("decompress take:" + (System.currentTimeMillis() - start));
        System.out.println(decompressedLength);
        // compressedLength == compressedLength2

        // - method 2: when the compressed length is known (a little slower)
        // the destination buffer needs to be over-sized
        LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
        int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
    }
 
Example #5
Source File: Lz4CompressUtils.java    From hibernate4-memcached with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}