Package net.cakenet.jsaton.util

Source Code of net.cakenet.jsaton.util.CompressionUtil

package net.cakenet.jsaton.util;

import java.io.ByteArrayOutputStream;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class CompressionUtil {
    public static byte[] inflate(byte[] source, boolean nowrap) {
        Inflater inf = new Inflater(nowrap);
        inf.setInput(source);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[100];
        try {
            while (!inf.finished()) {
                int read = inf.inflate(buf);
                baos.write(buf, 0, read);
            }
        } catch (DataFormatException dfe) {
            throw new RuntimeException(dfe);
        }
        return baos.toByteArray();
    }

    public static byte[] deflate(byte[] source, boolean nowrap) {
        Deflater d = new Deflater(Deflater.BEST_COMPRESSION, nowrap);
        d.setInput(source);
        d.finish();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[100];
        while (!d.finished()) {
            int len = d.deflate(buf);
            baos.write(buf, 0, len);
        }
        return baos.toByteArray();
    }
}
TOP

Related Classes of net.cakenet.jsaton.util.CompressionUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.