// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.compress;

import java.util.Arrays;

/**
 * Frame based Zstd compressor (https://github.com/facebook/zstd)
 * Implemented based on https://github.com/airlift/aircompressor - a pure Java implementation (no JNI).
 *
 * @author bjorncs
 */
public class ZstdCompressor {

    private static final io.airlift.compress.zstd.ZstdCompressor compressor = new io.airlift.compress.zstd.ZstdCompressor();
    private static final io.airlift.compress.zstd.ZstdDecompressor decompressor = new io.airlift.compress.zstd.ZstdDecompressor();

    public byte[] compress(byte[] input, int inputOffset, int inputLength) {
        int maxCompressedLength = compressor.maxCompressedLength(inputLength);
        byte[] output = new byte[maxCompressedLength];
        int compressedLength = compressor.compress(input, inputOffset, inputLength, output, 0, maxCompressedLength);
        return Arrays.copyOf(output, compressedLength);
    }

    public byte[] decompress(byte[] input, int inputOffset, int inputLength) {
        int decompressedLength = (int) io.airlift.compress.zstd.ZstdDecompressor.getDecompressedSize(input, inputOffset, inputLength);
        byte[] output = new byte[decompressedLength];
        decompressor.decompress(input, inputOffset, inputLength, output, 0, decompressedLength);
        return output;
    }
}

@@ -764,20 +764,12 @@
 h = 
-compressor.m
+getM
 axCo
@@ -883,27 +883,16 @@
 ength = 
-compressor.
 compress
@@ -1036,17 +1036,12 @@
 lic 
-byte%5B%5D de
+int 
 comp
@@ -1095,93 +1095,293 @@
 ngth
-) %7B%0A        int decompressedLength = (int) io.airlift.compress.zstd.ZstdDecompressor.
+, byte%5B%5D output, int outputOffset, int maxOutputLength) %7B%0A        return compressor.compress(input, inputOffset, inputLength, output, outputOffset, maxOutputLength);%0A    %7D%0A%0A    public byte%5B%5D decompress(byte%5B%5D input, int inputOffset, int inputLength) %7B%0A        int decompressedLength = 
 getD
@@ -1395,12 +1395,14 @@
 ssed
-Size
+Length
 (inp
@@ -1508,21 +1508,8 @@
 ress
-or.decompress
 (inp
@@ -1591,16 +1591,628 @@
 n output
+;%0A    %7D%0A%0A    public int decompress(byte%5B%5D input, int inputOffset, int inputLength, byte%5B%5D output, int outputOffset, int maxOutputLength) %7B%0A        return decompressor.decompress(input, inputOffset, inputLength, output, outputOffset, maxOutputLength);%0A    %7D%0A%0A    public static int getMaxCompressedLength(int uncompressedLength) %7B%0A        return compressor.maxCompressedLength(uncompressedLength);%0A    %7D%0A%0A    public static int getDecompressedLength(byte%5B%5D input, int inputOffset, int inputLength) %7B%0A        return (int) io.airlift.compress.zstd.ZstdDecompressor.getDecompressedSize(input, inputOffset, inputLength)
 ;%0A    %7D%0A

Extend ZstdCompressor with more low-level API methods