/** * Reads uncompressed data into an array of bytes. If <code>len</code> is not zero, the method * will block until some input can be decompressed; otherwise, no bytes are read and <code>0 * </code> is returned. * * @param b the buffer into which the data is read * @param off the start offset in the destination array <code>b</code> * @param len the maximum number of bytes read * @return the actual number of bytes read, or -1 if the end of the compressed input is reached or * a preset dictionary is needed * @exception NullPointerException If <code>b</code> is <code>null</code>. * @exception IndexOutOfBoundsException If <code>off</code> is negative, <code>len</code> is * negative, or <code>len</code> is greater than <code>b.length - off</code> * @exception ZipException if a ZIP format error has occurred * @exception IOException if an I/O error has occurred */ public int read(byte[] b, int off, int len) throws IOException { ensureOpen(); if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } try { int n; while ((n = inf.inflate(b, off, len)) == 0) { if (inf.finished() || inf.needsDictionary()) { reachEOF = true; return -1; } if (inf.needsInput()) { fill(); } } return n; } catch (DataFormatException e) { String s = e.getMessage(); throw new ZipException(s != null ? s : "Invalid ZLIB data format"); } }
@Override public void decompress() { if (compressed) { Inflater decompressor = new Inflater(); decompressor.setInput(data); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buf = new byte[1024]; while (!decompressor.finished()) { try { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } catch (DataFormatException e) { } } try { bos.close(); } catch (IOException e) { } data = bos.toByteArray(); } }
/* (non-Javadoc) * @see org.eclipse.jetty.websocket.AbstractExtension#onFrame(byte, byte, org.eclipse.jetty.io.Buffer) */ @Override public void onFrame(byte flags, byte opcode, Buffer buffer) { if (getConnection().isControl(opcode) || !isFlag(flags, 1)) { super.onFrame(flags, opcode, buffer); return; } if (buffer.array() == null) buffer = buffer.asMutableBuffer(); int length = 0xff & buffer.get(); if (length >= 0x7e) { int b = (length == 0x7f) ? 8 : 2; length = 0; while (b-- > 0) length = 0x100 * length + (0xff & buffer.get()); } // TODO check a max framesize _inflater.setInput(buffer.array(), buffer.getIndex(), buffer.length()); ByteArrayBuffer buf = new ByteArrayBuffer(length); try { while (_inflater.getRemaining() > 0) { int inflated = _inflater.inflate(buf.array(), buf.putIndex(), buf.space()); if (inflated == 0) throw new DataFormatException("insufficient data"); buf.setPutIndex(buf.putIndex() + inflated); } super.onFrame(clearFlag(flags, 1), opcode, buf); } catch (DataFormatException e) { LOG.warn(e); getConnection().close(WebSocketConnectionRFC6455.CLOSE_BAD_PAYLOAD, e.toString()); } }
private boolean handleCompressedFrame(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable(FRAME_COMPRESS_HEADER_LENGTH)) { return false; } int compressedPayloadLength = in.readInt(); if (!in.isReadable(compressedPayloadLength)) { return false; } // decompress payload Inflater inflater = new Inflater(); if (in.hasArray()) { inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), compressedPayloadLength); in.skipBytes(compressedPayloadLength); } else { byte[] array = new byte[compressedPayloadLength]; in.readBytes(array); inflater.setInput(array); } while (!inflater.finished()) { ByteBuf decompressed = ctx.alloc().heapBuffer(1024, 1024); byte[] outArray = decompressed.array(); int count = inflater.inflate(outArray, decompressed.arrayOffset(), decompressed.writableBytes()); decompressed.writerIndex(count); // put data in the pipeline out.add(decompressed); } return true; }
/** * inflater 解壓縮 * * @param value * @return */ public static byte[] inflater(byte[] value) { byte[] result = new byte[0]; Inflater inflater = null; ByteArrayOutputStream out = null; try { inflater = new Inflater(); inflater.setInput(value); // out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; while (!inflater.finished()) { int count = inflater.inflate(buffer); out.write(buffer, 0, count); } // result = out.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (inflater != null) { inflater.end(); // 不end也不會有oom,不過還是關了吧 } IoHelper.close(out); } return result; }
public String getSystraceHtml() { if (mSystraceIndex < 0) { return ""; } String trace = ""; if (mUncompress) { Inflater decompressor = new Inflater(); decompressor.setInput(mAtraceOutput, mSystraceIndex, mAtraceLength - mSystraceIndex); byte[] buf = new byte[4096]; int n; StringBuilder sb = new StringBuilder(1000); try { while ((n = decompressor.inflate(buf)) > 0) { sb.append(new String(buf, 0, n)); } } catch (DataFormatException e) { throw new RuntimeException(e); } decompressor.end(); trace = sb.toString(); } else { trace = new String(mAtraceOutput, mSystraceIndex, mAtraceLength - mSystraceIndex); } // each line should end with the characters \n\ followed by a newline String html_out = trace.replaceAll("\n", "\\\\n\\\\\n"); String header = String.format(mHtmlPrefix, mCss, mJs, ""); String footer = mHtmlSuffix; return header + html_out + footer; }
void check(Inflater inf, byte[] tmp, long pos, int cnt) throws DataFormatException { // Unlike inflate() above the exact byte count is known by the caller. // Push all of it in a single invocation to avoid unnecessary loops. // inf.setInput(block, (int) (pos - start), cnt); while (inf.inflate(tmp, 0, tmp.length) > 0) continue; }
/** * Flushes this output stream, forcing any pending buffered output bytes to be written. * * @throws IOException if an I/O error occurs or this stream is already closed */ public void flush() throws IOException { ensureOpen(); // Finish decompressing and writing pending output data if (!inf.finished()) { try { while (!inf.finished() && !inf.needsInput()) { int n; // Decompress pending output data n = inf.inflate(buf, 0, buf.length); if (n < 1) { break; } // Write the uncompressed output data block out.write(buf, 0, n); } super.flush(); } catch (DataFormatException ex) { // Improperly formatted compressed (ZIP) data String msg = ex.getMessage(); if (msg == null) { msg = "Invalid ZLIB data format"; } throw new ZipException(msg); } } }
public static byte[] inflateObject(byte[] compressedData) throws Exception { // Create the decompressor and give it the data to compress Inflater decompressor = new Inflater(); decompressor.setInput(compressedData); // Create an expandable byte array to hold the decompressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length); // Decompress the data byte[] buf = new byte[1024]; while (!decompressor.finished()) { try { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } catch (DataFormatException e) { throw e; } finally { try { bos.close(); } catch (IOException ioe) { } } } // Get the decompressed data return bos.toByteArray(); }
private final byte[] uncompress(final byte[] input) throws IOException { Inflater decompressor = new Inflater(); decompressor.setInput(input); // Create an expandable byte array to hold the decompressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Decompress the data byte[] buf = new byte[1024]; while (!decompressor.finished()) { try { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } catch (DataFormatException e) { // this will happen if the field is not compressed IOException newException = new IOException("field data are in wrong format: " + e.toString()); newException.initCause(e); throw newException; } } decompressor.end(); // Get the decompressed data return bos.toByteArray(); }
@Override public TransformationResult getMoreData(byte opCode, boolean fin, int rsv, ByteBuffer dest) throws IOException { // Control frames are never compressed and may appear in the middle of // a WebSocket method. Pass them straight through. if (Util.isControl(opCode)) { return next.getMoreData(opCode, fin, rsv, dest); } if (!Util.isContinuation(opCode)) { // First frame in new message skipDecompression = (rsv & RSV_BITMASK) == 0; } // Pass uncompressed frames straight through. if (skipDecompression) { return next.getMoreData(opCode, fin, rsv, dest); } int written; boolean usedEomBytes = false; while (dest.remaining() > 0) { // Space available in destination. Try and fill it. try { written = inflater.inflate(dest.array(), dest.arrayOffset() + dest.position(), dest.remaining()); } catch (DataFormatException e) { throw new IOException(sm.getString("perMessageDeflate.deflateFailed"), e); } dest.position(dest.position() + written); if (inflater.needsInput() && !usedEomBytes) { if (dest.hasRemaining()) { readBuffer.clear(); TransformationResult nextResult = next.getMoreData(opCode, fin, (rsv ^ RSV_BITMASK), readBuffer); inflater.setInput(readBuffer.array(), readBuffer.arrayOffset(), readBuffer.position()); if (TransformationResult.UNDERFLOW.equals(nextResult)) { return nextResult; } else if (TransformationResult.END_OF_FRAME.equals(nextResult) && readBuffer.position() == 0) { if (fin) { inflater.setInput(EOM_BYTES); usedEomBytes = true; } else { return TransformationResult.END_OF_FRAME; } } } } else if (written == 0) { if (fin && (isServer && !clientContextTakeover || !isServer && !serverContextTakeover)) { inflater.reset(); } return TransformationResult.END_OF_FRAME; } } return TransformationResult.OVERFLOW; }
@Test public void testDecompressDeflateRequest() throws Exception { request.setMethod("POST"); request.setURI("/banner"); request.setHeader(HttpHeaders.CONTENT_ENCODING, "deflate"); request.setHeader(HttpHeaders.CONTENT_TYPE, PLAIN_TEXT_UTF_8); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DeflaterOutputStream deflate = new DeflaterOutputStream(baos)) { Resources.copy(Resources.getResource("assets/new-banner.txt"), deflate); } byte[] output = baos.toByteArray(); request.setContent(output); // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(output); byte[] result = new byte[4096]; int resultLength = decompresser.inflate(result); decompresser.end(); // Decode the bytes into a String System.out.println(new String(result, 0, resultLength, "UTF-8")); HttpTester.Response response = HttpTester.parseResponse(servletTester.getResponses(request.generate())); System.out.println(response.getStatus()); System.out.println(response.getContent()); }
public byte[] read_size_and_inflate() throws IOException { final int size = read4BE(); final byte[] buf = new byte[size]; final java.util.zip.Inflater inflater = new java.util.zip.Inflater(); final java.util.zip.InflaterInputStream is = new java.util.zip.InflaterInputStream(this, inflater); try { int pos = 0; while (pos < size) { // Inflate fully final int dsize = is.read(buf, pos, size - pos); if (dsize == 0) break; pos += dsize; } if (pos < size) { throw new IOException("Decompression gave " + pos + " bytes, not " + size); } // Back up if the inflater read ahead: int read_ahead = inflater.getRemaining(); setPos(getPos() - read_ahead); } finally { is.close(); } return buf; }
public static byte[] decompress(byte[] data) throws IOException, DataFormatException { Inflater inflater = new Inflater(); inflater.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!inflater.finished()) { int count = inflater.inflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); System.out.println("Original: " + data.length); System.out.println("Compressed: " + output.length); return output; }
public static String decompress(byte[] data) { int length = ByteBuffer.wrap(Arrays.copyOfRange(data, 0, 4)).getInt(); if (length > 100000) { // This is a sanity check. More than 100kb of password settings make no sense. System.out.println("Decompression error: The trasferred length is too big."); return ""; } Inflater inflater = new Inflater(); inflater.setInput(data, 4, data.length - 4); byte[] decompressedBytes = new byte[length]; try { if (inflater.inflate(decompressedBytes) != length) { throw new AssertionError(); } } catch (DataFormatException e) { e.printStackTrace(); } inflater.end(); try { return new String(decompressedBytes, "UTF-8"); } catch (UnsupportedEncodingException e) { System.out.println( "Decompression error: UTF-8 is not supported. " + "Using default encoding."); return new String(decompressedBytes); } }
/** * Writes an array of bytes to the uncompressed output stream. * * @param b buffer containing compressed data to decompress and write to the output stream * @param off starting offset of the compressed data within {@code b} * @param len number of bytes to decompress from {@code b} * @throws IndexOutOfBoundsException if {@code off < 0}, or if {@code len < 0}, or if {@code len > * b.length - off} * @throws IOException if an I/O error occurs or this stream is already closed * @throws NullPointerException if {@code b} is null * @throws ZipException if a compression (ZIP) format error occurs */ public void write(byte[] b, int off, int len) throws IOException { // Sanity checks ensureOpen(); if (b == null) { throw new NullPointerException("Null buffer for read"); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } // Write uncompressed data to the output stream try { for (; ; ) { int n; // Fill the decompressor buffer with output data if (inf.needsInput()) { int part; if (len < 1) { break; } part = (len < 512 ? len : 512); inf.setInput(b, off, part); off += part; len -= part; } // Decompress and write blocks of output data do { n = inf.inflate(buf, 0, buf.length); if (n > 0) { out.write(buf, 0, n); } } while (n > 0); // Check the decompressor if (inf.finished()) { break; } if (inf.needsDictionary()) { throw new ZipException("ZLIB dictionary missing"); } } } catch (DataFormatException ex) { // Improperly formatted compressed (ZIP) data String msg = ex.getMessage(); if (msg == null) { msg = "Invalid ZLIB data format"; } throw new ZipException(msg); } }
/** Abstract. Reads the raw packet data from the data stream. */ public void readPacketData(DataInputStream par1DataInputStream) throws IOException { short short1 = par1DataInputStream.readShort(); this.dataLength = par1DataInputStream.readInt(); this.skyLightSent = par1DataInputStream.readBoolean(); this.chunkPostX = new int[short1]; this.chunkPosZ = new int[short1]; this.field_73590_a = new int[short1]; this.field_73588_b = new int[short1]; this.field_73584_f = new byte[short1][]; if (chunkDataNotCompressed.length < this.dataLength) { chunkDataNotCompressed = new byte[this.dataLength]; } par1DataInputStream.readFully(chunkDataNotCompressed, 0, this.dataLength); byte[] abyte = new byte[196864 * short1]; Inflater inflater = new Inflater(); inflater.setInput(chunkDataNotCompressed, 0, this.dataLength); try { inflater.inflate(abyte); } catch (DataFormatException dataformatexception) { throw new IOException("Bad compressed data format"); } finally { inflater.end(); } int i = 0; for (int j = 0; j < short1; ++j) { this.chunkPostX[j] = par1DataInputStream.readInt(); this.chunkPosZ[j] = par1DataInputStream.readInt(); this.field_73590_a[j] = par1DataInputStream.readShort(); this.field_73588_b[j] = par1DataInputStream.readShort(); int k = 0; int l = 0; int i1; for (i1 = 0; i1 < 16; ++i1) { k += this.field_73590_a[j] >> i1 & 1; l += this.field_73588_b[j] >> i1 & 1; } i1 = 2048 * 4 * k + 256; i1 += 2048 * l; if (this.skyLightSent) { i1 += 2048 * k; } this.field_73584_f[j] = new byte[i1]; System.arraycopy(abyte, i, this.field_73584_f[j], 0, i1); i += i1; } }
public static byte[] decompress(byte buffer[]) throws IOException { byte result[] = new byte[Byteconvert.convertInt(buffer, 8)]; inflater.setInput(buffer, 12, buffer.length - 12); try { inflater.inflate(result); } catch (DataFormatException e) { throw new IOException(); } inflater.reset(); return result; }
/** * Release an inflater previously obtained from this cache. * * @param i the inflater to return. May be null, in which case this method does nothing. */ public static void release(final Inflater i) { if (i == null) return; if (openInflaterCount == SZ) { i.end(); return; } i.reset(); releaseImpl(i); }
@Override public void decode(ChannelBuffer decompressed) throws Exception { try { int numBytes = decompressor.inflate(out); if (numBytes == 0 && decompressor.needsDictionary()) { decompressor.setDictionary(SPDY_DICT); numBytes = decompressor.inflate(out); } decompressed.writeBytes(out, 0, numBytes); } catch (DataFormatException e) { throw new SpdyProtocolException("Received invalid header block", e); } }
/** * @param compressed_size or use null if not known * @param uncompressed_size or use null if not known * @return uncompressed ByteArrayOutputStream * @throws IOException * @throws DataFormatException */ private ByteArrayOutputStream unCompress(Integer compressed_size, Integer uncompressed_size) throws IOException, DataFormatException { byte[] uncompressed_data = null; byte[] input_data = null; ByteArrayOutputStream ret = new ByteArrayOutputStream(); Inflater decompresser = new Inflater(false); long first_seek = fileChannel.position(); Boolean uncompressing = true; while (uncompressing) { if (decompresser.needsInput()) { input_data = new byte[(compressed_size != null) ? compressed_size.intValue() : 1024]; fileChannel.read(ByteBuffer.wrap(input_data)); decompresser.setInput(input_data, 0, input_data.length); } uncompressed_data = new byte [(uncompressed_size != null) ? uncompressed_size.intValue() : (input_data.length * 4)]; decompresser.inflate(uncompressed_data); int op = (int) (decompresser.getBytesWritten() - (long) ret.size()); if (op > 0) ret.write(uncompressed_data, 0, op); if (decompresser.finished()) uncompressing = false; } fileChannel.position( (first_seek + decompresser.getBytesRead())); // move file pointer to start of next stream decompresser.end(); return ret; }
private int inflate(byte[] src, byte[] dest) { Inflater inflater = (inflaterRef == null ? null : inflaterRef.get()); // construct the inflater object or reuse an existing one if (inflater == null) inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true)); inflater.reset(); inflater.setInput(src); try { return inflater.inflate(dest); } catch (DataFormatException ex) { return -1; } }
public byte[] uncompress(byte[] bytes) throws DataFormatException { try { inflater.setInput(bytes); int len = inflater.inflate(buffer); byte[] uncompressedBytes = new byte[len]; System.arraycopy(buffer, 0, uncompressedBytes, 0, len); return uncompressedBytes; } finally { try { inflater.reset(); } catch (Exception e) { logger.warn("inflater.reset failed", e); } } }
private int inflate(byte[] bytes) throws ZipException { try { return inflater.inflate(bytes); } catch (DataFormatException x) { throw new ZipException(x.getMessage()); } }
/** * Closes this input stream and releases any system resources associated with the stream. * * @exception IOException if an I/O error has occurred */ public void close() throws IOException { if (!closed) { if (usesDefaultInflater) inf.end(); in.close(); closed = true; } }
/** TIFF Adobe ZIP support contributed by Jason Newton. */ public byte[] zipUncompress(byte[] input) { ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; Inflater decompressor = new Inflater(); decompressor.setInput(input); try { while (!decompressor.finished()) { int rlen = decompressor.inflate(buffer); imageBuffer.write(buffer, 0, rlen); } } catch (DataFormatException e) { IJ.log(e.toString()); } decompressor.end(); return imageBuffer.toByteArray(); }
public static byte[] decompress(byte[] compressedData) throws IOException { Inflater inflater = new Inflater(); inflater.setInput(compressedData); ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length); byte[] buf = new byte[1024]; while (!inflater.finished()) { try { int count = inflater.inflate(buf); bos.write(buf, 0, count); } catch (DataFormatException e) { } } bos.close(); inflater.end(); return bos.toByteArray(); }
/** * Decoding and deflating the encoded AuthReq * * @param encodedStr encoded AuthReq * @return decoded AuthReq */ public static String decode(String encodedStr) throws SAMLSSOException { try { org.apache.commons.codec.binary.Base64 base64Decoder = new org.apache.commons.codec.binary.Base64(); byte[] xmlBytes = encodedStr.getBytes("UTF-8"); byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes); try { // TODO if the request came in POST, inflating is wrong Inflater inflater = new Inflater(true); inflater.setInput(base64DecodedByteArray); byte[] xmlMessageBytes = new byte[5000]; int resultLength = inflater.inflate(xmlMessageBytes); if (inflater.getRemaining() > 0) { throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data"); } inflater.end(); String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8"); if (log.isDebugEnabled()) { log.debug("Request message " + decodedString); } return decodedString; } catch (DataFormatException e) { ByteArrayInputStream bais = new ByteArrayInputStream(base64DecodedByteArray); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InflaterInputStream iis = new InflaterInputStream(bais); byte[] buf = new byte[1024]; int count = iis.read(buf); while (count != -1) { baos.write(buf, 0, count); count = iis.read(buf); } iis.close(); String decodedStr = new String(baos.toByteArray(), Charset.forName("UTF-8")); if (log.isDebugEnabled()) { log.debug("Request message " + decodedStr); } return decodedStr; } } catch (IOException e) { throw new SAMLSSOException("Error when decoding the SAML Request.", e); } }
/** * Fills input buffer with more data to decompress. * * @exception IOException if an I/O error has occurred */ protected void fill() throws IOException { ensureOpen(); len = in.read(buf, 0, buf.length); if (len == -1) { throw new EOFException("Unexpected end of ZLIB input stream"); } inf.setInput(buf, 0, len); }
/** * Reads from the compressed stream and stores the resulting uncompressed data into the byte * array. * * @return number of bytes read, or -1 upon EOF */ public int read(byte[] b, int off, int len) throws IOException { if (len <= 0 || off < 0 || off + len > b.length) return 0; if (_eof) return -1; // Read from uncompressed stream if (!_isGzip) return _in.read(b, off, len); try { int sublen; int length = 0; while (length < len) { if (_inflater.needsInput()) { _readBufferSize = _in.read(_readBuffer, 0, _readBuffer.length); if (_readBufferSize < 0) break; _inflater.setInput(_readBuffer, 0, _readBufferSize); } sublen = _inflater.inflate(b, off + length, len - length); _crc.update(b, off + length, sublen); _inputSize += sublen; _totalInputSize += sublen; length += sublen; // Unread gzip trailer and possibly beginning of appended gzip data. if (_inflater.finished()) { int remaining = _inflater.getRemaining(); _in.unread(_readBuffer, _readBufferSize - remaining, remaining); readTrailer(); int secondPart = read(b, off + length, len - length); return secondPart > 0 ? length + secondPart : length; } } return length; } catch (DataFormatException e) { throw new IOException(e.getMessage()); } }