All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jarvis.cache.compress.CommonsCompressor Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
package com.jarvis.cache.compress;

import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 *
 */
public class CommonsCompressor implements ICompressor {

    private static final int BUFFER = 1024;

    private static final CompressorStreamFactory FACTORY = new CompressorStreamFactory();

    private String name;

    public CommonsCompressor(String name) {
        this.name = name;
    }

    @Override
    public byte[] compress(ByteArrayInputStream bais) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CompressorOutputStream cos = FACTORY.createCompressorOutputStream(name, baos);
        int len;
        byte buf[] = new byte[BUFFER];
        while ((len = bais.read(buf, 0, BUFFER)) != -1) {
            cos.write(buf, 0, len);
        }
        cos.flush();
        cos.close();
        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();
        return output;
    }

    @Override
    public byte[] decompress(ByteArrayInputStream bais) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CompressorInputStream cis = FACTORY.createCompressorInputStream(name, bais);
        int len;
        byte buf[] = new byte[BUFFER];
        while ((len = cis.read(buf, 0, BUFFER)) != -1) {
            baos.write(buf, 0, len);
        }
        cis.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();
        return output;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy