Here you can find the source of decompress(byte[] bytes)
Parameter | Description |
---|---|
bytes | - the compressed bytes to decompress. |
Parameter | Description |
---|---|
RuntimeException | - if there is a decompression issue (rare). |
public static byte[] decompress(byte[] bytes) throws RuntimeException
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; public class Main { /**/*from w ww . j a v a2s. com*/ * Decompresses an array of bytes. * * @param bytes - the compressed bytes to decompress. * @return byte[] - the array of bytes containing the decompressed value. * @throws RuntimeException - if there is a decompression issue (rare). */ public static byte[] decompress(byte[] bytes) throws RuntimeException { if (bytes == null || bytes.length == 0) { return bytes; } try { InputStream in = getInputStream(bytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (in.available() > 0) { bos.write(in.read()); } byte[] data = bos.toByteArray(); return data; } catch (IOException e) { throw new IllegalStateException("cannot decompress ", e); } } /** * Returns input stream to decompress the data as read. * * @param data the compressed bytes * @return InputStream, never null. * @throws IOException */ public static InputStream getInputStream(byte[] data) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); GZIPInputStream retval = new GZIPInputStream(bis); return retval; } }