Example usage for java.util.zip GZIPOutputStream GZIPOutputStream

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

Introduction

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

Prototype

public GZIPOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size.

Usage

From source file:org.runnerup.export.GoogleFitSynchronizer.java

private Status sendData(StringWriter w, String suffix, RequestMethod method) throws IOException {
    Status status = Status.ERROR;/*from  w ww  . ja  v  a  2 s. co m*/
    for (int attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {
        HttpURLConnection connect = getHttpURLConnection(suffix, method);
        GZIPOutputStream gos = new GZIPOutputStream(connect.getOutputStream());
        gos.write(w.toString().getBytes());
        gos.flush();
        gos.close();

        int code = connect.getResponseCode();
        try {
            if (code == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                continue;
            } else if (code != HttpStatus.SC_OK) {
                Log.i(getName(), SyncHelper.parse(new GZIPInputStream(connect.getErrorStream())).toString());
                status = Status.ERROR;
                break;
            } else {
                Log.i(getName(), SyncHelper.parse(new GZIPInputStream(connect.getInputStream())).toString());
                status = Status.OK;
                break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            connect.disconnect();
        }
    }
    return status;
}

From source file:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java

static byte[] compress(String data) throws IOException {
    byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);

    try (ByteArrayOutputStream bos = new ByteArrayOutputStream(dataBytes.length)) {
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(dataBytes);/*from w w w . j  a va 2s .  c o  m*/
        gzip.close();

        return bos.toByteArray();
    }
}

From source file:com.knowbout.epg.EPG.java

private static void updateSitemap(String sitemap) {
    int lastslash = sitemap.lastIndexOf('/');
    String inprogress = sitemap.substring(0, lastslash) + "/inprogress-" + sitemap.substring(lastslash + 1);
    String marker = "<!-- EVERYTHING BELOW IS AUTOMATICALLY GENERATED -->";
    String baseurl = null;/*from  w w w.j av a  2 s .  c  om*/
    try {
        PrintStream doc = new PrintStream(new GZIPOutputStream(new FileOutputStream(inprogress)));
        BufferedReader orig = new BufferedReader(
                new InputStreamReader(new GZIPInputStream(new FileInputStream(sitemap))));
        String line = orig.readLine();
        while (line != null) {
            if ((line.indexOf("</urlset>") >= 0) || (line.indexOf(marker) >= 0)) {
                break;
            }
            if (baseurl == null) {
                if (line.indexOf("<loc>") >= 0) {
                    int http = line.indexOf("http://");
                    int nextslash = line.indexOf("/", http + 7);
                    baseurl = line.substring(http, nextslash);
                }
            }
            doc.println(line);
            line = orig.readLine();
        }
        doc.println(marker);
        Set<String> teams = new HashSet<String>();
        HibernateUtil.openSession();
        try {
            ScrollableResults scroll = Program.selectAllTeams();
            while (scroll.next()) {
                Program program = (Program) scroll.get(0);
                addToSitemap(doc, baseurl, program);
                teams.add(program.getSportName() + ":" + program.getTeamName());
            }
            scroll = Program.selectAllShowsMoviesSports();
            while (scroll.next()) {
                Program program = (Program) scroll.get(0);
                if (program.isSports()) {
                    if (!teams.contains(program.getSportName() + ":" + program.getHomeTeamName())) {
                        Program home = Program.selectByTeam(program.getSportName(), program.getHomeTeamName());
                        addToSitemap(doc, baseurl, home);
                        teams.add(home.getSportName() + ":" + home.getTeamName());
                    }
                    if (!teams.contains(program.getSportName() + ":" + program.getAwayTeamName())) {
                        Program away = Program.selectByTeam(program.getSportName(), program.getAwayTeamName());
                        addToSitemap(doc, baseurl, away);
                        teams.add(away.getSportName() + ":" + away.getTeamName());
                    }
                } else {
                    addToSitemap(doc, baseurl, program);
                }
            }
        } finally {
            HibernateUtil.closeSession();
        }
        doc.println("</urlset>");
        doc.close();
        orig.close();
        File origFile = new File(sitemap);
        File backupFile = new File(sitemap + ".bak");
        File inprogressFile = new File(inprogress);
        backupFile.delete();
        if (!origFile.renameTo(backupFile)) {
            throw new IOException("Could not rename " + origFile + " to " + backupFile);
        }
        if (!inprogressFile.renameTo(origFile)) {
            throw new IOException("Could not rename " + inprogressFile + " to " + origFile);
        }
    } catch (FileNotFoundException e) {
        log.error("Could not write to " + inprogress, e);
    } catch (IOException e) {
        log.error("IO Exception for " + inprogress + " or " + sitemap, e);
    }
}

From source file:com.asakusafw.runtime.stage.input.StageInputDriver.java

private static String encode(List<StageInput> inputList) throws IOException {
    assert inputList != null;
    String[] dictionary = buildDictionary(inputList);
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    try (DataOutputStream output = new DataOutputStream(new GZIPOutputStream(new Base64OutputStream(sink)))) {
        WritableUtils.writeVLong(output, SERIAL_VERSION);
        WritableUtils.writeStringArray(output, dictionary);
        WritableUtils.writeVInt(output, inputList.size());
        for (StageInput input : inputList) {
            writeEncoded(output, dictionary, input.getPathString());
            writeEncoded(output, dictionary, input.getFormatClass().getName());
            writeEncoded(output, dictionary, input.getMapperClass().getName());
            WritableUtils.writeVInt(output, input.getAttributes().size());
            for (Map.Entry<String, String> attribute : input.getAttributes().entrySet()) {
                writeEncoded(output, dictionary, attribute.getKey());
                writeEncoded(output, dictionary, attribute.getValue());
            }//from  www  . j a  v  a 2  s  .  co m
        }
    }
    return new String(sink.toByteArray(), ASCII);
}

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

public static String encodeObject(Serializable serializableObject, int options) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream b64os = null;//from   w w w .  j a va  2 s .  c om
    ObjectOutputStream oos = null;

    String var6;
    try {
        b64os = new Base64.Base64OutputStream(baos, 1 | options);
        oos = (options & 2) == 2 ? new ObjectOutputStream(new GZIPOutputStream(b64os))
                : new ObjectOutputStream(b64os);
        oos.writeObject(serializableObject);
        String var5 = new String(baos.toByteArray(), "UTF-8");
        return var5;
    } catch (UnsupportedEncodingException var28) {
        var6 = new String(baos.toByteArray());
        return var6;
    } catch (IOException var29) {
        LOG.error("error encoding object", var29);
        var6 = null;
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (Exception var27) {
                LOG.error("error closing ObjectOutputStream", var27);
            }
        }

        if (b64os != null) {
            try {
                b64os.close();
            } catch (Exception var26) {
                LOG.error("error closing Base64OutputStream", var26);
            }
        }

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

    }

    return var6;
}

