aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/vespa/config/LZ4PayloadCompressor.java
blob: 2a992721c865a6ecaa388e78a80402a095d377c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config;

import com.yahoo.compress.CompressionType;
import com.yahoo.compress.Compressor;

import java.nio.ByteBuffer;

/**
 * Wrapper for LZ4 compression that selects compression level based on properties.
 *
 * @author Ulf Lilleengen
 */
public class LZ4PayloadCompressor {

    private static final Compressor compressor = new Compressor(CompressionType.LZ4, 0);

    public byte[] compress(byte[] input) {
        return compressor.compressUnconditionally(input);
    }

    public byte[] compress(ByteBuffer input) {
        return compressor.compressUnconditionally(input);
    }

    public byte [] decompress(byte[] input, int uncompressedLen) {
        return compressor.decompressUnconditionally(input, 0, uncompressedLen);
    }

    public byte [] decompress(ByteBuffer input, int uncompressedLen) {
        ByteBuffer uncompressed = ByteBuffer.allocate(uncompressedLen);
        compressor.decompressUnconditionally(input, uncompressed);
        return uncompressed.array();
    }

}