use of java.util.zip.Inflater in project netty by netty.
the class JdkZlibDecoder method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (finished) {
// Skip data received after finished.
in.skipBytes(in.readableBytes());
return;
}
int readableBytes = in.readableBytes();
if (readableBytes == 0) {
return;
}
if (decideZlibOrNone) {
// First two bytes are needed to decide if it's a ZLIB stream.
if (readableBytes < 2) {
return;
}
boolean nowrap = !looksLikeZlib(in.getShort(in.readerIndex()));
inflater = new Inflater(nowrap);
decideZlibOrNone = false;
}
if (crc != null) {
switch(gzipState) {
case FOOTER_START:
if (readGZIPFooter(in)) {
finished = true;
}
return;
default:
if (gzipState != GzipState.HEADER_END) {
if (!readGZIPHeader(in)) {
return;
}
}
}
// Some bytes may have been consumed, and so we must re-set the number of readable bytes.
readableBytes = in.readableBytes();
}
if (in.hasArray()) {
inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes);
} else {
byte[] array = new byte[readableBytes];
in.getBytes(in.readerIndex(), array);
inflater.setInput(array);
}
int maxOutputLength = inflater.getRemaining() << 1;
ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength);
try {
boolean readFooter = false;
byte[] outArray = decompressed.array();
while (!inflater.needsInput()) {
int writerIndex = decompressed.writerIndex();
int outIndex = decompressed.arrayOffset() + writerIndex;
int length = decompressed.writableBytes();
if (length == 0) {
// completely filled the buffer allocate a new one and start to fill it
out.add(decompressed);
decompressed = ctx.alloc().heapBuffer(maxOutputLength);
outArray = decompressed.array();
continue;
}
int outputLength = inflater.inflate(outArray, outIndex, length);
if (outputLength > 0) {
decompressed.writerIndex(writerIndex + outputLength);
if (crc != null) {
crc.update(outArray, outIndex, outputLength);
}
} else {
if (inflater.needsDictionary()) {
if (dictionary == null) {
throw new DecompressionException("decompression failure, unable to set dictionary as non was specified");
}
inflater.setDictionary(dictionary);
}
}
if (inflater.finished()) {
if (crc == null) {
// Do not decode anymore.
finished = true;
} else {
readFooter = true;
}
break;
}
}
in.skipBytes(readableBytes - inflater.getRemaining());
if (readFooter) {
gzipState = GzipState.FOOTER_START;
if (readGZIPFooter(in)) {
finished = true;
}
}
} catch (DataFormatException e) {
throw new DecompressionException("decompression failure", e);
} finally {
if (decompressed.isReadable()) {
out.add(decompressed);
} else {
decompressed.release();
}
}
}
use of java.util.zip.Inflater in project android_frameworks_base by ResurrectionRemix.
the class StrictJarFile method getZipInputStream.
private InputStream getZipInputStream(ZipEntry ze) {
if (ze.getMethod() == ZipEntry.STORED) {
return new FDStream(fd, ze.getDataOffset(), ze.getDataOffset() + ze.getSize());
} else {
final FDStream wrapped = new FDStream(fd, ze.getDataOffset(), ze.getDataOffset() + ze.getCompressedSize());
int bufSize = Math.max(1024, (int) Math.min(ze.getSize(), 65535L));
return new ZipInflaterInputStream(wrapped, new Inflater(true), bufSize, ze);
}
}
use of java.util.zip.Inflater in project voltdb by VoltDB.
the class AbstractHistogram method decodeFromCompressedByteBuffer.
static <T extends AbstractHistogram> T decodeFromCompressedByteBuffer(final ByteBuffer buffer, final Class<T> histogramClass, final long minBarForHighestTrackableValue) throws DataFormatException {
int initialTargetPosition = buffer.position();
final int cookie = buffer.getInt();
final int headerSize;
if ((getCookieBase(cookie) == compressedEncodingCookieBase) || (getCookieBase(cookie) == V1CompressedEncodingCookieBase)) {
headerSize = ENCODING_HEADER_SIZE;
} else if (getCookieBase(cookie) == V0CompressedEncodingCookieBase) {
headerSize = V0_ENCODING_HEADER_SIZE;
} else {
throw new IllegalArgumentException("The buffer does not contain a compressed Histogram");
}
final int lengthOfCompressedContents = buffer.getInt();
final Inflater decompressor = new Inflater();
if (buffer.hasArray()) {
decompressor.setInput(buffer.array(), initialTargetPosition + 8, lengthOfCompressedContents);
} else {
byte[] compressedContents = new byte[lengthOfCompressedContents];
buffer.get(compressedContents);
decompressor.setInput(compressedContents);
}
final ByteBuffer headerBuffer = ByteBuffer.allocate(headerSize).order(BIG_ENDIAN);
decompressor.inflate(headerBuffer.array());
T histogram = decodeFromByteBuffer(headerBuffer, histogramClass, minBarForHighestTrackableValue, decompressor);
return histogram;
}
use of java.util.zip.Inflater in project voltdb by VoltDB.
the class FileArchiver method unarchive.
public static void unarchive(String infilename, String outfilename, FileAccess storage, int compressionType) throws IOException {
InputStream f = null;
OutputStream outstream = null;
boolean completed = false;
try {
if (!storage.isStreamElement(infilename)) {
return;
}
storage.removeElement(outfilename);
byte[] b = new byte[COPY_BLOCK_SIZE];
f = storage.openInputStreamElement(infilename);
switch(compressionType) {
case COMPRESSION_ZIP:
f = new InflaterInputStream(f, new Inflater());
break;
case COMPRESSION_GZIP:
f = new GZIPInputStream(f, b.length);
break;
case COMPRESSION_NONE:
break;
default:
throw new RuntimeException("FileArchiver: " + compressionType);
}
outstream = storage.openOutputStreamElement(outfilename);
while (true) {
int l = f.read(b, 0, b.length);
if (l == -1) {
break;
}
outstream.write(b, 0, l);
}
completed = true;
} catch (Throwable e) {
throw FileUtil.toIOException(e);
} finally {
try {
if (f != null) {
f.close();
}
if (outstream != null) {
outstream.flush();
outstream.close();
}
if (!completed && storage.isStreamElement(outfilename)) {
storage.removeElement(outfilename);
}
} catch (Throwable e) {
throw FileUtil.toIOException(e);
}
}
}
use of java.util.zip.Inflater in project voltdb by VoltDB.
the class ScriptReaderZipped method openFile.
protected void openFile() throws IOException {
InputStream d = db.isFilesInJar() ? getClass().getResourceAsStream(fileName) : db.getFileAccess().openInputStreamElement(fileName);
dataStreamIn = new DataInputStream(new BufferedInputStream(new InflaterInputStream(d, new Inflater()), 1 << 13));
}
Aggregations