From source file:arena.utils.FileUtils.java

public static void gzip(File input, File outFile) {
    Log log = LogFactory.getLog(FileUtils.class);
    InputStream inStream = null;//w  ww  . j  a v a  2 s.  c  om
    OutputStream outStream = null;
    GZIPOutputStream gzip = null;
    try {
        long inFileLengthKB = input.length() / 1024L;

        // Open the out file
        if (outFile == null) {
            outFile = new File(input.getParentFile(), input.getName() + ".gz");
        }
        inStream = new FileInputStream(input);
        outStream = new FileOutputStream(outFile, true);
        gzip = new GZIPOutputStream(outStream);

        // Iterate through in buffers and write out to the gzipped output stream
        byte buffer[] = new byte[102400]; // 100k buffer
        int read = 0;
        long readSoFar = 0;
        while ((read = inStream.read(buffer)) != -1) {
            readSoFar += read;
            gzip.write(buffer, 0, read);
            log.debug("Gzipped " + (readSoFar / 1024L) + "KB / " + inFileLengthKB + "KB of logfile "
                    + input.getName());
        }

        // Close the streams
        inStream.close();
        inStream = null;
        gzip.close();
        gzip = null;
        outStream.close();
        outStream = null;

        // Delete the old file
        input.delete();
        log.debug("Gzip of logfile " + input.getName() + " complete");
    } catch (IOException err) {
        // Delete the gzip file
        log.error("Error during gzip of logfile " + input, err);
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException err2) {
            }
        }
        if (gzip != null) {
            try {
                gzip.close();
            } catch (IOException err2) {
            }
        }
        if (outStream != null) {
            try {
                outStream.close();
            } catch (IOException err2) {
            }
        }
    }
}

From source file:com.sun.faces.renderkit.ResponseStateManagerImpl.java

public void writeState(FacesContext context, SerializedView view) throws IOException {

    StateManager stateManager = Util.getStateManager(context);
    ResponseWriter writer = context.getResponseWriter();

    writer.startElement("input", context.getViewRoot());
    writer.writeAttribute("type", "hidden", null);
    writer.writeAttribute("name", RIConstants.FACES_VIEW, null);
    writer.writeAttribute("id", RIConstants.FACES_VIEW, null);

    if (stateManager.isSavingStateInClient(context)) {
        GZIPOutputStream zos = null;
        ObjectOutputStream oos = null;
        boolean compress = isCompressStateSet(context);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        if (compress) {
            if (log.isDebugEnabled()) {
                log.debug("Compressing state before saving..");
            }//ww  w  .  j  a  v  a2  s . c  o m
            zos = new GZIPOutputStream(bos);
            oos = new ObjectOutputStream(zos);
        } else {
            oos = new ObjectOutputStream(bos);
        }
        oos.writeObject(view.getStructure());
        oos.writeObject(view.getState());
        oos.close();
        if (compress) {
            zos.close();
        }
        bos.close();

        String valueToWrite = (new String(Base64.encode(bos.toByteArray()), "ISO-8859-1"));
        writer.writeAttribute("value", valueToWrite, null);
    } else {
        writer.writeAttribute("value", view.getStructure(), null);
    }
    writer.endElement("input");

}

From source file:com.mbrlabs.mundus.assets.EditorAssetManager.java

