Example usage for java.util.zip GZIPInputStream GZIPInputStream

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

Introduction

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

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:org.wisdom.test.http.HttpResponse.java

@SuppressWarnings("unchecked")
private void parseResponseBody(Class<T> responseClass, HttpEntity responseEntity) {
    String charset = "UTF-8";
    if (responseEntity != null) {
        try {/*from w  w  w .  jav  a2  s. c o m*/
            byte[] raw;
            InputStream responseInputStream = responseEntity.getContent();
            if (isGzipped()) {
                responseInputStream = new GZIPInputStream(responseEntity.getContent());
            }
            raw = getBytes(responseInputStream);
            this.rawBody = new ByteArrayInputStream(raw);
            this.consumedSize = raw.length;

            if (responseEntity.getContentType() != null) {
                String responseCharset = getCharsetFromContentType(responseEntity.getContentType().getValue());
                if (responseCharset != null && !responseCharset.trim().equals("")) {
                    charset = responseCharset;
                }
            }

            if (JsonNode.class.equals(responseClass)) {
                String jsonString = new String(raw, charset).trim();
                this.body = (T) new ObjectMapper().readValue(jsonString, JsonNode.class);
            } else if (Document.class.equals(responseClass)) {
                String r = new String(raw, charset).trim();
                this.body = (T) Jsoup.parse(r);
            } else if (String.class.equals(responseClass)) {
                this.body = (T) new String(raw, charset);
            } else if (InputStream.class.equals(responseClass)) {
                this.body = (T) this.rawBody;
            } else {
                throw new IllegalArgumentException("Unknown result type. Only String, JsonNode, "
                        + "Document and InputStream are supported.");
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}

From source file:com.zimbra.cs.lmtpserver.utils.LmtpInject.java

public static void main(String[] args) {
    CliUtil.toolSetup();/* w w w  .  j  a  va2s.  c o  m*/
    CommandLine cl = parseArgs(args);

    if (cl.hasOption("h")) {
        usage(null);
    }
    boolean quietMode = cl.hasOption("q");
    int threads = 1;
    if (cl.hasOption("t")) {
        threads = Integer.valueOf(cl.getOptionValue("t")).intValue();
    }

    String host = null;
    if (cl.hasOption("a")) {
        host = cl.getOptionValue("a");
    } else {
        host = "localhost";
    }

    int port;
    Protocol proto = null;
    if (cl.hasOption("smtp")) {
        proto = Protocol.SMTP;
        port = 25;
    } else
        port = 7025;
    if (cl.hasOption("p"))
        port = Integer.valueOf(cl.getOptionValue("p")).intValue();

    String[] recipients = cl.getOptionValues("r");
    String sender = cl.getOptionValue("s");
    boolean tracingEnabled = cl.hasOption("T");

    int everyN;
    if (cl.hasOption("N")) {
        everyN = Integer.valueOf(cl.getOptionValue("N")).intValue();
    } else {
        everyN = 100;
    }

    // Process files from the -d option.
    List<File> files = new ArrayList<File>();
    if (cl.hasOption("d")) {
        File dir = new File(cl.getOptionValue("d"));
        if (!dir.isDirectory()) {
            System.err.format("%s is not a directory.\n", dir.getPath());
            System.exit(1);
        }
        File[] fileArray = dir.listFiles();
        if (fileArray == null || fileArray.length == 0) {
            System.err.format("No files found in directory %s.\n", dir.getPath());
        }
        Collections.addAll(files, fileArray);
    }

    // Process files specified as arguments.
    for (String arg : cl.getArgs()) {
        files.add(new File(arg));
    }

    // Validate file content.
    if (!cl.hasOption("noValidation")) {
        Iterator<File> i = files.iterator();
        while (i.hasNext()) {
            InputStream in = null;
            File file = i.next();
            boolean valid = false;
            try {
                in = new FileInputStream(file);
                if (FileUtil.isGzipped(file)) {
                    in = new GZIPInputStream(in);
                }
                in = new BufferedInputStream(in); // Required for RFC 822 check
                if (!EmailUtil.isRfc822Message(in)) {
                    System.err.format("%s does not contain a valid RFC 822 message.\n", file.getPath());
                } else {
                    valid = true;
                }
            } catch (IOException e) {
                System.err.format("Unable to validate %s: %s.\n", file.getPath(), e.toString());
            } finally {
                ByteUtil.closeStream(in);
            }
            if (!valid) {
                i.remove();
            }
        }
    }

    if (files.size() == 0) {
        System.err.println("No files to inject.");
        System.exit(1);
    }

    if (!quietMode) {
        System.out.format(
                "Injecting %d message(s) to %d recipient(s).  Server %s, port %d, using %d thread(s).\n",
                files.size(), recipients.length, host, port, threads);
    }

    int totalFailed = 0;
    int totalSucceeded = 0;
    long startTime = System.currentTimeMillis();
    boolean verbose = cl.hasOption("v");
    boolean skipTLSCertValidation = cl.hasOption("skipTLSCertValidation");

    LmtpInject injector = null;
    try {
        injector = new LmtpInject(threads, sender, recipients, files, host, port, proto, quietMode,
                tracingEnabled, verbose, skipTLSCertValidation);
    } catch (Exception e) {
        mLog.error("Unable to initialize LmtpInject", e);
        System.exit(1);
    }

    injector.setReportEvery(everyN);
    injector.markStartTime();
    try {
        injector.run();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    int succeeded = injector.getSuccessCount();
    int failedThisTime = injector.getFailureCount();
    long elapsedMS = System.currentTimeMillis() - startTime;
    double elapsed = elapsedMS / 1000.0;
    double msPerMsg = 0.0;
    double msgSizeKB = 0.0;
    if (succeeded > 0) {
        msPerMsg = elapsedMS;
        msPerMsg /= succeeded;
        msgSizeKB = injector.mFileSizeTotal / 1024.0;
        msgSizeKB /= succeeded;
    }
    double msgPerSec = ((double) succeeded / (double) elapsedMS) * 1000;
    if (!quietMode) {
        System.out.println();
        System.out.printf(
                "LmtpInject Finished\n" + "submitted=%d failed=%d\n" + "%.2fs, %.2fms/msg, %.2fmsg/s\n"
                        + "average message size = %.2fKB\n",
                succeeded, failedThisTime, elapsed, msPerMsg, msgPerSec, msgSizeKB);
    }

    totalFailed += failedThisTime;
    totalSucceeded += succeeded;

    if (totalFailed != 0)
        System.exit(1);
}

From source file:net.myrrix.web.servlets.IngestServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    MyrrixRecommender recommender = getRecommender();

    boolean fromBrowserUpload = request.getContentType().startsWith("multipart/form-data");

    Reader reader;// w w  w  .  j  a  v a 2s  .  co m
    if (fromBrowserUpload) {

        Collection<Part> parts = request.getParts();
        if (parts == null || parts.isEmpty()) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No form data");
            return;
        }
        Part part = parts.iterator().next();
        String partContentType = part.getContentType();
        InputStream in = part.getInputStream();
        if ("application/zip".equals(partContentType)) {
            in = new ZipInputStream(in);
        } else if ("application/gzip".equals(partContentType)) {
            in = new GZIPInputStream(in);
        } else if ("application/x-gzip".equals(partContentType)) {
            in = new GZIPInputStream(in);
        } else if ("application/bzip2".equals(partContentType)) {
            in = new BZip2CompressorInputStream(in);
        } else if ("application/x-bzip2".equals(partContentType)) {
            in = new BZip2CompressorInputStream(in);
        }
        reader = new InputStreamReader(in, Charsets.UTF_8);

    } else {

        String charEncodingName = request.getCharacterEncoding();
        Charset charEncoding = charEncodingName == null ? Charsets.UTF_8 : Charset.forName(charEncodingName);
        String contentEncoding = request.getHeader(HttpHeaders.CONTENT_ENCODING);
        if (contentEncoding == null) {
            reader = request.getReader();
        } else if ("gzip".equals(contentEncoding)) {
            reader = new InputStreamReader(new GZIPInputStream(request.getInputStream()), charEncoding);
        } else if ("zip".equals(contentEncoding)) {
            reader = new InputStreamReader(new ZipInputStream(request.getInputStream()), charEncoding);
        } else if ("bzip2".equals(contentEncoding)) {
            reader = new InputStreamReader(new BZip2CompressorInputStream(request.getInputStream()),
                    charEncoding);
        } else {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unsupported Content-Encoding");
            return;
        }

    }

    try {
        recommender.ingest(reader);
    } catch (IllegalArgumentException iae) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, iae.toString());
        return;
    } catch (NoSuchElementException nsee) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, nsee.toString());
        return;
    } catch (TasteException te) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, te.toString());
        getServletContext().log("Unexpected error in " + getClass().getSimpleName(), te);
        return;
    }

    String referer = request.getHeader(HttpHeaders.REFERER);
    if (fromBrowserUpload && referer != null) {
        // Parsing avoids response splitting
        response.sendRedirect(new URL(referer).toString());
    }

}

