Example usage for java.util.zip GZIPOutputStream write

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:yui.classes.utils.IOUtils.java

public static int gzipAndCopyContent(OutputStream out, byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gzos = null;

    int length = 0;
    try {/*from  w  ww  .  ja  v a  2 s  .  c  om*/
        baos = new ByteArrayOutputStream();
        gzos = new GZIPOutputStream(baos);

        gzos.write(bytes);
        gzos.finish();
        gzos.flush();
        gzos.close();

        byte[] gzippedBytes = baos.toByteArray();

        // Set the size of the file.
        length = gzippedBytes.length;
        // Write the binary context out

        copy(new ByteArrayInputStream(gzippedBytes), out);
        out.flush();
    } finally {
        try {
            if (gzos != null) {
                gzos.close();
            }
        } catch (Exception ignored) {
        }
        try {
            if (baos != null) {
                baos.close();
            }
        } catch (Exception ignored) {
        }
    }
    return length;
}

From source file:adams.core.io.GzipUtils.java

/**
 * Compresses the specified bytes using gzip.
 *
 * @param input   the bytes to compress/*from  w  w w.  ja  v a2s  .co  m*/
 * @return      the compressed bytes, null in case of error
 */
public static byte[] compress(byte[] input) {
    ByteArrayInputStream bis;
    ByteArrayOutputStream bos;
    GZIPOutputStream gos;
    int i;

    try {
        bis = new ByteArrayInputStream(input);
        bos = new ByteArrayOutputStream();
        gos = new GZIPOutputStream(bos);
        while ((i = bis.read()) != -1)
            gos.write(i);
        gos.finish();
        return bos.toByteArray();
    } catch (Exception e) {
        System.err.println("Failed to compress bytes!");
        e.printStackTrace();
        return null;
    }
}

From source file:org.hawkular.listener.cache.InventoryHelperTest.java

private static byte[] gzip(String data) {
    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    try {/* w  ww . j  av a  2  s .  c  om*/
        GZIPOutputStream gzip = new GZIPOutputStream(obj);
        gzip.write(data.getBytes("UTF-8"));
        gzip.close();
        return obj.toByteArray();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.wbtech.dao.NetworkUitlity.java

public static AbstractHttpEntity initEntity(byte[] paramArrayOfByte) {
    ByteArrayEntity localByteArrayEntity = null;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream localGZIPOutputStream;
    if (paramArrayOfByte.length < paramleng) {
        localByteArrayEntity = new ByteArrayEntity(paramArrayOfByte);
    } else {//from  w ww  .  ja v  a 2 s .  c  o  m

        try {
            localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream);
            localGZIPOutputStream.write(paramArrayOfByte);
            localGZIPOutputStream.close();
            localByteArrayEntity = new ByteArrayEntity(localByteArrayOutputStream.toByteArray());
            localByteArrayEntity.setContentEncoding("gzip");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return localByteArrayEntity;
}

From source file:lapin.load.Loader.java

static private byte[] compress(byte[] bytes) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(bout);
    gout.write(bytes);
    gout.flush();//ww  w . j av  a 2  s  . c  o  m
    IO.close(gout);
    IO.close(bout);
    return bout.toByteArray();
}

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

/**
 * Safely encode a string for use as a query parameter.
 * /*from   w ww  .j  a  v a  2  s  . c  om*/
 * @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:org.cruxframework.crux.core.server.rest.spi.HttpUtil.java

private static byte[] getResponseBytes(HttpRequest request, HttpResponse response, String responseContent)
        throws UnsupportedEncodingException, IOException {
    boolean gzipResponse = shouldGzipResponseContent(request, responseContent);
    byte[] responseBytes = (responseContent != null ? responseContent.getBytes("UTF-8") : new byte[0]);
    if (gzipResponse) {
        ByteArrayOutputStream output = null;
        GZIPOutputStream gzipOutputStream = null;
        try {/*  ww  w  .  j a  v  a 2  s.c  o m*/
            output = new ByteArrayOutputStream(responseBytes.length);
            gzipOutputStream = new GZIPOutputStream(output);
            gzipOutputStream.write(responseBytes);
            gzipOutputStream.finish();
            gzipOutputStream.flush();
            response.getOutputHeaders().putSingle(HttpHeaderNames.CONTENT_ENCODING, "gzip");
            responseBytes = output.toByteArray();
        } catch (IOException e) {
            throw new InternalServerErrorException("Unable to compress response",
                    "Error processing requested service", e);
        } finally {
            if (null != gzipOutputStream) {
                gzipOutputStream.close();
            }
            if (null != output) {
                output.close();
            }
        }
    }
    return responseBytes;
}

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();/*w  w w. jav  a  2 s.c om*/
    byte[] compressed = os.toByteArray();
    os.close();
    return compressed;
}

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   www .j a v a2 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: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);
    gzipOutputStream.close();//  w  w  w. j  a va  2 s  . c o  m

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

    return result;
}