public TerrainAsset createTerrainAsset(int vertexResolution, int size) throws IOException {
    String terraFilename = "terrain_" + UUID.randomUUID().toString() + ".terra";
    String metaFilename = terraFilename + ".meta";

    // create meta file
    String metaPath = FilenameUtils.concat(rootFolder.path(), metaFilename);
    MetaFile meta = createNewMetaFile(new FileHandle(metaPath), AssetType.TERRAIN);
    meta.setTerrainSize(size);//from ww  w .j  a v a  2  s  . c om
    meta.save();

    // create terra file
    String terraPath = FilenameUtils.concat(rootFolder.path(), terraFilename);
    File terraFile = new File(terraPath);
    FileUtils.touch(terraFile);

    // create initial height data
    float[] data = new float[vertexResolution * vertexResolution];
    for (int i = 0; i < data.length; i++) {
        data[i] = 0;
    }

    // write terra file
    DataOutputStream outputStream = new DataOutputStream(
            new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(terraFile))));
    for (float f : data) {
        outputStream.writeFloat(f);
    }
    outputStream.flush();
    outputStream.close();

    // load & apply standard chessboard texture
    TerrainAsset asset = new TerrainAsset(meta, new FileHandle(terraFile));
    asset.load();

    TextureAsset chessboard = (TextureAsset) findAssetByID(STANDARD_ASSET_TEXTURE_CHESSBOARD);
    if (chessboard != null) {
        // create splatmap
        PixmapTextureAsset splatmap = createPixmapTextureAsset(SplatMap.DEFAULT_SIZE);
        asset.setSplatmap(splatmap);
        asset.setSplatBase(chessboard);
        asset.applyDependencies();
    }

    addAsset(asset);
    return asset;
}

From source file:com.streamsets.pipeline.stage.cloudstorage.destination.GoogleCloudStorageTarget.java

@Override
public void write(Batch batch) throws StageException {
    String pathExpression = GcsUtil.normalizePrefix(gcsTargetConfig.commonPrefix)
            + gcsTargetConfig.partitionTemplate;
    if (gcsTargetConfig.dataFormat == DataFormat.WHOLE_FILE) {
        handleWholeFileFormat(batch, elVars);
    } else {/*from   w w w  .  j  a  v  a2s .  com*/
        Multimap<String, Record> pathToRecordMap = ELUtils.partitionBatchByExpression(partitionEval, elVars,
                pathExpression, timeDriverElEval, elVars, gcsTargetConfig.timeDriverTemplate,
                Calendar.getInstance(TimeZone.getTimeZone(ZoneId.of(gcsTargetConfig.timeZoneID))), batch);

        pathToRecordMap.keySet().forEach(path -> {
            Collection<Record> records = pathToRecordMap.get(path);
            String fileName = GcsUtil.normalizePrefix(path) + gcsTargetConfig.fileNamePrefix + '_'
                    + UUID.randomUUID();
            if (StringUtils.isNotEmpty(gcsTargetConfig.fileNameSuffix)) {
                fileName = fileName + "." + gcsTargetConfig.fileNameSuffix;
            }
            try {
                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
                OutputStream os = bOut;
                if (gcsTargetConfig.compress) {
                    fileName = fileName + ".gz";
                    os = new GZIPOutputStream(bOut);
                }
                BlobId blobId = BlobId.of(gcsTargetConfig.bucketTemplate, fileName);
                BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(getContentType()).build();
                final AtomicInteger recordsWithoutErrors = new AtomicInteger(0);
                try (DataGenerator dg = gcsTargetConfig.dataGeneratorFormatConfig.getDataGeneratorFactory()
                        .getGenerator(os)) {
                    records.forEach(record -> {
                        try {
                            dg.write(record);
                            recordsWithoutErrors.incrementAndGet();
                        } catch (DataGeneratorException | IOException e) {
                            LOG.error("Error writing record {}. Reason {}", record.getHeader().getSourceId(),
                                    e);
                            getContext().toError(record, Errors.GCS_02, record.getHeader().getSourceId(), e);
                        }
                    });
                } catch (IOException e) {
                    LOG.error("Error happened when creating Output stream. Reason {}", e);
                    records.forEach(record -> getContext().toError(record, e));
                }

                try {
                    if (recordsWithoutErrors.get() > 0) {
                        Blob blob = storage.create(blobInfo, bOut.toByteArray());
                        GCSEvents.GCS_OBJECT_WRITTEN.create(getContext())
                                .with(GCSEvents.BUCKET, blob.getBucket())
                                .with(GCSEvents.OBJECT_KEY, blob.getName())
                                .with(GCSEvents.RECORD_COUNT, recordsWithoutErrors.longValue()).createAndSend();
                    }
                } catch (StorageException e) {
                    LOG.error("Error happened when writing to Output stream. Reason {}", e);
                    records.forEach(record -> getContext().toError(record, e));
                }
            } catch (IOException e) {
                LOG.error("Error happened when creating Output stream. Reason {}", e);
                records.forEach(record -> getContext().toError(record, e));
            }
        });
    }
}

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

public void addGZIPPostParam(String key, String value) {
    try {//from w ww .ja va  2s.  c o  m
        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) {
    }
}