Java Decompress Byte Array decompress(byte[] compressedData)

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

Description

decompress

License

Open Source License

Declaration

public static byte[] decompress(byte[] compressedData) 

Method Source Code

//package com.java2s;
/********************************************************************************
 * Copyright (c) 2009 Regents of the University of Minnesota
 *
 * This Software was written at the Minnesota Supercomputing Institute
 * http://msi.umn.edu//from  w  w  w  . j a v  a 2  s  .  c  o  m
 *
 * All rights reserved. The following statement of license applies
 * only to this file, and and not to the other files distributed with it
 * or derived therefrom.  This file is made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Minnesota Supercomputing Institute - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

public class Main {
    public static byte[] decompress(byte[] compressedData) {
        byte[] decompressedData;

        // using a ByteArrayOutputStream to not having to define the result array size beforehand
        Inflater decompressor = new Inflater();

        decompressor.setInput(compressedData);
        // Create an expandable byte array to hold the decompressed data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                if (count == 0 && decompressor.needsInput()) {
                    break;
                }
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
                throw new IllegalStateException(
                        "Encountered wrong data format " + "while trying to decompress binary data!", e);
            }
        }
        try {
            bos.close();
        } catch (IOException e) {
            // ToDo: add logging
            e.printStackTrace();
        }
        // Get the decompressed data
        decompressedData = bos.toByteArray();

        if (decompressedData == null) {
            throw new IllegalStateException("Decompression of binary data produced no result (null)!");
        }
        return decompressedData;
    }
}

Related

  1. decompress(byte[] compressed)
  2. decompress(byte[] compressed)
  3. decompress(byte[] compressed)
  4. decompress(byte[] compressed)
  5. decompress(byte[] compressedData)
  6. decompress(byte[] compressedData, int off, int len)
  7. decompress(byte[] data)
  8. decompress(byte[] data)
  9. decompress(byte[] data)