@Override public InputStream decode(String contentEncoding, InputStream encodedStream) throws IOException { // correct impl. should wrap deflate in zlib, but some don't do it - have to identify, which one we got InputStream markSupportingStream = encodedStream.markSupported() ? encodedStream : new BufferedInputStream(encodedStream); markSupportingStream.mark(1); // read the first byte int firstByte = markSupportingStream.read(); markSupportingStream.reset(); // if using zlib, first 3 bits should be 0, 4th should be 1 // that should never be the case if no zlib wrapper if ((firstByte & 15) == 8) { // ok, zlib wrapped stream return new InflaterInputStream(markSupportingStream); } else { // no zlib wrapper return new InflaterInputStream(markSupportingStream, new Inflater(true)); } }
@Override public void expand(byte[] in, int inPos, int inLen, byte[] out, int outPos, int outLen) { Inflater decompresser = new Inflater(); decompresser.setInput(in, inPos, inLen); decompresser.finished(); try { int len = decompresser.inflate(out, outPos, outLen); if (len != outLen) { throw new DataFormatException(len + " " + outLen); } } catch (DataFormatException e) { throw DbException.get(ErrorCode.COMPRESSION_ERROR, e); } decompresser.end(); }
try { if(type.equalsIgnoreCase(HttpHeaders.Values.GZIP)) { reader = new GZIPInputStream(new ByteArrayInputStream(fullMessage)); }else if(type.equalsIgnoreCase(HttpHeaders.Values.DEFLATE)) { reader = new InflaterInputStream(new ByteArrayInputStream(fullMessage), new Inflater(true)); byte[] decompressBuffer = new byte[DECOMPRESS_BUFFER_SIZE]; int bytesRead; while ((bytesRead = reader.read(decompressBuffer)) > -1) { uncompressed.write(decompressBuffer, 0, bytesRead); try { if (reader != null) { reader.close();
private byte[] decompress(byte[] data) throws SessionValueEncoderException { if (!doCompress()) { return data; } ByteArrayInputStream bais = new ByteArrayInputStream(data); Inflater inf = new Inflater(false); InflaterInputStream iis = new InflaterInputStream(bais, inf); try { return StreamUtil.readBytes(iis, true).toByteArray(); } catch (Exception e) { throw new SessionValueEncoderException(e); } finally { inf.end(); } }
private DataInputStream getZlibInputStream(byte[] data) { return new DataInputStream(new BufferedInputStream(new InflaterInputStream( new ByteArrayInputStream(data), new Inflater(), 2048))); }
if (inf.needsInput()) { in.mark(buf.length); inf.setInput(buf, 0, bufUsed); ret = inf.inflate(b, off, len); } catch (final DataFormatException e) { throw new IOException("Gzip-compressed data is corrupt"); count(ret); if (inf.finished()) { in.reset(); final int skipAmount = bufUsed - inf.getRemaining(); if (IOUtils.skip(in, skipAmount) != skipAmount) { throw new IOException(); if (isize != (inf.getBytesWritten() & 0xffffffffL)) { throw new IOException("Gzip-compressed data is corrupt" + "(uncompressed size mismatch)"); inf.end(); inf = null; endReached = true;
/** * 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; } }
@SuppressWarnings("resource") // caller must close the InputStream @Override InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password) throws IOException { final Inflater inflater = new Inflater(true); // Inflater with nowrap=true has this odd contract for a zero padding // byte following the data stream; this used to be zlib's requirement // and has been fixed a long time ago, but the contract persists so // we comply. // https://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html#Inflater(boolean) final InflaterInputStream inflaterInputStream = new InflaterInputStream(new SequenceInputStream(in, new ByteArrayInputStream(ONE_ZERO_BYTE)), inflater); return new DeflateDecoderInputStream(inflaterInputStream, inflater); } @Override
super(is, new Inflater(true), size); byte[] header = new byte[10]; readFully(header, 0, header.length); short magic = Memory.peekShort(header, 0, ByteOrder.LITTLE_ENDIAN); if (magic != (short) GZIP_MAGIC) { boolean hcrc = (flags & FHCRC) != 0; if (hcrc) { crc.update(header, 0, header.length); readFully(header, 0, 2); if (hcrc) { crc.update(header, 0, 2); int result = in.read(buf, 0, max); if (result == -1) { throw new EOFException(); crc.update(buf, 0, result); readZeroTerminated(hcrc);
protected void bufferAndDecompress() throws IOException { if (allDataRead) { eos = true; return; } readChunkHeader(); fillBuffer(compressedBuffer); inflater.setInput(compressedBuffer); try { inflater.inflate(buffer); } catch (final DataFormatException e) { throw new IOException(e); } inflater.reset(); bufferIndex = 0; final int moreDataByte = in.read(); if (moreDataByte < 1) { allDataRead = true; } else if (moreDataByte > 1) { throw new IOException("Expected indicator of whether or not more data was to come (-1, 0, or 1) but got " + moreDataByte); } }
Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); if (inf.finished() || currentEntry == null) { return -1; if ((len = in.read(buf)) == -1) { eof = true; return -1; lastRead += toRead; inRead += toRead; crc.update(buffer, byteOffset, toRead); return toRead; if (inf.needsInput()) { fill(); if (len > 0) { read = inf.inflate(buffer, byteOffset, byteCount); } catch (DataFormatException e) { throw new ZipException(e.getMessage()); if (read == 0 && inf.finished()) { return -1; crc.update(buffer, byteOffset, read); return read;
/** * Fills the input buffer with data to be decompressed. * * @throws IOException * if an {@code IOException} occurs. */ protected void fill() throws IOException { checkClosed(); if (nativeEndBufSize > 0) { ZipFile.RAFStream is = (ZipFile.RAFStream) in; len = is.fill(inf, nativeEndBufSize); } else { if ((len = in.read(buf)) > 0) { inf.setInput(buf, 0, len); } } }
protected InputStream detectStreamEncode(String encoding, InputStream ins) throws IOException { if (encoding != null && encoding.contains("gzip")) { return new GZIPInputStream(ins); } else if (encoding != null && encoding.contains("deflate")) { return new InflaterInputStream(ins, new Inflater(true)); } else { return ins; } }
public static InputStream getInputStream(URLConnection c) throws IOException { InputStream is = c.getInputStream(); String enc = c.getContentEncoding(); if ("gzip".equalsIgnoreCase(enc) || "x-gzip".equalsIgnoreCase(enc)) { is = new GZIPInputStream(is, TRANSFER_BUFFER); } else if ("deflate".equalsIgnoreCase(enc)) { is = new InflaterInputStream(is, new Inflater(), TRANSFER_BUFFER); } return new BufferedInputStream(is); }
public int readZlib(byte[] pixels,int offset,int length) throws java.io.IOException { if (zStream == null){ in = new BufferedInputStream( new FileInputStream( file.getFD() ) ); zStream = new InflaterInputStream( in, new Inflater(), length ); } return zStream.read(pixels, offset, length); } /**