Search in sources :

Example 96 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project k-9 by k9mail.

the class ImapConnection method enableCompression.

private void enableCompression() throws IOException, MessagingException {
    try {
        executeSimpleCommand(Commands.COMPRESS_DEFLATE);
    } catch (NegativeImapResponseException e) {
        Log.d(LOG_TAG, "Unable to negotiate compression: " + e.getMessage());
        return;
    }
    try {
        InflaterInputStream input = new InflaterInputStream(socket.getInputStream(), new Inflater(true));
        ZOutputStream output = new ZOutputStream(socket.getOutputStream(), JZlib.Z_BEST_SPEED, true);
        output.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
        setUpStreamsAndParser(input, output);
        if (K9MailLib.isDebug()) {
            Log.i(LOG_TAG, "Compression enabled for " + getLogId());
        }
    } catch (IOException e) {
        close();
        Log.e(LOG_TAG, "Error enabling compression", e);
    }
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) ZOutputStream(com.jcraft.jzlib.ZOutputStream)

Example 97 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project bazel by bazelbuild.

the class ProfileInfo method loadProfile.

/**
   * Loads and parses Blaze profile file.
   *
   * @param profileFile profile file path
   *
   * @return ProfileInfo object with some fields populated (call calculateStats()
   *         and analyzeRelationships() to populate the remaining fields)
   * @throws UnsupportedEncodingException if the file format is invalid
   * @throws IOException if the file can't be read
   */
public static ProfileInfo loadProfile(Path profileFile) throws IOException {
    // It is extremely important to wrap InflaterInputStream using
    // BufferedInputStream because majority of reads would be done using
    // readInt()/readLong() methods and InflaterInputStream is very inefficient
    // in handling small read requests (performance difference with 1MB buffer
    // used below is almost 10x).
    DataInputStream in = new DataInputStream(new BufferedInputStream(new InflaterInputStream(profileFile.getInputStream(), new Inflater(false), 65536), 1024 * 1024));
    if (in.readInt() != Profiler.MAGIC) {
        in.close();
        throw new UnsupportedEncodingException("Invalid profile datafile format");
    }
    if (in.readInt() != Profiler.VERSION) {
        in.close();
        throw new UnsupportedEncodingException("Incompatible profile datafile version");
    }
    String fileComment = in.readUTF();
    // Read list of used record types
    int typeCount = in.readInt();
    boolean hasUnknownTypes = false;
    Set<String> supportedTasks = new HashSet<>();
    for (ProfilerTask task : ProfilerTask.values()) {
        supportedTasks.add(task.toString());
    }
    List<ProfilerTask> typeList = new ArrayList<>();
    for (int i = 0; i < typeCount; i++) {
        String name = in.readUTF();
        if (supportedTasks.contains(name)) {
            typeList.add(ProfilerTask.valueOf(name));
        } else {
            hasUnknownTypes = true;
            typeList.add(ProfilerTask.UNKNOWN);
        }
    }
    ProfileInfo info = new ProfileInfo(fileComment);
    // TODO(bazel-team): Maybe this still should handle corrupted(truncated) files.
    try {
        int size;
        while ((size = in.readInt()) != Profiler.EOF_MARKER) {
            byte[] backingArray = new byte[size];
            in.readFully(backingArray);
            ByteBuffer buffer = ByteBuffer.wrap(backingArray);
            long threadId = VarInt.getVarLong(buffer);
            int id = VarInt.getVarInt(buffer);
            int parentId = VarInt.getVarInt(buffer);
            long startTime = VarInt.getVarLong(buffer);
            long duration = VarInt.getVarLong(buffer);
            int descIndex = VarInt.getVarInt(buffer) - 1;
            if (descIndex == -1) {
                String desc = in.readUTF();
                descIndex = info.descriptionList.size();
                info.descriptionList.add(desc);
            }
            ProfilerTask type = typeList.get(buffer.get());
            byte[] stats = null;
            if (buffer.hasRemaining()) {
                // Copy aggregated stats.
                int offset = buffer.position();
                stats = Arrays.copyOfRange(backingArray, offset, size);
                if (hasUnknownTypes) {
                    while (buffer.hasRemaining()) {
                        byte attrType = buffer.get();
                        if (typeList.get(attrType) == ProfilerTask.UNKNOWN) {
                            // We're dealing with unknown aggregated type - update stats array to
                            // use ProfilerTask.UNKNOWN.ordinal() value.
                            stats[buffer.position() - 1 - offset] = (byte) ProfilerTask.UNKNOWN.ordinal();
                        }
                        VarInt.getVarInt(buffer);
                        VarInt.getVarLong(buffer);
                    }
                }
            }
            ProfileInfo.Task task = info.new Task(threadId, id, parentId, startTime, duration, type, descIndex, new CompactStatistics(stats));
            info.addTask(task);
        }
    } catch (IOException e) {
        info.corruptedOrIncomplete = true;
    } finally {
        in.close();
    }
    return info;
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer) BufferedInputStream(java.io.BufferedInputStream) Inflater(java.util.zip.Inflater) HashSet(java.util.HashSet)

Example 98 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project jetty.project by eclipse.

