List of usage examples for java.util.zip Inflater Inflater
public Inflater()
From source file:org.getspout.spout.packet.PacketCustomMultiBlockOverride.java
public void decompress() { if (compressed) { Inflater decompressor = new Inflater(); decompressor.setInput(data);/* ww w. j a v a 2 s . c o m*/ 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) { } compressed = false; data = bos.toByteArray(); } }
From source file:org.getspout.spoutapi.packet.PacketCustomMultiBlockOverride.java
@Override public void decompress() { if (compressed) { Inflater decompressor = new Inflater(); decompressor.setInput(data);//from w ww . j a v a2 s. c o m 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(); } }
From source file:Version2LicenseDecoder.java
private Reader unzipText(byte[] licenseText) { ByteArrayInputStream in = new ByteArrayInputStream(licenseText); in.skip((long) LICENSE_PREFIX.length); InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater()); try {/*w w w . j a v a2 s.c o m*/ return new InputStreamReader(zipIn, "UTF-8"); } catch (UnsupportedEncodingException var5) { throw new LicenseException(var5); } }
From source file:z.hol.net.http.entity.DeflateDecompressingEntity.java
/** * Returns the non-null InputStream that should be returned to by all requests to * {@link #getContent()}./*w ww . j a v a 2 s. c o m*/ * * @return a non-null InputStream * @throws IOException if there was a problem */ @Override InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException { /* * A zlib stream will have a header. * * CMF | FLG [| DICTID ] | ...compressed data | ADLER32 | * * * CMF is one byte. * * * FLG is one byte. * * * DICTID is four bytes, and only present if FLG.FDICT is set. * * Sniff the content. Does it look like a zlib stream, with a CMF, etc? c.f. RFC1950, * section 2.2. http://tools.ietf.org/html/rfc1950#page-4 * * We need to see if it looks like a proper zlib stream, or whether it is just a deflate * stream. RFC2616 calls zlib streams deflate. Confusing, isn't it? That's why some servers * implement deflate Content-Encoding using deflate streams, rather than zlib streams. * * We could start looking at the bytes, but to be honest, someone else has already read * the RFCs and implemented that for us. So we'll just use the JDK libraries and exception * handling to do this. If that proves slow, then we could potentially change this to check * the first byte - does it look like a CMF? What about the second byte - does it look like * a FLG, etc. */ /* We read a small buffer to sniff the content. */ byte[] peeked = new byte[6]; PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.length); int headerLength = pushback.read(peeked); if (headerLength == -1) { throw new IOException("Unable to read the response"); } /* We try to read the first uncompressed byte. */ byte[] dummy = new byte[1]; Inflater inf = new Inflater(); try { int n; while ((n = inf.inflate(dummy)) == 0) { if (inf.finished()) { /* Not expecting this, so fail loudly. */ throw new IOException("Unable to read the response"); } if (inf.needsDictionary()) { /* Need dictionary - then it must be zlib stream with DICTID part? */ break; } if (inf.needsInput()) { inf.setInput(peeked); } } if (n == -1) { throw new IOException("Unable to read the response"); } /* * We read something without a problem, so it's a valid zlib stream. Just need to reset * and return an unused InputStream now. */ pushback.unread(peeked, 0, headerLength); return new InflaterInputStream(pushback); } catch (DataFormatException e) { /* Presume that it's an RFC1951 deflate stream rather than RFC1950 zlib stream and try * again. */ pushback.unread(peeked, 0, headerLength); return new InflaterInputStream(pushback, new Inflater(true)); } }
From source file:org.openteufel.file.mpq.MPQFileSector.java
public int getDecompressed(ByteBuffer out) throws DataFormatException, IOException { // If the file is encrypted, each sector (after compression/implosion, if applicable) is encrypted with the file's key. // Each sector is encrypted using the key + the 0-based index of the sector in the file. // NOTE compression type byte (if existing) is encrypted as well! ByteBuffer dataDecrypted;/* ww w .java 2 s . co m*/ if (this.encryptionSeed != null) dataDecrypted = MPQEncryptionUtils.decrypt(dataRaw, encryptionSeed); else dataDecrypted = dataRaw; dataDecrypted.rewind(); switch (compression) { case Uncompressed: { out.put(dataDecrypted); return dataDecrypted.capacity(); } case Imploded: { byte[] buf = new byte[sizeUncompressed]; int numDecompressed = Exploder.pkexplode(dataDecrypted.array(), buf); if (numDecompressed != this.sizeUncompressed) throw new IllegalStateException(); out.put(buf, 0, sizeUncompressed); return sizeUncompressed; } case ZLib: { int numDecompressed = 0; byte[] buf = new byte[1024]; Inflater inflater = new Inflater(); inflater.setInput(dataDecrypted.array()); while (!inflater.finished()) { int decompressedBytes = inflater.inflate(buf); numDecompressed += decompressedBytes; out.put(buf, 0, decompressedBytes); } inflater.end(); if (numDecompressed != this.sizeUncompressed) throw new IllegalStateException(); return numDecompressed; } case BZip2: { int numDecompressed = 0; byte[] buf = new byte[1024]; InputStream inputStream = new ByteArrayInputStream(dataDecrypted.array()); BZip2CompressorInputStream uncompressStream = new BZip2CompressorInputStream(inputStream); while (true) { int decompressedBytes = uncompressStream.read(buf); if (decompressedBytes < 0) break; numDecompressed += decompressedBytes; out.put(buf, 0, decompressedBytes); } uncompressStream.close(); inputStream.close(); if (numDecompressed != sizeUncompressed) throw new IllegalStateException(); return numDecompressed; } default: throw new IllegalStateException("Unknown Compression"); } }
From source file:org.hyperic.hq.livedata.agent.commands.LiveData_result.java
private String decompress(String s) throws IOException, DataFormatException { Inflater decompressor = new Inflater(); decompressor.setInput(Base64.decode(s)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count);/* w ww .j av a 2s . c o m*/ } bos.close(); byte[] decompressedData = bos.toByteArray(); return new String(decompressedData, 0, decompressedData.length, "UTF-8"); }
From source file:org.zaproxy.zap.extension.ascanrulesAlpha.GitMetadata.java
/** * inflate the byte array, using the specified buffer size * * @param data the data to inflate// w w w . j av a 2 s . c o m * @param buffersize the buffer size to use when inflating the data * @return the inflated data * @throws Exception */ protected byte[] inflate(byte[] data, int buffersize) throws Exception { Inflater inflater = new Inflater(); inflater.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[buffersize]; while (!inflater.finished()) { int count = inflater.inflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); inflater.end(); return outputStream.toByteArray(); }
From source file:org.getspout.spoutapi.packet.PacketCustomBlockChunkOverride.java
@Override public void decompress() { if (compressed && hasData) { Inflater decompressor = new Inflater(); decompressor.setInput(data);//from w w w. j av a 2 s. c o m 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(); } }
From source file:org.apache.fop.render.pdf.ImageRawPNGAdapter.java
/** {@inheritDoc} */ public void setup(PDFDocument doc) { super.setup(doc); ColorModel cm = ((ImageRawPNG) this.image).getColorModel(); if (cm instanceof IndexColorModel) { numberOfInterleavedComponents = 1; } else {/* w ww. j ava 2s . c o m*/ // this can be 1 (gray), 2 (gray + alpha), 3 (rgb) or 4 (rgb + alpha) // numberOfInterleavedComponents = (cm.hasAlpha() ? 1 : 0) + cm.getNumColorComponents(); numberOfInterleavedComponents = cm.getNumComponents(); } // set up image compression for non-alpha channel FlateFilter flate; try { flate = new FlateFilter(); flate.setApplied(true); flate.setPredictor(FlateFilter.PREDICTION_PNG_OPT); if (numberOfInterleavedComponents < 3) { // means palette (1) or gray (1) or gray + alpha (2) flate.setColors(1); } else { // means rgb (3) or rgb + alpha (4) flate.setColors(3); } flate.setColumns(image.getSize().getWidthPx()); flate.setBitsPerComponent(this.getBitsPerComponent()); } catch (PDFFilterException e) { throw new RuntimeException("FlateFilter configuration error", e); } this.pdfFilter = flate; this.disallowMultipleFilters(); // Handle transparency channel if applicable; note that for palette images the transparency is // not TRANSLUCENT if (cm.hasAlpha() && cm.getTransparency() == ColorModel.TRANSLUCENT) { doc.getProfile().verifyTransparencyAllowed(image.getInfo().getOriginalURI()); // TODO: Implement code to combine image with background color if transparency is not allowed // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha channel // and then deflate it back again ByteArrayOutputStream baos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater()); InputStream in = ((ImageRawStream) image).createInputStream(); try { InflaterInputStream infStream = new InflaterInputStream(in, new Inflater()); DataInputStream dataStream = new DataInputStream(infStream); // offset is the byte offset of the alpha component int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA int numColumns = image.getSize().getWidthPx(); int bytesPerRow = numberOfInterleavedComponents * numColumns; int filter; // read line by line; the first byte holds the filter while ((filter = dataStream.read()) != -1) { byte[] bytes = new byte[bytesPerRow]; dataStream.readFully(bytes, 0, bytesPerRow); dos.write((byte) filter); for (int j = 0; j < numColumns; j++) { dos.write(bytes, offset, 1); offset += numberOfInterleavedComponents; } offset = numberOfInterleavedComponents - 1; } dos.close(); } catch (IOException e) { throw new RuntimeException("Error processing transparency channel:", e); } finally { IOUtils.closeQuietly(in); } // set up alpha channel compression FlateFilter transFlate; try { transFlate = new FlateFilter(); transFlate.setApplied(true); transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT); transFlate.setColors(1); transFlate.setColumns(image.getSize().getWidthPx()); transFlate.setBitsPerComponent(this.getBitsPerComponent()); } catch (PDFFilterException e) { throw new RuntimeException("FlateFilter configuration error", e); } BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(), image.getSize().getHeightPx(), baos.toByteArray(), null); alphaMask.setPDFFilter(transFlate); alphaMask.disallowMultipleFilters(); alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY)); softMask = doc.addImage(null, alphaMask).makeReference(); } }
From source file:org.exist.xmldb.RemoteResourceSet.java
@Override public Resource getMembersAsResource() throws XMLDBException { final List<Object> params = new ArrayList<>(); params.add(Integer.valueOf(handle)); params.add(outputProperties);/*from www .ja v a2 s .com*/ try { final Path tmpfile = TemporaryFileManager.getInstance().getTemporaryFile(); try (final OutputStream os = Files.newOutputStream(tmpfile)) { Map<?, ?> table = (Map<?, ?>) xmlRpcClient.execute("retrieveAllFirstChunk", params); long offset = ((Integer) table.get("offset")).intValue(); byte[] data = (byte[]) table.get("data"); final boolean isCompressed = "yes" .equals(outputProperties.getProperty(EXistOutputKeys.COMPRESS_OUTPUT, "no")); // One for the local cached file Inflater dec = null; byte[] decResult = null; int decLength = 0; if (isCompressed) { dec = new Inflater(); decResult = new byte[65536]; dec.setInput(data); do { decLength = dec.inflate(decResult); os.write(decResult, 0, decLength); } while (decLength == decResult.length || !dec.needsInput()); } else { os.write(data); } while (offset > 0) { params.clear(); params.add(table.get("handle")); params.add(Long.toString(offset)); table = (Map<?, ?>) xmlRpcClient.execute("getNextExtendedChunk", params); offset = Long.parseLong((String) table.get("offset")); data = (byte[]) table.get("data"); // One for the local cached file if (isCompressed) { dec.setInput(data); do { decLength = dec.inflate(decResult); os.write(decResult, 0, decLength); } while (decLength == decResult.length || !dec.needsInput()); } else { os.write(data); } } if (dec != null) { dec.end(); } final RemoteXMLResource res = new RemoteXMLResource(leasableXmlRpcClient.lease(), collection, handle, 0, XmldbURI.EMPTY_URI, Optional.empty()); res.setContent(tmpfile); res.setProperties(outputProperties); return res; } catch (final XmlRpcException xre) { final byte[] data = (byte[]) xmlRpcClient.execute("retrieveAll", params); String content; try { content = new String(data, outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8")); } catch (final UnsupportedEncodingException ue) { LOG.warn(ue); content = new String(data); } final RemoteXMLResource res = new RemoteXMLResource(leasableXmlRpcClient.lease(), collection, handle, 0, XmldbURI.EMPTY_URI, Optional.empty()); res.setContent(content); res.setProperties(outputProperties); return res; } catch (final IOException | DataFormatException ioe) { throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe); } } catch (final IOException ioe) { throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe); } catch (final XmlRpcException xre) { throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, xre.getMessage(), xre); } }