Java Byte Array Uncompress unCompressString(final byte[] data, final String encoding)

Here you can find the source of unCompressString(final byte[] data, final String encoding)

Description

un Compress String

License

Open Source License

Declaration

public static String unCompressString(final byte[] data, final String encoding) throws IOException 

Method Source Code

//package com.java2s;
/*//  w w  w .j  a v  a2s.  com
 * Copyright (c) 2006-2007 Massachusetts General Hospital 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the i2b2 Software License v1.0 
 * which accompanies this distribution. 
 * 
 * Contributors:
 *       Kavishwar Wagholikar (kavi)
 *       July 4, 2015
 */

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

import java.io.IOException;

import java.util.zip.GZIPInputStream;

public class Main {
    public static String unCompressString(final byte[] data, final String encoding) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        } else {
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            GZIPInputStream is = new GZIPInputStream(bais);
            byte[] tmp = new byte[256];
            while (true) {
                int r = is.read(tmp);
                if (r < 0) {
                    break;
                }
                buffer.write(tmp, 0, r);
            }
            is.close();

            byte[] content = buffer.toByteArray();
            return new String(content, 0, content.length, encoding);
        }
    }
}

Related

  1. uncompressByteArray(byte[] ubytes, String type)
  2. uncompressByteArray(byte[] xmlByteArray)
  3. uncompressBytes(byte[] bytesToUncompress)
  4. uncompressGzip(byte[] b)
  5. uncompressObject(byte[] compressed)
  6. unCompressToString(byte[] b)