the class GzipTester method getResponseMetadata.

public ContentMetadata getResponseMetadata(Response response) throws Exception {
    long size = response.getContentBytes().length;
    String contentEncoding = response.get("Content-Encoding");
    ByteArrayInputStream bais = null;
    InputStream in = null;
    DigestOutputStream digester = null;
    ByteArrayOutputStream uncompressedStream = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (contentEncoding == null) {
            LOG.debug("No response content-encoding");
            in = new PassThruInputStream(bais);
        } else if (contentEncoding.contains(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (contentEncoding.contains(GzipHandler.DEFLATE)) {
            in = new InflaterInputStream(bais, new Inflater(true));
        } else {
            assertThat("Unexpected response content-encoding", contentEncoding, isEmptyOrNullString());
        }
        uncompressedStream = new ByteArrayOutputStream((int) size);
        digester = new DigestOutputStream(uncompressedStream, digest);
        IO.copy(in, digester);
        byte[] output = uncompressedStream.toByteArray();
        String actualSha1Sum = Hex.asHex(digest.digest());
        return new ContentMetadata(output.length, actualSha1Sum);
    } finally {
        IO.close(digester);
        IO.close(in);
        IO.close(bais);
        IO.close(uncompressedStream);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) Inflater(java.util.zip.Inflater) MessageDigest(java.security.MessageDigest)

Example 99 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project jetty.project by eclipse.

the class GzipTester method assertIsResponseGzipCompressed.

public HttpTester.Response assertIsResponseGzipCompressed(String method, String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod(method);
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", compressionType);
    if (ifmodifiedsince > 0)
        request.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(), DateGenerator.formatDate(ifmodifiedsince));
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + requestedFilename);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    // Assert the response headers
    // Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK));
    // Response headers should have either a Transfer-Encoding indicating chunked OR a Content-Length
    /*
         * TODO need to check for the 3rd option of EOF content. To do this properly you might need to look at both HTTP/1.1 and HTTP/1.0 requests String
         * contentLength = response.get("Content-Length"); String transferEncoding = response.get("Transfer-Encoding");
         * 
         * boolean chunked = (transferEncoding != null) && (transferEncoding.indexOf("chunk") >= 0); if(!chunked) {
         * Assert.assertThat("Response.header[Content-Length]",contentLength,notNullValue()); } else {
         * Assert.assertThat("Response.header[Transfer-Encoding]",transferEncoding,notNullValue()); }
         */
    int qindex = compressionType.indexOf(";");
    if (qindex < 0)
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
    else
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
    Assert.assertThat(response.get("ETag"), Matchers.startsWith("W/"));
    // Assert that the decompressed contents are what we expect.
    File serverFile = testdir.getPathFile(serverFilename).toFile();
    String expected = IO.readToString(serverFile);
    String actual = null;
    ByteArrayInputStream bais = null;
    InputStream in = null;
    ByteArrayOutputStream out = null;
    try {
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (compressionType.startsWith(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (compressionType.startsWith(GzipHandler.DEFLATE)) {
            in = new InflaterInputStream(bais, new Inflater(true));
        }
        out = new ByteArrayOutputStream();
        IO.copy(in, out);
        actual = out.toString(encoding);
        assertThat("Uncompressed contents", actual, equalTo(expected));
    } finally {
        IO.close(out);
        IO.close(in);
        IO.close(bais);
    }
    return response;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Example 100 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project jetty.project by eclipse.

the class GzipTester method assertNonStaticContentIsResponseGzipCompressed.

public HttpTester.Response assertNonStaticContentIsResponseGzipCompressed(String method, String path, String expected) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod(method);
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", accept);
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + path);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    int qindex = compressionType.indexOf(";");
    if (qindex < 0)
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
    else
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
    ByteArrayInputStream bais = null;
    InputStream in = null;
    ByteArrayOutputStream out = null;
    String actual = null;
    try {
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (compressionType.startsWith(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (compressionType.startsWith(GzipHandler.DEFLATE)) {
            in = new InflaterInputStream(bais, new Inflater(true));
        }
        out = new ByteArrayOutputStream();
        IO.copy(in, out);
        actual = out.toString(encoding);
        assertThat("Uncompressed contents", actual, equalTo(expected));
    } finally {
        IO.close(out);
        IO.close(in);
        IO.close(bais);
    }
    return response;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)200 ByteArrayInputStream (java.io.ByteArrayInputStream)113 InputStream (java.io.InputStream)100 IOException (java.io.IOException)74 Inflater (java.util.zip.Inflater)66 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 GZIPInputStream (java.util.zip.GZIPInputStream)39 DataInputStream (java.io.DataInputStream)33 FileInputStream (java.io.FileInputStream)27 BufferedInputStream (java.io.BufferedInputStream)24 InputStreamReader (java.io.InputStreamReader)13 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)13 HttpURLConnection (java.net.HttpURLConnection)12 URL (java.net.URL)11 File (java.io.File)10 BufferedReader (java.io.BufferedReader)9 OutputStream (java.io.OutputStream)9 Point (java.awt.Point)7 EOFException (java.io.EOFException)7 URLConnection (java.net.URLConnection)7