From source file:ch.ledcom.jpreseed.UsbCreatorTest.java

private CpioArchiveInputStream getCpioArchiveInputStream(FsDirectoryEntry newInitRdGzEntry) throws IOException {
    FsFile initrdGzFile = newInitRdGzEntry.getFile();
    ByteBuffer initRdBuffer = ByteBuffer.allocate((int) initrdGzFile.getLength());
    initrdGzFile.read(0, initRdBuffer);/*from  w  ww .j a v a 2 s. c  o m*/
    initRdBuffer.rewind();
    return new CpioArchiveInputStream(new GZIPInputStream(new ByteBufferDataInputStream(initRdBuffer)));
}

From source file:me.j360.trace.example.consumer2.ZipkinServerConfiguration.java

static byte[] gunzip(byte[] input) throws IOException {
    GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(input));
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length)) {
        byte[] buf = GZIP_BUFFER.get();
        int len;/*from www  .java2 s .c  om*/
        while ((len = in.read(buf)) > 0) {
            outputStream.write(buf, 0, len);
        }
        return outputStream.toByteArray();
    }
}

From source file:com.dotcms.rest.SyncPollsResource.java

private void untar(InputStream bundle, String path, String fileName) {
    TarEntry entry;/*  w ww  .  ja v  a2 s.c o  m*/
    TarInputStream inputStream = null;
    FileOutputStream outputStream = null;

    try {
        // get a stream to tar file
        InputStream gstream = new GZIPInputStream(bundle);
        inputStream = new TarInputStream(gstream);

        // For each entry in the tar, extract and save the entry to the file
        // system
        while (null != (entry = inputStream.getNextEntry())) {
            // for each entry to be extracted
            int bytesRead;

            String pathWithoutName = path;

            // if the entry is a directory, create the directory
            if (entry.isDirectory()) {
                File fileOrDir = new File(pathWithoutName + entry.getName());
                fileOrDir.mkdir();
                continue;
            }

            // write to file
            byte[] buf = new byte[1024];
            outputStream = new FileOutputStream(pathWithoutName + entry.getName());
            while ((bytesRead = inputStream.read(buf, 0, 1024)) > -1)
                outputStream.write(buf, 0, bytesRead);
            try {
                if (null != outputStream)
                    outputStream.close();
            } catch (Exception e) {
            }
        } // while

    } catch (Exception e) {
        e.printStackTrace();
    } finally { // close your streams
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:com.vk.sdkweb.api.httpClient.VKHttpOperation.java

/**
 * Start current prepared http-operation for result
 */// www  . ja v  a  2 s  .  c o  m
@Override
public void start() {
    setState(VKOperationState.Executing);
    try {
        response = VKHttpClient.getClient().execute(mUriRequest);
        InputStream inputStream = response.getEntity().getContent();
        Header contentEncoding = response.getFirstHeader("Content-Encoding");
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            inputStream = new GZIPInputStream(inputStream);
        }

        if (outputStream == null) {
            outputStream = new ByteArrayOutputStream();
        }

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1)
            outputStream.write(buffer, 0, bytesRead);
        inputStream.close();
        outputStream.flush();
        if (outputStream instanceof ByteArrayOutputStream) {
            mResponseBytes = ((ByteArrayOutputStream) outputStream).toByteArray();
        }
        outputStream.close();
    } catch (Exception e) {
        mLastException = e;
    }
    finish();
}

