Example usage for java.util.zip GZIPOutputStream finish

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

Introduction

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

Prototype

public void finish() throws IOException 

Source Link

Document

Finishes writing compressed data to the output stream without closing the underlying stream.

Usage

From source file:cn.sharesdk.analysis.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;//w  w  w .  ja  v a  2  s.  com
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);

    } catch (IOException e) {
        e.printStackTrace();
        Ln.e("NetworkHelper", "Base64Gzip == >>", e);
    }

    //Ln.i("after base64gizp", result);
    return result;
}

From source file:com.spstudio.common.image.ImageUtils.java

/**
 * byte[]//from www.ja va 2 s.  com
 *
 * @param ??
 * @return ??
 */
public static byte[] compress(byte[] data) {
    System.out.println("before:" + data.length);

    GZIPOutputStream gzip = null;
    ByteArrayOutputStream baos = null;
    byte[] newData = null;

    try {
        baos = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(baos);

        gzip.write(data);
        gzip.finish();
        gzip.flush();

        newData = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            gzip.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    System.out.println("after:" + newData.length);
    return newData;
}

From source file:de.kapsi.net.daap.DaapUtil.java

/**
 * Serializes the <code>chunk</code> and compresses it optionally.
 * The serialized data is returned as a byte-Array.
 *//*from  w  w  w. j  a v a  2 s  .c  om*/
public static final byte[] serialize(Chunk chunk, boolean compress) throws IOException {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream(chunk.getSize());

    if (compress) {
        GZIPOutputStream gzip = new GZIPOutputStream(buffer);
        chunk.serialize(gzip);
        gzip.finish();
        gzip.close();
    } else {
        chunk.serialize(buffer);
        buffer.flush();
        buffer.close();
    }

    return buffer.toByteArray();
}

From source file:VASSAL.tools.image.tilecache.TileUtils.java

/**
 * Write a tile image to a stream.// w  w  w .j av  a2s .co m
 *
 * @param tile the image
 * @param out the stream
 *
 * @throws ImageIOException if the write fails
 */
public static void write(BufferedImage tile, OutputStream out) throws IOException {
    ByteBuffer bb;

    // write the header
    bb = ByteBuffer.allocate(18);

    bb.put("VASSAL".getBytes()).putInt(tile.getWidth()).putInt(tile.getHeight()).putInt(tile.getType());

    out.write(bb.array());

    // write the tile data
    final DataBufferInt db = (DataBufferInt) tile.getRaster().getDataBuffer();
    final int[] data = db.getData();

    bb = ByteBuffer.allocate(4 * data.length);
    bb.asIntBuffer().put(data);

    final GZIPOutputStream zout = new GZIPOutputStream(out);
    zout.write(bb.array());
    zout.finish();
}

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

/**
 * Compresses the specified bytes using gzip.
 *
 * @param input   the bytes to compress// w ww. j av  a2 s  .c  o  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: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. jav  a  2s . c  o m*/
        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:cn.sharesdk.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;/*from w  ww .j ava2s.  c om*/
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);
    } catch (IOException e) {
        e.printStackTrace();
        Ln.i("NetworkHelper", "Base64Gzip == >>", e);
    }
    return result;
}

From source file:org.apache.myfaces.shared_impl.util.StateUtils.java

public static byte[] compress(byte[] bytes) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from  w  w  w.  j  a v a  2  s.c  o m
        GZIPOutputStream gzip = new GZIPOutputStream(baos);
        gzip.write(bytes, 0, bytes.length);
        gzip.finish();
        byte[] fewerBytes = baos.toByteArray();
        gzip.close();
        baos.close();
        gzip = null;
        baos = null;
        return fewerBytes;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

From source file:org.wso2.carbon.apimgt.impl.GZIPUtils.java

public static void compressFile(String sourcePath, String destinationPath) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Compressing file : " + sourcePath + " to : " + destinationPath);
    }// w  w  w  .  j ava2  s  .c o m
    byte[] buffer = new byte[BUFFER_SIZE];
    GZIPOutputStream gzipOutputStream = null;
    FileOutputStream fileOutputStream = null;
    FileInputStream fileInputStream = null;

    try {
        fileOutputStream = new FileOutputStream(destinationPath);
        gzipOutputStream = new GZIPOutputStream(fileOutputStream);
        fileInputStream = new FileInputStream(sourcePath);

        int length = 0;
        while ((length = fileInputStream.read(buffer)) > 0) {
            gzipOutputStream.write(buffer, 0, length);
        }
        gzipOutputStream.finish();
    } catch (IOException e) {
        throw new APIManagementException(
                "Error while compressing file at " + sourcePath + " to" + destinationPath, e);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
        IOUtils.closeQuietly(gzipOutputStream);
    }
}

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

/**
 * Safely encode a string for use as a query parameter.
 * //  w ww. j  a v a  2 s .co 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());
    }
}