Java Decompress Byte Array decompress(byte[] bytes)

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

Description

Decompress a compressed object

License

Open Source License

Parameter

Parameter Description
bytes byte array corresponding to compressed object
T the type of the object to decompress and deserialize

Return

decompressed object

Declaration

public static <T> T decompress(byte[] bytes) 

Method Source Code


//package com.java2s;
/*//  w w w .j  av  a  2 s. c  om
 * Copyright (c) 2016 IBM Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.ObjectInputStream;

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * Decompress a compressed object
     *
     * @param bytes byte array corresponding to compressed object
     * @param <T>   the type of the object to decompress and deserialize
     * @return decompressed object
     */
    public static <T> T decompress(byte[] bytes) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        T object = null;
        try (GZIPInputStream gzipIn = new GZIPInputStream(bais);
                ObjectInputStream objectIn = new ObjectInputStream(gzipIn)) {
            object = (T) objectIn.readObject();
        } catch (IOException | ClassNotFoundException e) {
            describeException(e, "Exception in Util.decompress");
        }
        return object;
    }

    /**
     * Output information about an exception
     *
     * @param e       The exception
     * @param message Message to output
     */
    public static void describeException(Exception e, String message) {
        System.out.println(message);
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

Related

  1. decompress(byte[] byteArray)
  2. decompress(byte[] bytes)
  3. Decompress(byte[] bytes)
  4. decompress(byte[] bytes)
  5. decompress(byte[] bytes)