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();
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations