inflate byte array - Android File Input Output

Android examples for File Input Output:Byte Array Compress

Description

inflate byte array

Demo Code


import android.util.Log;
import java.io.*;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

public class Main{
    private final static String TAG = IoUtil.class.getName();
    /**/*from  w w  w .j  a v a 2  s  .co  m*/
     * @param content
     * @return
     * @throws java.io.IOException
     */
    public static byte[] inflate(byte[] content) {
        ByteArrayInputStream bytein = new ByteArrayInputStream(content);
        InflaterInputStream decompresser = new InflaterInputStream(bytein);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            IoUtil.copy(decompresser, output);
            decompresser.close();
        } catch (IOException e) {
            Log.w(TAG, e.getMessage());
        }
        return output.toByteArray();
        // return content;
    }
    public static String toString(InputStream input) throws IOException {
        StringBuilderWriter sw = new StringBuilderWriter();
        copy(input, sw);
        return sw.toString();
    }
    public static int copy(InputStream input, OutputStream output)
            throws IOException {
        long count = copyLarge(input, output);
        if (count > Integer.MAX_VALUE) {
            return -1;
        }
        return (int) count;
    }
    public static void copy(InputStream input, Writer output)
            throws IOException {
        InputStreamReader in = new InputStreamReader(input);
        copy(in, output);
    }
    public static int copy(Reader input, Writer output) throws IOException {
        long count = copyLarge(input, output);
        if (count > 2147483647L) {
            return -1;
        }
        return (int) count;
    }
    public static void close(InputStream input) {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
            }
        }
    }
    public static void close(OutputStream output) {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
            }
        }
    }
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copy(input, output);
        return output.toByteArray();
    }
    public static long copyLarge(InputStream input, OutputStream output)
            throws IOException {
        byte[] buffer = new byte[4096];
        long count = 0L;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
    public static long copyLarge(Reader input, Writer output)
            throws IOException {
        char[] buffer = new char[4096];
        long count = 0L;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
}

Related Tutorials