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:com.navercorp.pinpoint.web.filter.Base64.java

public static String encodeBytes(byte[] source, int off, int len, int options) {
    if ((options & 2) == 2) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = null;

        String var33;
        try {//from w w w.ja  v  a2 s  . c om
            gzos = new GZIPOutputStream(new Base64.Base64OutputStream(baos, 1 | options));
            gzos.write(source, off, len);
            gzos.close();
            gzos = null;
            String var32 = new String(baos.toByteArray(), "UTF-8");
            return var32;
        } catch (UnsupportedEncodingException var27) {
            var33 = new String(baos.toByteArray());
        } catch (IOException var28) {
            LOG.error("error encoding byte array", var28);
            var33 = null;
            return var33;
        } finally {
            if (gzos != null) {
                try {
                    gzos.close();
                } catch (Exception var25) {
                    LOG.error("error closing GZIPOutputStream", var25);
                }
            }

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

        }

        return var33;
    } else {
        boolean breakLines = (options & 8) == 0;
        int len43 = len * 4 / 3;
        byte[] outBuff = new byte[len43 + (len % 3 > 0 ? 4 : 0) + (breakLines ? len43 / 76 : 0)];
        int d = 0;
        int e = 0;
        int len2 = len - 2;

        for (int lineLength = 0; d < len2; e += 4) {
            encode3to4(source, d + off, 3, outBuff, e, options);
            lineLength += 4;
            if (breakLines && lineLength == 76) {
                outBuff[e + 4] = 10;
                ++e;
                lineLength = 0;
            }

            d += 3;
        }

        if (d < len) {
            encode3to4(source, d + off, len - d, outBuff, e, options);
            e += 4;
        }

        try {
            return new String(outBuff, 0, e, "UTF-8");
        } catch (UnsupportedEncodingException var26) {
            return new String(outBuff, 0, e);
        }
    }
}

From source file:com.parse.ParseOkHttpClientTest.java

@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
    // Make mock response
    Buffer buffer = new Buffer();
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
    gzipOut.write("content".getBytes());
    gzipOut.close();//from   w ww. ja  va2 s . c  o  m
    buffer.write(byteOut.toByteArray());
    MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer)
            .setHeader("Content-Encoding", "gzip");

    // Start mock server
    server.enqueue(mockResponse);
    server.start();

    ParseHttpClient client = new ParseOkHttpClient(10000, null);

    final Semaphore done = new Semaphore(0);
    // Add plain interceptor to disable decompress response stream
    client.addExternalInterceptor(new ParseNetworkInterceptor() {
        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
            // Make sure the response we get from the interceptor is the raw gzip stream
            byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
            assertArrayEquals(byteOut.toByteArray(), content);

            // We need to set a new stream since we have read it
            return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray()))
                    .build();
        }
    });

    // We do not need to add Accept-Encoding header manually, httpClient library should do that.
    String requestUrl = server.getUrl("/").toString();
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl)
            .setMethod(ParseHttpRequest.Method.GET).build();

    // Execute request
    ParseHttpResponse parseResponse = client.execute(parseRequest);

    // Make sure the response we get is ungziped by OkHttp library
    byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
    assertArrayEquals("content".getBytes(), content);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));

    server.shutdown();
}

From source file:org.jsnap.http.base.HttpServlet.java

