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.syncany.operations.init.ApplicationLink.java

private byte[] getPlaintextStorageXml() throws Exception {
    ByteArrayOutputStream plaintextByteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream plaintextOutputStream = new DataOutputStream(plaintextByteArrayOutputStream);
    plaintextOutputStream.writeInt(transferSettings.getType().getBytes().length);
    plaintextOutputStream.write(transferSettings.getType().getBytes());

    GZIPOutputStream plaintextGzipOutputStream = new GZIPOutputStream(plaintextOutputStream);
    new Persister(new Format(0)).write(transferSettings, plaintextGzipOutputStream);
    plaintextGzipOutputStream.close();

    return plaintextByteArrayOutputStream.toByteArray();
}

From source file:com.kkbox.toolkit.internal.api.APIRequest.java

public void addGZIPPostParam(String key, String value) {
    try {/*w w  w .j  a  v  a 2 s  .com*/
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add((new BasicNameValuePair(key, value)));
        GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gZIPOutputStream.write(EntityUtils.toByteArray(new UrlEncodedFormEntity(postParams, HTTP.UTF_8)));
        gZIPOutputStream.close();
        byte[] byteDataForGZIP = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        gzipStreamEntity = new InputStreamEntity(new ByteArrayInputStream(byteDataForGZIP),
                byteDataForGZIP.length);
        gzipStreamEntity.setContentType("application/x-www-form-urlencoded");
        gzipStreamEntity.setContentEncoding("gzip");
    } catch (Exception e) {
    }
}

From source file:com.netflix.astyanax.AbstractColumnListMutation.java

@Override
public ColumnListMutation<C> putCompressedColumn(C columnName, String value, Integer ttl) {
    Preconditions.checkNotNull(value, "Can't insert null value");

    if (value == null) {
        putEmptyColumn(columnName, ttl);
        return this;
    }/*w  w w.j a  va  2 s .c  o m*/

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
        gzip = new GZIPOutputStream(out);
        gzip.write(StringUtils.getBytesUtf8(value));
        gzip.close();
        return this.putColumn(columnName, ByteBuffer.wrap(out.toByteArray()), ttl);
    } catch (IOException e) {
        throw new RuntimeException("Error compressing column " + columnName, e);
    }
}

From source file:com.github.jasonruckman.gzip.AbstractBenchmark.java

