Search in sources :

Example 26 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project qi4j-sdk by Qi4j.

the class Inputs method text.

// START SNIPPET: method
/**
     * Read lines from a textfile at a given URL.
     *
     * If the content support gzip encoding, then the data is automatically unzipped when read.
     *
     * The charset in the content-type of the URL will be used for parsing. Default is UTF-8.
     *
     * @param source textfile with lines separated by \n character
     *
     * @return Input that provides lines from the textfiles as strings
     */
public static Input<String, IOException> text(final URL source) // END SNIPPET: method
{
    return new Input<String, IOException>() {

        @Override
        public <ReceiverThrowableType extends Throwable> void transferTo(Output<? super String, ReceiverThrowableType> output) throws IOException, ReceiverThrowableType {
            URLConnection urlConnection = source.openConnection();
            urlConnection.setRequestProperty("Accept-Encoding", "gzip");
            InputStream stream = urlConnection.getInputStream();
            // If file is gzipped, unzip it automatically
            if ("gzip".equals(urlConnection.getContentEncoding())) {
                stream = new GZIPInputStream(stream);
            }
            // Figure out charset given content-type
            String contentType = urlConnection.getContentType();
            String charSet = "UTF-8";
            if (contentType.indexOf("charset=") != -1) {
                charSet = contentType.substring(contentType.indexOf("charset=") + "charset=".length());
            }
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charSet))) {
                output.receiveFrom(new Sender<String, IOException>() {

                    @Override
                    public <ReceiverThrowableType extends Throwable> void sendTo(Receiver<? super String, ReceiverThrowableType> receiver) throws ReceiverThrowableType, IOException {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            receiver.receive(line);
                        }
                    }
                });
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URLConnection(java.net.URLConnection) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedReader(java.io.BufferedReader)

Example 27 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project LogisticsPipes by RS485.

the class ServerPacketBufferHandlerThread method decompress.

private static byte[] decompress(byte[] contentBytes) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(contentBytes));
        int buffer = 0;
        while ((buffer = gzip.read()) != -1) {
            out.write(buffer);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return out.toByteArray();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 28 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project LogisticsPipes by RS485.

the class ClientPacketBufferHandlerThread method decompress.

private static byte[] decompress(byte[] contentBytes) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(contentBytes));
        int buffer = 0;
        while ((buffer = gzip.read()) != -1) {
            out.write(buffer);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return out.toByteArray();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 29 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project Notes by MiCode.

the class GTaskClient method getResponseContent.

private String getResponseContent(HttpEntity entity) throws IOException {
    String contentEncoding = null;
    if (entity.getContentEncoding() != null) {
        contentEncoding = entity.getContentEncoding().getValue();
        Log.d(TAG, "encoding: " + contentEncoding);
    }
    InputStream input = entity.getContent();
    if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
        input = new GZIPInputStream(entity.getContent());
    } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
        Inflater inflater = new Inflater(true);
        input = new InflaterInputStream(entity.getContent(), inflater);
    }
    try {
        InputStreamReader isr = new InputStreamReader(input);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        while (true) {
            String buff = br.readLine();
            if (buff == null) {
                return sb.toString();
            }
            sb = sb.append(buff);
        }
    } finally {
        input.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) BufferedReader(java.io.BufferedReader) Inflater(java.util.zip.Inflater)

Example 30 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project phonegap-facebook-plugin by Wizcorp.

the class HttpEngine method initContentStream.

private void initContentStream(InputStream transferStream) throws IOException {
    responseTransferIn = transferStream;
    if (transparentGzip && responseHeaders.isContentEncodingGzip()) {
        // If the response was transparently gzipped, remove the gzip header field
        // so clients don't double decompress. http://b/3009828
        //
        // Also remove the Content-Length in this case because it contains the
        // length 528 of the gzipped response. This isn't terribly useful and is
        // dangerous because 529 clients can query the content length, but not
        // the content encoding.
        responseHeaders.stripContentEncoding();
        responseHeaders.stripContentLength();
        responseBodyIn = new GZIPInputStream(transferStream);
    } else {
        responseBodyIn = transferStream;
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream)

Aggregations

GZIPInputStream (java.util.zip.GZIPInputStream)376 InputStream (java.io.InputStream)144 IOException (java.io.IOException)125 ByteArrayInputStream (java.io.ByteArrayInputStream)120 FileInputStream (java.io.FileInputStream)98 ByteArrayOutputStream (java.io.ByteArrayOutputStream)77 InputStreamReader (java.io.InputStreamReader)57 File (java.io.File)56 BufferedReader (java.io.BufferedReader)45 BufferedInputStream (java.io.BufferedInputStream)41 Test (org.junit.Test)41 FileOutputStream (java.io.FileOutputStream)30 URL (java.net.URL)25 InflaterInputStream (java.util.zip.InflaterInputStream)25 OutputStream (java.io.OutputStream)24 GZIPOutputStream (java.util.zip.GZIPOutputStream)21 ObjectInputStream (java.io.ObjectInputStream)19 HttpURLConnection (java.net.HttpURLConnection)19 URLConnection (java.net.URLConnection)17 HashMap (java.util.HashMap)15