Search in sources :

Example 61 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project android-ocr by rmtheis.

the class OcrInitAsyncTask method gunzip.

/**
   * Unzips the given Gzipped file to the given destination, and deletes the
   * gzipped file.
   * 
   * @param zippedFile
   *          The gzipped file to be uncompressed
   * @param outFilePath
   *          File to unzip to, including path
   * @throws FileNotFoundException
   * @throws IOException
   */
private void gunzip(File zippedFile, File outFilePath) throws FileNotFoundException, IOException {
    int uncompressedFileSize = getGzipSizeUncompressed(zippedFile);
    Integer percentComplete;
    int percentCompleteLast = 0;
    int unzippedBytes = 0;
    final Integer progressMin = 0;
    int progressMax = 100 - progressMin;
    publishProgress("Uncompressing data for " + languageName + "...", progressMin.toString());
    // If the file is a tar file, just show progress to 50%
    String extension = zippedFile.toString().substring(zippedFile.toString().length() - 16);
    if (extension.equals(".tar.gz.download")) {
        progressMax = 50;
    }
    GZIPInputStream gzipInputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(zippedFile)));
    OutputStream outputStream = new FileOutputStream(outFilePath);
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
    final int BUFFER = 8192;
    byte[] data = new byte[BUFFER];
    int len;
    while ((len = gzipInputStream.read(data, 0, BUFFER)) > 0) {
        bufferedOutputStream.write(data, 0, len);
        unzippedBytes += len;
        percentComplete = (int) ((unzippedBytes / (float) uncompressedFileSize) * progressMax) + progressMin;
        if (percentComplete > percentCompleteLast) {
            publishProgress("Uncompressing data for " + languageName + "...", percentComplete.toString());
            percentCompleteLast = percentComplete;
        }
    }
    gzipInputStream.close();
    bufferedOutputStream.flush();
    bufferedOutputStream.close();
    if (zippedFile.exists()) {
        zippedFile.delete();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 62 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project robovm by robovm.

the class URLConnectionTest method testClientConfiguredGzipContentEncodingAndConnectionReuse.

/**
     * Test a bug where gzip input streams weren't exhausting the input stream,
     * which corrupted the request that followed.
     * http://code.google.com/p/android/issues/detail?id=7059
     */
private void testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind transferKind) throws Exception {
    MockResponse responseOne = new MockResponse();
    responseOne.addHeader("Content-Encoding: gzip");
    transferKind.setBody(responseOne, gzip("one (gzipped)".getBytes("UTF-8")), 5);
    server.enqueue(responseOne);
    MockResponse responseTwo = new MockResponse();
    transferKind.setBody(responseTwo, "two (identity)", 5);
    server.enqueue(responseTwo);
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    connection.addRequestProperty("Accept-Encoding", "gzip");
    InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
    assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE));
    assertEquals(0, server.takeRequest().getSequenceNumber());
    connection = server.getUrl("/").openConnection();
    assertEquals("two (identity)", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) MockResponse(com.google.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 63 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project XposedInstaller by rovo89.

the class RepoLoader method downloadAndParseFiles.

private boolean downloadAndParseFiles(List<String> messages) {
    // These variables don't need to be atomic, just mutable
    final AtomicBoolean hasChanged = new AtomicBoolean(false);
    final AtomicInteger insertCounter = new AtomicInteger();
    final AtomicInteger deleteCounter = new AtomicInteger();
    for (Entry<Long, Repository> repoEntry : mRepositories.entrySet()) {
        final long repoId = repoEntry.getKey();
        final Repository repo = repoEntry.getValue();
        String url = (repo.partialUrl != null && repo.version != null) ? String.format(repo.partialUrl, repo.version) : repo.url;
        File cacheFile = getRepoCacheFile(url);
        SyncDownloadInfo info = DownloadsUtil.downloadSynchronously(url, cacheFile);
        Log.i(XposedApp.TAG, String.format("Downloaded %s with status %d (error: %s), size %d bytes", url, info.status, info.errorMessage, cacheFile.length()));
        if (info.status != SyncDownloadInfo.STATUS_SUCCESS) {
            if (info.errorMessage != null)
                messages.add(info.errorMessage);
            continue;
        }
        InputStream in = null;
        RepoDb.beginTransation();
        try {
            in = new FileInputStream(cacheFile);
            if (url.endsWith(".gz"))
                in = new GZIPInputStream(in);
            RepoParser.parse(in, new RepoParserCallback() {

                @Override
                public void onRepositoryMetadata(Repository repository) {
                    if (!repository.isPartial) {
                        RepoDb.deleteAllModules(repoId);
                        hasChanged.set(true);
                    }
                }

                @Override
                public void onNewModule(Module module) {
                    RepoDb.insertModule(repoId, module);
                    hasChanged.set(true);
                    insertCounter.incrementAndGet();
                }

                @Override
                public void onRemoveModule(String packageName) {
                    RepoDb.deleteModule(repoId, packageName);
                    hasChanged.set(true);
                    deleteCounter.decrementAndGet();
                }

                @Override
                public void onCompleted(Repository repository) {
                    if (!repository.isPartial) {
                        RepoDb.updateRepository(repoId, repository);
                        repo.name = repository.name;
                        repo.partialUrl = repository.partialUrl;
                        repo.version = repository.version;
                    } else {
                        RepoDb.updateRepositoryVersion(repoId, repository.version);
                        repo.version = repository.version;
                    }
                    Log.i(XposedApp.TAG, String.format("Updated repository %s to version %s (%d new / %d removed modules)", repo.url, repo.version, insertCounter.get(), deleteCounter.get()));
                }
            });
            RepoDb.setTransactionSuccessful();
        } catch (Throwable t) {
            Log.e(XposedApp.TAG, "Cannot load repository from " + url, t);
            messages.add(sApp.getString(R.string.repo_load_failed, url, t.getMessage()));
            DownloadsUtil.clearCache(url);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException ignored) {
                }
            cacheFile.delete();
            RepoDb.endTransation();
        }
    }
    // repositories
    return hasChanged.get();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SyncDownloadInfo(de.robv.android.xposed.installer.util.DownloadsUtil.SyncDownloadInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Repository(de.robv.android.xposed.installer.repo.Repository) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Module(de.robv.android.xposed.installer.repo.Module) File(java.io.File) RepoParserCallback(de.robv.android.xposed.installer.repo.RepoParser.RepoParserCallback)

Example 64 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project robovm by robovm.

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)

Example 65 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project robovm by robovm.

the class DeflaterOutputStreamTest method createInflaterStream.

/**
     * Creates an optionally-flushing deflater stream, writes some bytes to it,
     * and flushes it. Returns an inflater stream that reads this deflater's
     * output.
     *
     * <p>These bytes are written on a separate thread so that when the inflater
     * stream is read, that read will fail when no bytes are available. Failing
     * takes 3 seconds, co-ordinated by PipedInputStream's 'broken pipe'
     * timeout. The 3 second delay is unfortunate but seems to be the easiest
     * way demonstrate that data is unavailable. Ie. other techniques will cause
     * the dry read to block indefinitely.
     */
static InputStream createInflaterStream(final Class<?> c, final boolean flushing) throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    final PipedOutputStream pout = new PipedOutputStream();
    PipedInputStream pin = new PipedInputStream(pout);
    executor.submit(new Callable<Void>() {

        public Void call() throws Exception {
            OutputStream out;
            if (c == DeflaterOutputStream.class) {
                out = new DeflaterOutputStream(pout, flushing);
            } else if (c == GZIPOutputStream.class) {
                out = new GZIPOutputStream(pout, flushing);
            } else {
                throw new AssertionError();
            }
            out.write(1);
            out.write(2);
            out.write(3);
            out.flush();
            return null;
        }
    }).get();
    executor.shutdown();
    if (c == DeflaterOutputStream.class) {
        return new InflaterInputStream(pin);
    } else if (c == GZIPOutputStream.class) {
        return new GZIPInputStream(pin);
    } else {
        throw new AssertionError();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) InflaterInputStream(java.util.zip.InflaterInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedOutputStream(java.io.PipedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ExecutorService(java.util.concurrent.ExecutorService) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Callable(java.util.concurrent.Callable)

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