Java GZip gzipString(String src, byte default_value[])

Here you can find the source of gzipString(String src, byte default_value[])

Description

gzip String

License

BSD License

Declaration

static public byte[] gzipString(String src, byte default_value[]) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;

import java.util.zip.GZIPOutputStream;
import com.amazonaws.util.IOUtils;

public class Main {
    static private Charset utf8 = Charset.forName("UTF-8");

    static public byte[] gzipString(String src, byte default_value[]) {
        if (src == null)
            return default_value;
        return gzip(src.getBytes(utf8), default_value);
    }/*from   w  w w.  j  a va2s .  com*/

    static public byte[] gzip(byte src[], byte default_value[]) {
        try {
            if (src == null)
                return default_value;

            ByteArrayOutputStream out_raw = new ByteArrayOutputStream();
            GZIPOutputStream out = new GZIPOutputStream(out_raw);

            ByteArrayInputStream in = new ByteArrayInputStream(src);

            IOUtils.copy(in, out);

            in.close();
            out.close();

            return out_raw.toByteArray();
        } catch (Exception e) {
            return default_value;
        }
    }
}

Related

  1. gzip(String s)
  2. gzip(String str)
  3. gzipDecompress(byte[] compressed)
  4. gzipFileReader(String file)
  5. gzipReader(final InputStream inputStream)