Search in sources :

Example 71 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project exhibitor by soabase.

the class BackupManager method restore.

/**
     * Restore the given key to the given file
     *
     * @param backup the backup to pull down
     * @param destinationFile the file
     * @throws Exception errors
     */
public void restore(BackupMetaData backup, File destinationFile) throws Exception {
    File tempFile = File.createTempFile("exhibitor-backup", ".tmp");
    OutputStream out = new FileOutputStream(tempFile);
    InputStream in = null;
    try {
        backupProvider.get().downloadBackup(exhibitor, backup, out, getBackupConfig());
        CloseableUtils.closeQuietly(out);
        out = null;
        out = new FileOutputStream(destinationFile);
        in = new GZIPInputStream(new FileInputStream(tempFile));
        ByteStreams.copy(in, out);
    } finally {
        CloseableUtils.closeQuietly(in);
        CloseableUtils.closeQuietly(out);
        if (!tempFile.delete()) {
            exhibitor.getLog().add(ActivityLog.Type.ERROR, "Could not delete temp file (for restore): " + tempFile);
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 72 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project jstorm by alibaba.

the class Utils method unTarUsingJava.

private static void unTarUsingJava(File inFile, File targetDir, boolean gzipped) throws IOException {
    InputStream inputStream = null;
    TarArchiveInputStream tis = null;
    try {
        if (gzipped) {
            inputStream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile)));
        } else {
            inputStream = new BufferedInputStream(new FileInputStream(inFile));
        }
        tis = new TarArchiveInputStream(inputStream);
        for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; ) {
            unpackEntries(tis, entry, targetDir);
            entry = tis.getNextTarEntry();
        }
    } finally {
        cleanup(tis, inputStream);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 73 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project jstorm by alibaba.

the class Utils method fromCompressedJsonConf.

public static Map<String, Object> fromCompressedJsonConf(byte[] serialized) {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        InputStreamReader in = new InputStreamReader(new GZIPInputStream(bis));
        Object ret = JSONValue.parseWithException(in);
        in.close();
        return (Map<String, Object>) ret;
    } catch (IOException | ParseException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ComponentObject(backtype.storm.generated.ComponentObject) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 74 with GZIPInputStream

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

the class AbstractAjaxCallback method httpDo.

private void httpDo(HttpUriRequest hr, String url, AjaxStatus status) throws ClientProtocolException, IOException {
    DefaultHttpClient client = getClient();
    if (proxyHandle != null) {
        proxyHandle.applyProxy(this, hr, client);
    }
    if (AGENT != null) {
        hr.addHeader("User-Agent", AGENT);
    } else if (AGENT == null && GZIP) {
        hr.addHeader("User-Agent", "gzip");
    }
    if (headers != null) {
        for (String name : headers.keySet()) {
            hr.addHeader(name, headers.get(name));
        }
    }
    if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
        hr.addHeader("Accept-Encoding", "gzip");
    }
    if (ah != null) {
        ah.applyToken(this, hr);
    }
    String cookie = makeCookie();
    if (cookie != null) {
        hr.addHeader("Cookie", cookie);
    }
    HttpParams hp = hr.getParams();
    if (proxy != null)
        hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    if (timeout > 0) {
        hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
        hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    }
    if (!redirect) {
        hp.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    }
    HttpContext context = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    request = hr;
    if (abort) {
        throw new IOException("Aborted");
    }
    if (SIMULATE_ERROR) {
        throw new IOException("Simulated Error");
    }
    HttpResponse response = null;
    try {
        //response = client.execute(hr, context);
        response = execute(hr, client, context);
    } catch (HttpHostConnectException e) {
        //if proxy is used, automatically retry without proxy
        if (proxy != null) {
            AQUtility.debug("proxy failed, retrying without proxy");
            hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
            //response = client.execute(hr, context);
            response = execute(hr, client, context);
        } else {
            throw e;
        }
    }
    byte[] data = null;
    String redirect = url;
    int code = response.getStatusLine().getStatusCode();
    String message = response.getStatusLine().getReasonPhrase();
    String error = null;
    HttpEntity entity = response.getEntity();
    File file = null;
    File tempFile = null;
    if (code < 200 || code >= 300) {
        InputStream is = null;
        try {
            if (entity != null) {
                is = entity.getContent();
                byte[] s = toData(getEncoding(entity), is);
                error = new String(s, "UTF-8");
                AQUtility.debug("error", error);
            }
        } catch (Exception e) {
            AQUtility.debug(e);
        } finally {
            AQUtility.close(is);
        }
    } else {
        HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        redirect = currentHost.toURI() + currentReq.getURI();
        int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
        OutputStream os = null;
        InputStream is = null;
        try {
            file = getPreFile();
            if (file == null) {
                os = new PredefinedBAOS(size);
            } else {
                //file.createNewFile();
                tempFile = makeTempFile(file);
                os = new BufferedOutputStream(new FileOutputStream(tempFile));
            }
            is = entity.getContent();
            boolean gzip = "gzip".equalsIgnoreCase(getEncoding(entity));
            if (gzip) {
                is = new GZIPInputStream(is);
            }
            int contentLength = (int) entity.getContentLength();
            //AQUtility.debug("gzip response", entity.getContentEncoding());
            copy(is, os, contentLength, tempFile, file);
            if (file == null) {
                data = ((PredefinedBAOS) os).toByteArray();
            } else {
                if (!file.exists() || file.length() == 0) {
                    file = null;
                }
            }
        } finally {
            AQUtility.close(is);
            AQUtility.close(os);
        }
    }
    AQUtility.debug("response", code);
    if (data != null) {
        AQUtility.debug(data.length, url);
    }
    status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file).client(client).context(context).headers(response.getAllHeaders());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) GZIPInputStream(java.util.zip.GZIPInputStream) HttpHost(org.apache.http.HttpHost) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) PredefinedBAOS(com.androidquery.util.PredefinedBAOS) BufferedOutputStream(java.io.BufferedOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) Date(java.util.Date) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 75 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project platform_frameworks_base by android.

the class InputMethodSubtypeArray method decompress.

private static byte[] decompress(final byte[] data, final int expectedSize) {
    try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
        final GZIPInputStream unzipper = new GZIPInputStream(inputStream)) {
        final byte[] result = new byte[expectedSize];
        int totalReadBytes = 0;
        while (totalReadBytes < result.length) {
            final int restBytes = result.length - totalReadBytes;
            final int readBytes = unzipper.read(result, totalReadBytes, restBytes);
            if (readBytes < 0) {
                break;
            }
            totalReadBytes += readBytes;
        }
        if (expectedSize != totalReadBytes) {
            return null;
        }
        return result;
    } catch (Exception e) {
        Slog.e(TAG, "Failed to decompress the data.", e);
        return null;
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

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