Example usage for java.util.zip GZIPOutputStream close

List of usage examples for java.util.zip GZIPOutputStream close

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:edu.auburn.ppl.cyclecolumbus.TripUploader.java

/******************************************************************************************
 * Compresses the string to send to the server into byte array
 ******************************************************************************************
 * @param string String to compress//from  w w  w . j av  a 2  s.  c o m
 * @return Byte array to send to server
 * @throws IOException
 ******************************************************************************************/
public static byte[] compress(String string) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(string.getBytes());
    gos.close();
    byte[] compressed = os.toByteArray();
    os.close();
    return compressed;
}

From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java

static byte[] compress(String string) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(string.getBytes("UTF-8"));
    gos.close();
    byte[] compressed = os.toByteArray();
    os.close();//from www  .j a va 2 s  . com
    return compressed;
}

From source file:org.opendatakit.common.utils.WebUtils.java

/**
 * Safely encode a string for use as a query parameter.
 * //from   w w w  .  j a va 2s .  c o  m
 * @param rawString
 * @return encoded string
 */
public static String safeEncode(String rawString) {
    if (rawString == null || rawString.length() == 0) {
        return null;
    }

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(rawString.getBytes(CharEncoding.UTF_8));
        gzip.finish();
        gzip.close();
        String candidate = Base64.encodeBase64URLSafeString(out.toByteArray());
        return candidate;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Unexpected failure: " + e.toString());
    }
}

From source file:com.stacksync.desktop.util.FileUtil.java

public static byte[] gzip(byte[] content) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    gzipOutputStream.write(content);// ww w  .ja  va 2  s  . co m
    gzipOutputStream.close();

    byte[] result = byteArrayOutputStream.toByteArray();
    byteArrayOutputStream.close();

    return result;
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Compresses the input file into a GZIP file container.
 *
 * @param anInPathFileName The file name to be compressed.
 * @param aGzipPathFileName The file name of the ZIP container.
 *
 * @throws IOException Related to opening the file streams and
 * related read/write operations.//  www.  j a v a  2s.  com
 */
public static void gzipFile(String anInPathFileName, String aGzipPathFileName) throws IOException {
    int byteCount;

    FileInputStream fileInputStream = new FileInputStream(anInPathFileName);
    FileOutputStream fileOutputStream = new FileOutputStream(aGzipPathFileName);
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);

    byte[] ioBuf = new byte[FILE_IO_BUFFER_SIZE];
    byteCount = fileInputStream.read(ioBuf);
    while (byteCount > 0) {
        gzipOutputStream.write(ioBuf, 0, byteCount);
        byteCount = fileInputStream.read(ioBuf);
    }

    gzipOutputStream.close();
    fileOutputStream.close();
    fileInputStream.close();
}

From source file:com.beligum.core.utils.AssetPacker.java

private static byte[] encodeGzip(String content) throws IOException {
    byte[] retVal = null;

    ByteArrayOutputStream out = null;
    GZIPOutputStream gzip = null;
    try {/*from w w  w .  j  a  v a2  s  .com*/
        out = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(out);
        gzip.write(content.getBytes("UTF-8"));
        gzip.close();
        out.close();

        retVal = out.toByteArray();
    } catch (Exception e) {
        if (gzip != null) {
            gzip.close();
        }
        if (out != null) {
            out.close();
        }
    }

    return retVal;
}

From source file:com.navercorp.pinpoint.web.filter.Base64.java

public static String encodeBytes(byte[] source, int off, int len, int options) {
    if ((options & 2) == 2) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = null;

        String var33;
        try {//from w  w  w  .  j av a2 s.co m
            gzos = new GZIPOutputStream(new Base64.Base64OutputStream(baos, 1 | options));
            gzos.write(source, off, len);
            gzos.close();
            gzos = null;
            String var32 = new String(baos.toByteArray(), "UTF-8");
            return var32;
        } catch (UnsupportedEncodingException var27) {
            var33 = new String(baos.toByteArray());
        } catch (IOException var28) {
            LOG.error("error encoding byte array", var28);
            var33 = null;
            return var33;
        } finally {
            if (gzos != null) {
                try {
                    gzos.close();
                } catch (Exception var25) {
                    LOG.error("error closing GZIPOutputStream", var25);
                }
            }

            try {
                baos.close();
            } catch (Exception var24) {
                LOG.error("error closing ByteArrayOutputStream", var24);
            }

        }

        return var33;
    } else {
        boolean breakLines = (options & 8) == 0;
        int len43 = len * 4 / 3;
        byte[] outBuff = new byte[len43 + (len % 3 > 0 ? 4 : 0) + (breakLines ? len43 / 76 : 0)];
        int d = 0;
        int e = 0;
        int len2 = len - 2;

        for (int lineLength = 0; d < len2; e += 4) {
            encode3to4(source, d + off, 3, outBuff, e, options);
            lineLength += 4;
            if (breakLines && lineLength == 76) {
                outBuff[e + 4] = 10;
                ++e;
                lineLength = 0;
            }

            d += 3;
        }

        if (d < len) {
            encode3to4(source, d + off, len - d, outBuff, e, options);
            e += 4;
        }

        try {
            return new String(outBuff, 0, e, "UTF-8");
        } catch (UnsupportedEncodingException var26) {
            return new String(outBuff, 0, e);
        }
    }
}

From source file:org.mitre.opensextant.util.FileUtility.java

/**
 *
 * @param text//from w ww.  j  a va 2 s.  c o m
 * @param fname
 * @return
 * @throws IOException
 */
public static boolean writeGzipFile(String text, String fname) throws IOException {
    if (fname == null || text == null) {
        return false;
    }

    FileOutputStream outstream = new FileOutputStream(fname);
    GZIPOutputStream gzout = new GZIPOutputStream(new BufferedOutputStream(outstream), default_buffer);

    gzout.write(text.getBytes(default_encoding));

    gzout.flush();
    gzout.finish();

    gzout.close();
    outstream.close();
    return true;

}

From source file:com.evolveum.midpoint.repo.sql.util.RUtil.java

public static byte[] getByteArrayFromXml(String xml, boolean compress) {
    byte[] array;

    GZIPOutputStream gzip = null;
    try {/*from ww w  .  ja v a  2 s.c o m*/
        if (compress) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(xml.getBytes("utf-8"));
            gzip.close();
            out.close();

            array = out.toByteArray();
        } else {
            array = xml.getBytes("utf-8");
        }
    } catch (Exception ex) {
        throw new SystemException("Couldn't save full xml object, reason: " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(gzip);
    }

    return array;
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

/**
 * GZIP encode./*ww w  .  jav  a2 s  . c  o m*/
 * 
 * @param str
 * @return
 * @throws IOException
 */
public synchronized static byte[] gzipEncode(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return null;
    }
    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);
    gzip.write(str.getBytes("UTF-8"));
    gzip.close();
    return obj.toByteArray();
}