Java Deflate Byte Array deflate(byte[] input)

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

Description

Method deflate

License

Open Source License

Parameter

Parameter Description
input byte[]

Exception

Parameter Description
IOException an exception

Return

byte[]

Declaration

public static byte[] deflate(byte[] input) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;

public class Main {
    /**/*from  www. jav a 2s  .c  om*/
     * Field BUF_LEN
     */
    private static int BUF_LEN = 1024;

    /**
     * Method deflate
     * @param input byte[]
     * @return byte[]
     * @throws IOException
     */
    public static byte[] deflate(byte[] input) throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(input);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(bos);
        byte[] buffer = new byte[BUF_LEN];
        int bytesRead = bis.read(buffer);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bytesRead);
            bytesRead = bis.read(buffer);
        }

        dos.close();

        return bos.toByteArray();
    }
}

Related

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