protected void doService(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response)
        throws HttpException, IOException {
    // Client might keep the executing thread blocked for very long unless this header is added.
    response.addHeader(new Header(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE));
    // Create a wrapped request object.
    String uri, data;/*from   w  ww.ja va  2  s.  c o  m*/
    String method = request.getRequestLine().getMethod();
    if (method.equals(HttpGet.METHOD_NAME)) {
        BasicHttpRequest get = (BasicHttpRequest) request;
        data = get.getRequestLine().getUri();
        int ix = data.indexOf('?');
        uri = (ix < 0 ? data : data.substring(0, ix));
        data = (ix < 0 ? "" : data.substring(ix + 1));
    } else if (method.equals(HttpPost.METHOD_NAME)) {
        BasicHttpEntityEnclosingRequest post = (BasicHttpEntityEnclosingRequest) request;
        HttpEntity postedEntity = post.getEntity();
        uri = post.getRequestLine().getUri();
        data = EntityUtils.toString(postedEntity);
    } else {
        response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
        response.setHeader(new Header(HTTP.CONTENT_LEN, "0"));
        return;
    }
    String cookieLine = "";
    if (request.containsHeader(COOKIE)) {
        Header[] cookies = request.getHeaders(COOKIE);
        for (Header cookie : cookies) {
            if (cookieLine.length() > 0)
                cookieLine += "; ";
            cookieLine += cookie.getValue();
        }
    }
    HttpRequest req = new HttpRequest(uri, underlying, data, cookieLine);
    // Create a wrapped response object.
    ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
    HttpResponse resp = new HttpResponse(out);
    // Do implementation specific processing.
    doServiceImpl(req, resp);
    out.flush(); // It's good practice to do this.
    // Do the actual writing to the actual response object.
    if (resp.redirectTo != null) {
        // Redirection is requested.
        resp.statusCode = HttpStatus.SC_MOVED_TEMPORARILY;
        response.setStatusCode(resp.statusCode);
        Header redirection = new Header(LOCATION, resp.redirectTo);
        response.setHeader(redirection);
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG,
                "Status Code: " + Integer.toString(resp.statusCode));
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG, redirection.toString());
    } else {
        // There will be a response entity.
        response.setStatusCode(resp.statusCode);
        HttpEntity entity;
        Header contentTypeHeader;
        boolean text = resp.contentType.startsWith(Formatter.TEXT);
        if (text) { // text/* ...
            entity = new StringEntity(out.toString(resp.characterSet), resp.characterSet);
            contentTypeHeader = new Header(HTTP.CONTENT_TYPE,
                    resp.contentType + HTTP.CHARSET_PARAM + resp.characterSet);
        } else { // application/octet-stream, image/* ...
            entity = new ByteArrayEntity(out.toByteArray());
            contentTypeHeader = new Header(HTTP.CONTENT_TYPE, resp.contentType);
        }
        boolean acceptsGzip = clientAcceptsGzip(request);
        long contentLength = entity.getContentLength();
        // If client accepts gzipped content, the implementing object requested that response
        // gets gzipped and size of the response exceeds implementing object's size threshold
        // response entity will be gzipped.
        boolean gzipped = false;
        if (acceptsGzip && resp.zipSize > 0 && contentLength >= resp.zipSize) {
            ByteArrayOutputStream zipped = new ByteArrayOutputStream(BUFFER_SIZE);
            GZIPOutputStream gzos = new GZIPOutputStream(zipped);
            entity.writeTo(gzos);
            gzos.close();
            entity = new ByteArrayEntity(zipped.toByteArray());
            contentLength = zipped.size();
            gzipped = true;
        }
        // This is where true writes are made.
        Header contentLengthHeader = new Header(HTTP.CONTENT_LEN, Long.toString(contentLength));
        Header contentEncodingHeader = null;
        response.setHeader(contentTypeHeader);
        response.setHeader(contentLengthHeader);
        if (gzipped) {
            contentEncodingHeader = new Header(CONTENT_ENCODING, Formatter.GZIP);
            response.setHeader(contentEncodingHeader);
        }
        response.setEntity(entity);
        // Log critical headers.
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG,
                "Status Code: " + Integer.toString(resp.statusCode));
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG, contentTypeHeader.toString());
        if (gzipped)
            Logger.getLogger(HttpServlet.class).log(Level.DEBUG, contentEncodingHeader.toString());
        Logger.getLogger(HttpServlet.class).log(Level.DEBUG, contentLengthHeader.toString());
    }
    // Log cookies.
    for (Cookie cookie : resp.cookies) {
        if (cookie.valid()) {
            Header h = new Header(SET_COOKIE, cookie.toString());
            response.addHeader(h);
            Logger.getLogger(HttpServlet.class).log(Level.DEBUG, h.toString());
        }
    }
}

From source file:com.orange.mmp.api.ws.jsonrpc.MMPJsonRpcServlet.java

/**
 * Gzip something.// ww w . j  av a  2 s.  c  o m
 * 
 * @param in
 *            original content
 * @return size gzipped content
 */
private byte[] gzip(byte[] in) throws IOException {
    if (in != null && in.length > 0) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(bout);
        gout.write(in);
        gout.flush();
        gout.close();
        return bout.toByteArray();
    }
    return new byte[0];
}

From source file:it.drwolf.ridire.session.async.Mapper.java

