Java Byte Array Uncompress uncompress(byte[] data)

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

Description

uncompress the supplied data using GZIPInputStream and return the uncompressed data.

License

Open Source License

Parameter

Parameter Description
data data to uncompress

Return

uncompressesd data

Declaration

public static byte[] uncompress(byte[] data) throws IOException 

Method Source Code


//package com.java2s;
/*// www  .  ja va2 s. c  o  m
 * ***** BEGIN LICENSE BLOCK *****
 * Zimbra Collaboration Suite Server
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Synacor, Inc.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 * ***** END LICENSE BLOCK *****
 */

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

import java.io.IOException;

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * uncompress the supplied data using GZIPInputStream
     * and return the uncompressed data.
     * @param data data to uncompress
     * @return uncompressesd data
     */
    public static byte[] uncompress(byte[] data) throws IOException {
        // TODO: optimize, this makes my head hurt
        ByteArrayOutputStream baos = null;
        ByteArrayInputStream bais = null;
        GZIPInputStream gis = null;
        try {
            int estimatedResultSize = data.length * 3;
            baos = new ByteArrayOutputStream(estimatedResultSize);
            bais = new ByteArrayInputStream(data);
            byte[] buffer = new byte[8192];
            gis = new GZIPInputStream(bais);

            int numRead;
            while ((numRead = gis.read(buffer, 0, buffer.length)) != -1) {
                baos.write(buffer, 0, numRead);
            }
            return baos.toByteArray();
        } finally {
            if (gis != null)
                gis.close();
            else if (bais != null)
                bais.close();
            if (baos != null)
                baos.close();
        }
    }
}

Related

  1. uncompress(byte[] b)
  2. uncompress(byte[] gzData, String charset)
  3. uncompress(byte[] input, int uncompr_len)
  4. uncompress(final byte[] buffer)
  5. uncompress(final byte[] compressedData)