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:org.apache.carbondata.core.util.ObjectSerializationUtil.java

/**
 * Convert object to Base64 String//w ww. j  ava  2  s.c  om
 *
 * @param obj Object to be serialized
 * @return serialized string
 * @throws IOException
 */
public static String convertObjectToString(Object obj) throws IOException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gos = null;
    ObjectOutputStream oos = null;

    try {
        baos = new ByteArrayOutputStream();
        gos = new GZIPOutputStream(baos);
        oos = new ObjectOutputStream(gos);
        oos.writeObject(obj);
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
            if (gos != null) {
                gos.close();
            }
            if (baos != null) {
                baos.close();
            }
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    return CarbonUtil.encodeToString(baos.toByteArray());
}

From source file:org.apache.carbondata.hadoop.util.ObjectSerializationUtil.java

/**
 * Convert object to Base64 String//ww w  .  j  a v a  2  s  .co m
 *
 * @param obj Object to be serialized
 * @return serialized string
 * @throws IOException
 */
public static String convertObjectToString(Object obj) throws IOException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gos = null;
    ObjectOutputStream oos = null;

    try {
        baos = new ByteArrayOutputStream();
        gos = new GZIPOutputStream(baos);
        oos = new ObjectOutputStream(gos);
        oos.writeObject(obj);
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
            if (gos != null) {
                gos.close();
            }
            if (baos != null) {
                baos.close();
            }
        } catch (IOException e) {
            LOG.error(e);
        }
    }

    return new String(Base64.encodeBase64(baos.toByteArray()), CarbonCommonConstants.DEFAULT_CHARSET);
}

From source file:org.cloudata.core.common.io.CWritableUtils.java

public static int writeCompressedByteArray(DataOutput out, byte[] bytes) throws IOException {
    if (bytes != null) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzout = new GZIPOutputStream(bos);
        gzout.write(bytes, 0, bytes.length);
        gzout.close();
        byte[] buffer = bos.toByteArray();
        int len = buffer.length;
        out.writeInt(len);//  www  .j  a  va  2  s . c  o m
        out.write(buffer, 0, len);
        /* debug only! Once we have confidence, can lose this. */
        return ((bytes.length != 0) ? (100 * buffer.length) / bytes.length : 0);
    } else {
        out.writeInt(-1);
        return -1;
    }
}

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   www .j a v a 2s.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: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 {/* w  w w .j  a  v a2s .  co 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:org.commoncrawl.util.shared.ArcFileReaderTests.java

/**
 * Gzip passed bytes. Use only when bytes is small.
 * //from w  w  w . ja v a2 s.  co m
 * @param bytes
 *          What to gzip.
 * @return A gzip member of bytes.
 * @throws IOException
 */
static byte[] gzip(byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOS = new GZIPOutputStream(baos);
    gzipOS.write(bytes, 0, bytes.length);
    gzipOS.close();
    return baos.toByteArray();
}

From source file:dk.netarkivet.common.utils.ZipUtils.java

/** GZip a file into a given dir.  The resulting file will have .gz
 * appended.//from  w  w  w  .  j a  v a  2s .  c  o  m
 *
 * @param f A file to gzip.  This must be a real file, not a directory
 * or the like.
 * @param toDir The directory that the gzipped file will be placed in.
 */
private static void gzipFileInto(File f, File toDir) {
    try {
        GZIPOutputStream out = null;
        try {
            File outF = new File(toDir, f.getName() + GZIP_SUFFIX);
            out = new GZIPOutputStream(new FileOutputStream(outF));
            FileUtils.writeFileToStream(f, out);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // Not really a problem to not be able to close,
                    // so don't abort
                    log.debug("Error closing output file for '" + f + "'", e);
                }
            }
        }
    } catch (IOException e) {
        throw new IOFailure("Error while gzipping file '" + f + "'", e);
    }
}

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

/**
 * byte[]//w  ww.  java 2  s.  c  o m
 *
 * @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:Main.java

public static void compressFile(String inputFilePath, boolean deleteSource) throws IOException {
    byte[] buffer = new byte[1024];
    FileOutputStream fos = null;//from w  w w.  ja v a  2s  .  co m
    GZIPOutputStream gzos = null;
    FileInputStream fis = null;

    try {
        fos = new FileOutputStream(inputFilePath + ".gz");
        gzos = new GZIPOutputStream(fos);

        fis = new FileInputStream(inputFilePath);
        //         String zipEntryName = inputFilePath.substring(inputFilePath.lastIndexOf(File.separator) + 1);         
        //         gzos.putNextEntry(new ZipEntry(zipEntryName));

        int length;
        while ((length = fis.read(buffer)) > 0) {
            gzos.write(buffer, 0, length);
        }
    } finally {
        gzos.close();
        fos.close();
        fis.close();
    }

    if (deleteSource) {
        new File(inputFilePath).delete();
    }
}

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

public static byte[] compress(byte[] bytes) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*  www . j a v  a 2  s  . com*/
        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);
    }
}