Java Decompress Byte Array decompress(byte[] bytes, int bufferSize)

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

Description

Decompresses the given byte[] and returns the Object.

License

Apache License

Parameter

Parameter Description
T Type of expected Object
bytes compressed byte[]
bufferSize size of buffer to be used (in bytes)

Exception

Parameter Description
IOException if failed to decompress
ClassCastException if cannot cast to specified type

Return

decompressed Object

Declaration

@SuppressWarnings("unchecked") 
public static <T> T decompress(byte[] bytes, int bufferSize) throws IOException, ClassCastException 

Method Source Code


//package com.java2s;
/*/*  w  ww.  jav  a2  s .com*/
 * Copyright (C) 2008 Yohan Liyanage. 
 * 
 * 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import java.util.zip.Inflater;

public class Main {
    /**
     * Decompresses the given byte[] and returns the Object.
     * 
     * @param <T> Type of expected Object
     * @param bytes compressed byte[]
     * @param bufferSize size of buffer to be used (in bytes)
     * 
     * @return decompressed Object
     * 
     * @throws IOException if failed to decompress
     * @throws ClassCastException if cannot cast to specified type
     */
    @SuppressWarnings("unchecked") /* Ignore Unchecked Cast Warning */
    public static <T> T decompress(byte[] bytes, int bufferSize) throws IOException, ClassCastException {

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferSize);
            Inflater inflater = new Inflater();
            inflater.setInput(bytes);

            // Decompress
            byte[] buf = new byte[bufferSize];
            while (!inflater.finished()) {
                int count = inflater.inflate(buf);
                bos.write(buf, 0, count);
            }
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));

            return (T) ois.readObject();
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
}

Related

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