at.yawk.accordion.compression.SnappyCompressor.java Source code

Java tutorial

Introduction

Here is the source code for at.yawk.accordion.compression.SnappyCompressor.java

Source

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package at.yawk.accordion.compression;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import lombok.Getter;
import org.xerial.snappy.Snappy;

/**
 * Compressor implementation that uses snappy to compress data.
 *
 * @author yawkat
 */
public class SnappyCompressor implements Compressor {
    @Getter
    private static final Compressor instance = new SnappyCompressor();

    private SnappyCompressor() {
    }

    @Override
    public ByteBuf encode(ByteBuf raw) {
        try {
            return Unpooled.wrappedBuffer(Snappy.compress(toArray(raw)));
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public ByteBuf decode(ByteBuf compressed) {
        try {
            return Unpooled.wrappedBuffer(Snappy.uncompress(toArray(compressed)));
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private static byte[] toArray(ByteBuf raw) {
        return Arrays.copyOfRange(raw.array(), raw.arrayOffset() + raw.readerIndex(),
                raw.arrayOffset() + raw.readerIndex() + raw.readableBytes());
    }
}