Search in sources :

Example 36 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 37 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)

Example 38 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project intellij-community by JetBrains.

the class RefCountingStorage method internalReadStream.

private BufferExposingByteArrayOutputStream internalReadStream(int record) throws IOException {
    waitForPendingWriteForRecord(record);
    byte[] result;
    synchronized (myLock) {
        result = super.readBytes(record);
    }
    InflaterInputStream in = new CustomInflaterInputStream(result);
    try {
        final BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream();
        StreamUtil.copyStreamContent(in, outputStream);
        return outputStream;
    } finally {
        in.close();
    }
}
Also used : BufferExposingByteArrayOutputStream(com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream) InflaterInputStream(java.util.zip.InflaterInputStream)

Example 39 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project intellij-community by JetBrains.

the class ConnectionStreams method setGzipped.

// Actions ================================================================
// Actions ================================================================
public void setGzipped() throws IOException {
    loggedWriter.flush();
    loggedOutputStream.flush();
    deflaterOutputStream = new DeflaterOutputStream(connection.getOutputStream(), new Deflater(6));
    setOutputStream(deflaterOutputStream);
    setInputStream(new InflaterInputStream(connection.getInputStream()));
}
Also used : Deflater(java.util.zip.Deflater) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 40 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project gerrit by GerritCodeReview.

the class PatchList method readObject.

private void readObject(final ObjectInputStream input) throws IOException {
    final ByteArrayInputStream buf = new ByteArrayInputStream(readBytes(input));
    try (InflaterInputStream in = new InflaterInputStream(buf)) {
        oldId = readCanBeNull(in);
        newId = readNotNull(in);
        isMerge = readVarInt32(in) != 0;
        comparisonType = ComparisonType.readFrom(in);
        insertions = readVarInt32(in);
        deletions = readVarInt32(in);
        final int cnt = readVarInt32(in);
        final PatchListEntry[] all = new PatchListEntry[cnt];
        for (int i = 0; i < all.length; i++) {
            all[i] = PatchListEntry.readFrom(in);
        }
        patches = all;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)91 ByteArrayInputStream (java.io.ByteArrayInputStream)49 InputStream (java.io.InputStream)46 IOException (java.io.IOException)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 Inflater (java.util.zip.Inflater)33 GZIPInputStream (java.util.zip.GZIPInputStream)26 DataInputStream (java.io.DataInputStream)16 BufferedInputStream (java.io.BufferedInputStream)11 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