Java Deflate Byte Array deflate(byte[] in)

Here you can find the source of deflate(byte[] in)

Description

deflate

License

Open Source License

Declaration

public static byte[] deflate(byte[] in) 

Method Source Code

//package com.java2s;
/**//from ww w . j  ava  2 s  .c o  m
 * (C) 2007-2010 Taobao Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

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

import java.util.zip.InflaterInputStream;

public class Main {
    public static byte[] deflate(byte[] in) {
        ByteArrayOutputStream bos = null;

        if (in != null) {
            ByteArrayInputStream bis = new ByteArrayInputStream(in);

            bos = new ByteArrayOutputStream();

            InflaterInputStream gis;

            try {
                gis = new InflaterInputStream(bis);

                byte[] buf = new byte[8192];
                int r = -1;

                while ((r = gis.read(buf)) > 0) {
                    bos.write(buf, 0, r);
                }
            } catch (IOException e) {
                bos = null;
            }
        }

        return (bos == null) ? null : bos.toByteArray();
    }
}

Related

  1. deflate(byte[] bytes)
  2. deflate(byte[] data)
  3. deflate(byte[] data)
  4. deflate(byte[] data)
  5. deflate(byte[] data, byte[] dictionary)
  6. deflate(byte[] input)
  7. deflate(final byte[] pInput)
  8. deflate(String inString)
  9. deflate(String text, String encode)