Java Decompress Byte Array decompress(byte[] bytes)

Here you can find the source of decompress(byte[] bytes)

Description

Decompresses an array of bytes.

License

Open Source License

Parameter

Parameter Description
bytes - the compressed bytes to decompress.

Exception

Parameter Description
RuntimeException - if there is a decompression issue (rare).

Return

byte[] - the array of bytes containing the decompressed value.

Declaration

public static byte[] decompress(byte[] bytes) throws RuntimeException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.GZIPInputStream;

public class Main {
    /**/*from   w  ww  . j  a v a2s.  com*/
     * Decompresses an array of bytes.
     *
     * @param bytes -  the compressed bytes to decompress.
     * @return byte[] - the array of bytes containing the decompressed value.
     * @throws RuntimeException - if there is a decompression issue (rare).
     */
    public static byte[] decompress(byte[] bytes) throws RuntimeException {
        if (bytes == null || bytes.length == 0) {
            return bytes;
        }
        try {
            InputStream in = getInputStream(bytes);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            while (in.available() > 0) {
                bos.write(in.read());
            }
            byte[] data = bos.toByteArray();
            return data;
        } catch (IOException e) {
            throw new IllegalStateException("cannot decompress ", e);
        }

    }

    /**
     * Returns input stream to decompress the data as read.
     *
     * @param data the compressed bytes
     * @return InputStream, never null.
     * @throws IOException
     */
    public static InputStream getInputStream(byte[] data) throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream retval = new GZIPInputStream(bis);
        return retval;
    }
}

Related

  1. decompress(byte[] bytes)
  2. decompress(byte[] bytes)
  3. Decompress(byte[] bytes)
  4. decompress(byte[] bytes)
  5. decompress(byte[] bytes)
  6. decompress(byte[] bytes, int bufferSize)
  7. decompress(byte[] compressed)
  8. decompress(byte[] compressed)
  9. decompress(byte[] compressed)