Search in sources :

Example 16 with InflaterInputStream

use of java.util.zip.InflaterInputStream 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)

Example 17 with InflaterInputStream

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

the class BlobBackupHelper method inflate.

// Returns null if inflation failed
private byte[] inflate(byte[] compressedData) {
    byte[] result = null;
    if (compressedData != null) {
        try {
            ByteArrayInputStream source = new ByteArrayInputStream(compressedData);
            DataInputStream headerIn = new DataInputStream(source);
            int version = headerIn.readInt();
            if (version > mCurrentBlobVersion) {
                Log.w(TAG, "Saved payload from unrecognized version " + version);
                return null;
            }
            InflaterInputStream in = new InflaterInputStream(source);
            ByteArrayOutputStream inflated = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int nRead;
            while ((nRead = in.read(buffer)) > 0) {
                inflated.write(buffer, 0, nRead);
            }
            in.close();
            inflated.flush();
            result = inflated.toByteArray();
            if (DEBUG) {
                Log.v(TAG, "Inflated " + compressedData.length + " bytes to " + result.length);
            }
        } catch (IOException e) {
            // result is still null here
            Log.w(TAG, "Unable to process restored payload: " + e.getMessage());
        }
    }
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 18 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project okio by square.

the class DeflaterSinkTest method inflate.

/**
   * Uses streaming decompression to inflate {@code deflated}. The input must
   * either be finished or have a trailing sync flush.
   */
private Buffer inflate(Buffer deflated) throws IOException {
    InputStream deflatedIn = deflated.inputStream();
    Inflater inflater = new Inflater();
    InputStream inflatedIn = new InflaterInputStream(deflatedIn, inflater);
    Buffer result = new Buffer();
    byte[] buffer = new byte[8192];
    while (!inflater.needsInput() || deflated.size() > 0 || deflatedIn.available() > 0) {
        int count = inflatedIn.read(buffer, 0, buffer.length);
        if (count != -1) {
            result.write(buffer, 0, count);
        }
    }
    return result;
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 19 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project commons by twitter.

the class Base64ZlibCodec method decompress.

private static byte[] decompress(byte[] compressed) throws InvalidDataException {
    byte[] bytes;
    try {
        final InputStream bin = new ByteArrayInputStream(compressed);
        final InputStream zin;
        if (startsWith(compressed, GZIP_HEADER_PREFIX)) {
            zin = new GZIPInputStream(bin);
        } else if (startsWith(compressed, ZLIB_HEADER_PREFIX)) {
            zin = new InflaterInputStream(bin);
        } else {
            throw new Base64ZlibCodec.InvalidDataException("Value doesn't start with either GZIP or zlib header");
        }
        try {
            bytes = ByteStreams.toByteArray(zin);
        } finally {
            zin.close();
        }
    } catch (IOException e) {
        throw new Base64ZlibCodec.InvalidDataException("zlib/GZIP decoding error", e);
    }
    return bytes;
}
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) IOException(java.io.IOException)

Example 20 with InflaterInputStream

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

the class ZipCombiner method writeEntry.

/**
   * Writes an entry from the specified source {@link ZipReader} and {@link ZipFileEntry} using the
   * specified {@link EntryAction}.
   * 
   *  <p>Writes the output entry from the input entry performing inflation or deflation as needed
   *  and applies any values from the {@link EntryAction} as needed.
   */
private void writeEntry(ZipReader zip, ZipFileEntry entry, EntryAction action) throws IOException {
    checkArgument(action.getType() != ActionType.SKIP, "Cannot write a zip entry whose action is of type SKIP.");
    ZipFileEntry outEntry = new ZipFileEntry(entry);
    if (action.getType() == ActionType.RENAME) {
        checkNotNull(action.getNewName(), "ZipEntryFilter actions of type RENAME must not have a null filename.");
        outEntry.setName(action.getNewName());
    }
    if (action.getDate() != null) {
        outEntry.setTime(action.getDate().getTime());
    }
    InputStream data;
    if (mode == OutputMode.FORCE_DEFLATE && entry.getMethod() != Compression.DEFLATED) {
        // The output mode is deflate, but the entry compression is not. Create a deflater stream
        // from the raw file data and deflate to a temporary byte array to determine the deflated
        // size. Then use this byte array as the input stream for writing the entry.
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        copyStream(new DeflaterInputStream(zip.getRawInputStream(entry), getDeflater()), tmp);
        data = new ByteArrayInputStream(tmp.toByteArray());
        outEntry.setMethod(Compression.DEFLATED);
        outEntry.setCompressedSize(tmp.size());
    } else if (mode == OutputMode.FORCE_STORED && entry.getMethod() != Compression.STORED) {
        // The output mode is stored, but the entry compression is not; create an inflater stream
        // from the raw file data. 
        data = new InflaterInputStream(zip.getRawInputStream(entry), getInflater());
        outEntry.setMethod(Compression.STORED);
        outEntry.setCompressedSize(entry.getSize());
    } else {
        // Entry compression agrees with output mode; use the raw file data as is.
        data = zip.getRawInputStream(entry);
    }
    writeEntry(outEntry, data);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)91 ByteArrayInputStream (java.io.ByteArrayInputStream)49 InputStream (java.io.InputStream)46 IOException (java.io.IOException)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 Inflater (java.util.zip.Inflater)33 GZIPInputStream (java.util.zip.GZIPInputStream)26 DataInputStream (java.io.DataInputStream)16 BufferedInputStream (java.io.BufferedInputStream)11 HttpURLConnection (java.net.HttpURLConnection)9 OutputStream (java.io.OutputStream)8 URL (java.net.URL)8 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)8 BufferedReader (java.io.BufferedReader)6 InputStreamReader (java.io.InputStreamReader)6 URLConnection (java.net.URLConnection)6 EOFException (java.io.EOFException)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 SocketException (java.net.SocketException)4