From source file:com.endgame.binarypig.util.BuildSequenceFileFromArchive.java

public void load(FileSystem fs, Configuration conf, File archive, Path outputDir) throws Exception {
    Text key = new Text();
    BytesWritable val = new BytesWritable();

    SequenceFile.Writer writer = null;
    ArchiveInputStream archiveInputStream = null;

    try {/*  w  ww.  j  a va 2s  .  c  o m*/
        Path sequenceName = new Path(outputDir, archive.getName() + ".seq");
        System.out.println("Writing to " + sequenceName);
        writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class, BytesWritable.class,
                CompressionType.RECORD);
        String lowerName = archive.toString().toLowerCase();

        if (lowerName.endsWith(".tar.gz") || lowerName.endsWith(".tgz")) {
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
                    new GZIPInputStream(new FileInputStream(archive)));
        } else if (lowerName.endsWith(".tar.bz") || lowerName.endsWith(".tar.bz2")
                || lowerName.endsWith(".tbz")) {
            FileInputStream is = new FileInputStream(archive);
            is.read(); // read 'B'
            is.read(); // read 'Z'
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
                    new CBZip2InputStream(is));
        } else if (lowerName.endsWith(".tar")) {
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
                    new FileInputStream(archive));
        } else if (lowerName.endsWith(".zip")) {
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("zip",
                    new FileInputStream(archive));
        } else {
            throw new RuntimeException("Can't handle archive format for: " + archive);
        }

        ArchiveEntry entry = null;
        while ((entry = archiveInputStream.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                try {
                    byte[] outputFile = IOUtils.toByteArray(archiveInputStream);
                    val.set(outputFile, 0, outputFile.length);
                    key.set(DigestUtils.md5Hex(outputFile));

                    writer.append(key, val);
                } catch (IOException e) {
                    System.err.println("Warning: archive may be truncated: " + archive);
                    // Truncated Archive
                    break;
                }
            }
        }
    } finally {
        archiveInputStream.close();
        writer.close();
    }
}

