Java Decompress Byte Array decompress(byte[] source)

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

Description

Decompresses the given byte array of compressed data.

License

Open Source License

Parameter

Parameter Description
source byte array containing the compressed data

Exception

Parameter Description
IOExceptionif I/O error occurs

Return

decompressed data as a byte array

Declaration

public static byte[] decompress(byte[] source) throws IOException 

Method Source Code


//package com.java2s;
/*//w w w .jav a  2 s .  co m
* Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  Helper class containing common non-HTI-specific helper
*                functions that can be used in other java classes.
*
*/

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

import java.io.IOException;

import java.util.zip.InflaterInputStream;

public class Main {
    /**
     * Decompresses the given byte array of compressed data.
     * Returns the decompressed data as a byte array.
     *
     * @param source            byte array containing the compressed data
     * @return                  decompressed data as a byte array
     * @throws IOException      if I/O error occurs
     */
    public static byte[] decompress(byte[] source) throws IOException {
        // decompress the response through InflaterInputStream
        InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(source));

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int readBytes;

        while ((readBytes = iis.read(buffer)) != -1) {
            baos.write(buffer, 0, readBytes);
        }
        iis.close();

        return baos.toByteArray();
    }
}

Related

  1. decompress(byte[] data, int offset, int length)
  2. decompress(byte[] gzipped)
  3. decompress(byte[] in)
  4. decompress(byte[] input)
  5. decompress(byte[] source)
  6. decompress(byte[] src, Inflater decompresser, int compressCycleSize)
  7. decompress(byte[] str)
  8. decompress(byte[] zipByte)
  9. decompress(final byte[] compressed)