Search in sources :

Example 31 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project poi by apache.

the class ExOleObjStg method getData.

/**
     * Opens an input stream which will decompress the data on the fly.
     *
     * @return the data input stream.
     */
public InputStream getData() {
    if (isCompressed()) {
        int size = LittleEndian.getInt(_data);
        InputStream compressedStream = new ByteArrayInputStream(_data, 4, _data.length);
        return new BoundedInputStream(new InflaterInputStream(compressedStream), size);
    } else {
        return new ByteArrayInputStream(_data, 0, _data.length);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream)

Example 32 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project poi by apache.

the class EscherMetafileBlip method inflatePictureData.

/**
     * Decompresses the provided data, returning the inflated result.
     *
     * @param data the deflated picture data.
     * @return the inflated picture data.
     */
private static byte[] inflatePictureData(byte[] data) {
    try {
        InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(data));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int readBytes;
        while ((readBytes = in.read(buf)) > 0) {
            out.write(buf, 0, readBytes);
        }
        return out.toByteArray();
    } catch (IOException e) {
        log.log(POILogger.WARN, "Possibly corrupt compression or non-compressed data", e);
        return data;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 33 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project poi by apache.

the class EscherPictBlip method inflatePictureData.

/**
     * Decompresses the provided data, returning the inflated result.
     *
     * @param data the deflated picture data.
     * @return the inflated picture data.
     */
private static byte[] inflatePictureData(byte[] data) {
    try {
        InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(data));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int readBytes;
        while ((readBytes = in.read(buf)) > 0) {
            out.write(buf, 0, readBytes);
        }
        return out.toByteArray();
    } catch (IOException e) {
        log.log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e);
        return data;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 34 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project poi by apache.

the class PICT method read.

private byte[] read(byte[] data, int pos) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    Header header = new Header();
    header.read(data, pos);
    long bs_exp = (long) pos + header.getSize();
    long bs_act = bis.skip(bs_exp);
    if (bs_exp != bs_act) {
        throw new EOFException();
    }
    byte[] chunk = new byte[4096];
    ByteArrayOutputStream out = new ByteArrayOutputStream(header.getWmfSize());
    InflaterInputStream inflater = new InflaterInputStream(bis);
    try {
        int count;
        while ((count = inflater.read(chunk)) >= 0) {
            out.write(chunk, 0, count);
            // PICT zip-stream can be erroneous, so we clear the array to determine
            // the maximum of read bytes, after the inflater crashed
            bytefill(chunk, (byte) 0);
        }
    } catch (Exception e) {
        int lastLen;
        for (lastLen = chunk.length - 1; lastLen >= 0 && chunk[lastLen] == 0; lastLen--) ;
        if (++lastLen > 0) {
            if (header.getWmfSize() > out.size()) {
                // sometimes the wmfsize is smaller than the amount of already successfully read bytes
                // in this case we take the lastLen as-is, otherwise we truncate it to the given size
                lastLen = Math.min(lastLen, header.getWmfSize() - out.size());
            }
            out.write(chunk, 0, lastLen);
        }
        // End of picture marker for PICT is 0x00 0xFF
        LOG.log(POILogger.ERROR, "PICT zip-stream is invalid, read as much as possible. Uncompressed length of header: " + header.getWmfSize() + " / Read bytes: " + out.size(), e);
    } finally {
        inflater.close();
    }
    return out.toByteArray();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) EOFException(java.io.EOFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFException(org.apache.poi.hslf.exceptions.HSLFException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 35 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project poi by apache.

the class EMF method getData.

@Override
public byte[] getData() {
    try {
        byte[] rawdata = getRawData();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream is = new ByteArrayInputStream(rawdata);
        Header header = new Header();
        header.read(rawdata, CHECKSUM_SIZE);
        long len = is.skip(header.getSize() + (long) CHECKSUM_SIZE);
        assert (len == header.getSize() + CHECKSUM_SIZE);
        InflaterInputStream inflater = new InflaterInputStream(is);
        byte[] chunk = new byte[4096];
        int count;
        while ((count = inflater.read(chunk)) >= 0) {
            out.write(chunk, 0, count);
        }
        inflater.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new HSLFException(e);
    }
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)102 ByteArrayInputStream (java.io.ByteArrayInputStream)58 InputStream (java.io.InputStream)48 IOException (java.io.IOException)39 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 Inflater (java.util.zip.Inflater)35 GZIPInputStream (java.util.zip.GZIPInputStream)26 DataInputStream (java.io.DataInputStream)17 BufferedInputStream (java.io.BufferedInputStream)12 HttpURLConnection (java.net.HttpURLConnection)9 OutputStream (java.io.OutputStream)8 URL (java.net.URL)8 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)8 BufferedReader (java.io.BufferedReader)6 InputStreamReader (java.io.InputStreamReader)6 URLConnection (java.net.URLConnection)6 EOFException (java.io.EOFException)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 SocketException (java.net.SocketException)4