From source file:com.insightml.utils.io.IoUtils.java

public static BufferedReader gzipReader(final InputStream inputStream) throws IOException {
    return reader(new GZIPInputStream(inputStream));
}

From source file:net.landora.anidb.mylist.ListReader.java

public boolean download(InputStream input) throws Throwable {
    TarInputStream is = null;//from   w  w  w . j  av  a 2 s . c o  m
    try {
        is = new TarInputStream(new GZIPInputStream(input));

        TarEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.getName().equalsIgnoreCase("mylist.xml")) {
                continue;
            }

            XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
            reader.nextTag();
            reader.require(XMLStreamReader.START_ELEMENT, null, "my_anime_list");
            values = new HashMap<>();
            StringBuilder value = new StringBuilder();

            while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                reader.require(XMLStreamReader.START_ELEMENT, null, null);
                String tableName = reader.getLocalName();

                values.clear();

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String valueName = reader.getLocalName();

                    value.setLength(0);
                    while (reader.next() != XMLStreamReader.END_ELEMENT) {
                        switch (reader.getEventType()) {
                        case XMLStreamReader.CDATA:
                        case XMLStreamReader.CHARACTERS:
                        case XMLStreamReader.SPACE:
                            value.append(reader.getText());
                        }
                    }
                    reader.require(XMLStreamReader.END_ELEMENT, null, valueName);
                    values.put(valueName, value.toString());

                }
                reader.require(XMLStreamReader.END_ELEMENT, null, tableName);

                handleTable(tableName);

            }
            reader.require(XMLStreamReader.END_ELEMENT, null, "my_anime_list");

            saveLast();
        }
        return true;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        } else if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
}