protected byte[] doWriteSidney() {
    try {/*from  www .jav a  2  s .  c o  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = new GZIPOutputStream(baos);
        Writer<T> writer = safeWriter.get();
        writer.open(gzos);
        for (T t : sampleData) {
            writer.write(t);
        }
        writer.close();
        gzos.close();
        return baos.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.umd.cs.marmoset.modelClasses.TestOutcome.java

public static byte[] compress(String txt) {
    byte[] rawBYtes = txt.getBytes();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {// w  w  w . ja  va2 s . c  om
        GZIPOutputStream gzout = new GZIPOutputStream(out);

        gzout.write(rawBYtes);
        gzout.close();
    } catch (IOException e) {
        e.printStackTrace();
        return rawBYtes;
    }
    return out.toByteArray();
}

From source file:com.github.jasonruckman.gzip.AbstractBenchmark.java

protected byte[] doUnsafeWriteSidney() {
    try {/*from   ww w . j  a v  a  2s  .  c  om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = new GZIPOutputStream(baos);
        Writer<T> writer = unsafeWriter.get();
        writer.open(gzos);
        for (T t : sampleData) {
            writer.write(t);
        }
        writer.close();
        gzos.close();
        return baos.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.broadleafcommerce.common.sitemap.service.SiteMapServiceImpl.java

/**
 * Gzip a file and then delete the file/*from   w w w .  j a v  a  2s  .  com*/
 * 
 * @param fileName
 */
protected void gzipAndDeleteFiles(FileWorkArea fileWorkArea, List<String> fileNames) {
    for (String fileName : fileNames) {
        try {
            String fileNameWithPath = FilenameUtils
                    .normalize(fileWorkArea.getFilePathLocation() + File.separator + fileName);

            FileInputStream fis = new FileInputStream(fileNameWithPath);
            FileOutputStream fos = new FileOutputStream(fileNameWithPath + ENCODING_EXTENSION);
            GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                gzipOS.write(buffer, 0, len);
            }
            //close resources
            gzipOS.close();
            fos.close();
            fis.close();

            File originalFile = new File(fileNameWithPath);
            originalFile.delete();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.jabsorb.ng.JSONRPCServlet.java

/**
 * Gzip something./*  ww w. java2  s  .  c  om*/
 * 
 * @param in
 *            original content
 * @return size gzipped content
 */
private byte[] gzip(final byte[] in) {

    if (in != null && in.length > 0) {
        long tstart = System.currentTimeMillis();
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            GZIPOutputStream gout = new GZIPOutputStream(bout);
            gout.write(in);
            gout.flush();
            gout.close();
            if (log.isDebugEnabled()) {
                log.debug("gzip", "gzipping took " + (System.currentTimeMillis() - tstart) + " msec");
            }
            return bout.toByteArray();
        } catch (IOException io) {
            log.error("gzip", "io exception gzipping byte array", io);
        }
    }
    return new byte[0];
}

From source file:tvraterplugin.Updater.java

/**
 * Does the Update//www  .  jav  a2s  . c o m
 */
public void run() {
    String name = mSettings.getName();
    String password = mSettings.getPassword();
    if (StringUtils.isEmpty(name) || (StringUtils.isEmpty(password))) {

        JOptionPane.showMessageDialog(mPlugin.getParentFrameForTVRater(),
                mLocalizer.msg("noUser", "Please Enter your Userdata in the\nconfiguration of this Plugin"),
                mLocalizer.msg("error", "Error while updating TV Rater"), JOptionPane.ERROR_MESSAGE);
        return;
    }

    try {
        if (!NetworkUtilities.checkConnection(new URL("http://www.tvaddicted.de"))) {
            JOptionPane.showMessageDialog(null, mLocalizer.msg("noConnectionMessage", "No Connection!"),
                    mLocalizer.msg("noConnectionTitle", "No Connection!"), JOptionPane.ERROR_MESSAGE);
            return;
        }

        mUpdateList = createUpdateList();
        if (mUpdateList.size() == 0) {
            mWasSuccessfull = true;
            return;
        }

        URL url = new URL(LOCATION);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);

        OutputStream out = connection.getOutputStream();

        GZIPOutputStream outZipped = new GZIPOutputStream(out);
        writeData(outZipped);
        outZipped.close();

        Node data = readURLConnection(connection);

        if (data.getNodeName().equals("error")) {
            String message = getTextFromNode(data);

            JOptionPane.showMessageDialog(mPlugin.getParentFrameForTVRater(),
                    mLocalizer.msg("serverError", "The Server has send the following error:") + "\n" + message,
                    mLocalizer.msg("error", "Error while updating TV Rater"), JOptionPane.ERROR_MESSAGE);
        } else {
            readData(data);
            mWasSuccessfull = true;
            mPlugin.updateCurrentDate();
        }

        out.close();
    } catch (Exception e) {
        ErrorHandler.handle(
                mLocalizer.msg("updateError", "An error occured while updating the TVRater Database"), e);
        e.printStackTrace();
    }
}

From source file:org.example.Serializer.java

private byte[] zip(byte[] serialized) throws Exception {
    ByteArrayOutputStream zipBaos = new ByteArrayOutputStream();

    GZIPOutputStream gzos = new GZIPOutputStream(zipBaos);
    gzos.write(serialized);/*from   ww  w .j a va  2  s . c o m*/
    gzos.close();

    return zipBaos.toByteArray();
}