private void createArchivedResource(File f, CrawledResource cr, EntityManager entityManager) {
    // System.out.println(System.getProperty("java.io.tmpdir"));
    String posEnabled = this.em.find(Parameter.class, Parameter.POS_ENABLED.getKey()).getValue();
    File resourceDir;//from  w  w  w.  j  ava  2s  . c om
    int status = Parameter.FINISHED;
    try {
        resourceDir = new File(FilenameUtils.getFullPath(f.getCanonicalPath().replaceAll("__\\d+", ""))
                + JobMapperMonitor.RESOURCESDIR);
        if (!resourceDir.exists()) {
            FileUtils.forceMkdir(resourceDir);
        }
        ArchiveReader reader = ArchiveReaderFactory.get(f);
        ARCRecord record = (ARCRecord) reader.get(cr.getOffset());
        record.skipHttpHeader();
        byte[] buf = new byte[Mapper.BUFLENGTH];
        int count = 0;
        String resourceFile = cr.getDigest() + ".gz";
        GZIPOutputStream baos = new GZIPOutputStream(new FileOutputStream(new File(resourceDir, resourceFile)));
        while ((count = record.read(buf)) != -1) {
            baos.write(buf, 0, count);
        }
        baos.finish();
        baos.close();
        reader.close();
        // long t1 = System.currentTimeMillis();
        StringWithEncoding cleanText = this.createPlainTextResource(f, cr, entityManager);
        this.removeGZippedResource(resourceDir, resourceFile);
        // long t2 = System.currentTimeMillis();
        // System.out.println("Creazione plain text: " + (t2 - t1));
        String plainTextFileName = cr.getDigest() + ".txt";
        if (cleanText != null && cleanText.getString() != null && cleanText.getString().trim().length() > 0
                && cleanText.getCleaner() != null && (cleanText.getCleaner().equals(Mapper.ALCHEMY)
                        || cleanText.getCleaner().equals(Mapper.READABILITY))) {
            cr.setCleaner(cleanText.getCleaner());
            File plainTextFile = new File(resourceDir, plainTextFileName);
            FileUtils.writeStringToFile(plainTextFile, cleanText.getString(), cleanText.getEncoding());
            cr.setExtractedTextHash(MD5DigestCreator.getMD5Digest(plainTextFile));
            // language detection
            // t1 = System.currentTimeMillis();
            String language = this.detectLanguage(cleanText.getString());
            // t2 = System.currentTimeMillis();
            // System.out.println("Language detection: " + (t2 - t1));
            cr.setLanguage(language);
            if (language != null && language.equalsIgnoreCase(Mapper.ITALIAN) && posEnabled != null
                    && posEnabled.equalsIgnoreCase("true")) {
                // PoS tag if it's an italian text
                // t1 = System.currentTimeMillis();
                String posTagResourceFileName = this.createPoSTagResource(plainTextFile, entityManager,
                        cleanText.getEncoding());
                // t2 = System.currentTimeMillis();
                // System.out.println("PoS tagging: " + (t2 - t1));
                if (posTagResourceFileName != null) {
                    Integer wordsNumber = Mapper.countWordsFromPoSTagResource(posTagResourceFileName);
                    cr.setWordsNumber(wordsNumber);
                }
            }
        }
    } catch (Exception e) {
        status = Parameter.PROCESSING_ERROR;
        e.printStackTrace();
    }
    cr.setProcessed(status);
}

From source file:TextFileHandler.java

/**
 * Save content to a .gz file//from  ww  w  . j av a 2 s  .  co  m
 * @param fileName e.g. foo.txt.gz
 * @param content
 */
public void saveGZipFile(String fileName, String content) {
    try {
        GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(fileName));
        PrintWriter pw = new PrintWriter(out);
        pw.write(content);
        pw.flush();
        pw.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:at.medevit.elexis.emediplan.core.internal.EMediplanServiceImpl.java

/**
 * Get the encoded (Header with zipped and Base64 encoded content) String. The header of the
 * current CHMED Version is added to the resulting String.
 * //from w ww .j a v  a  2 s. co  m
 * @param json
 * @return
 */
protected String getEncodedJson(@NonNull String json) {
    StringBuilder sb = new StringBuilder();
    // header for compresses json
    sb.append("CHMED16A1");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {
        gzip.write(json.getBytes());
    } catch (IOException e) {
        LoggerFactory.getLogger(getClass()).error("Error encoding json", e);
        throw new IllegalStateException("Error encoding json", e);
    }
    sb.append(Base64.getEncoder().encodeToString(out.toByteArray()));
    return sb.toString();
}

From source file:de.uni_koblenz.west.splendid.tools.NQuadSourceAggregator.java

public OutputStream getOutputStream(String file) throws IOException {
    if (file == null)
        return System.out;

    // TODO: check if file already exists and should be overwritten

    OutputStream out = new FileOutputStream(file);
    if (file.endsWith(".gz")) {
        out = new GZIPOutputStream(out);
    }/*from w  w w .  ja v  a 2 s  .  c o m*/
    return out;
}

From source file:edu.umd.umiacs.clip.tools.io.AllFiles.java

public static Path write(Path path, Stream<?> lines, Charset cs, OpenOption... options) {
    if (options.length == 0) {
        options = new OpenOption[] { CREATE_NEW };
    }/*from   w  w w.  j av a  2 s .  c om*/
    Objects.requireNonNull(lines);
    CharsetEncoder encoder = cs.newEncoder();
    try {
        OutputStream out = newOutputStream(path, options);
        if (path.toString().endsWith(".gz")) {
            out = new GZIPOutputStream(out);
        } else if (path.toString().endsWith(".bz2")) {
            out = new BZip2CompressorOutputStream(out);
        }
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder), BUFFER_SIZE)) {
            lines.forEach(line -> {
                try {
                    writer.append(line.toString());
                    writer.newLine();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return path;
}

From source file:de.julielab.jcore.ae.lingpipegazetteer.chunking.ChunkerProviderImpl.java

private void serializeDictionary(File serializedDictionaryFile) throws FileNotFoundException, IOException {
    LOGGER.info("Storing dictionary to: {}", serializedDictionaryFile.getAbsolutePath());
    LOGGER.info(//from   w w  w . j  a  v  a 2  s .c  om
            "Warning: Loading a serialized dictionary seems to take longer than just reading the original text entries");
    ObjectOutputStream oos = new ObjectOutputStream(
            new GZIPOutputStream(new FileOutputStream(serializedDictionaryFile)));
    dict.compileTo(oos);
    oos.close();
    LOGGER.info("{} bytes written.", serializedDictionaryFile.length());
}