Search in sources :

Example 61 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project teaTime by ancfdy.

the class AppZLibMgr method decompress.

/**
 * 解压缩
 *
 * @param is
 *            输入流
 * @return byte[] 解压缩后的数据
 */
public static byte[] decompress(InputStream is) {
    InflaterInputStream iis = new InflaterInputStream(is);
    ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
    try {
        int i = 1024;
        byte[] buf = new byte[i];
        while ((i = iis.read(buf, 0, i)) > 0) {
            o.write(buf, 0, i);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return o.toByteArray();
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 62 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project commons by twitter.

the class Base64ZlibCodec method decompress.

private static byte[] decompress(byte[] compressed) throws InvalidDataException {
    byte[] bytes;
    try {
        final InputStream bin = new ByteArrayInputStream(compressed);
        final InputStream zin;
        if (startsWith(compressed, GZIP_HEADER_PREFIX)) {
            zin = new GZIPInputStream(bin);
        } else if (startsWith(compressed, ZLIB_HEADER_PREFIX)) {
            zin = new InflaterInputStream(bin);
        } else {
            throw new Base64ZlibCodec.InvalidDataException("Value doesn't start with either GZIP or zlib header");
        }
        try {
            bytes = ByteStreams.toByteArray(zin);
        } finally {
            zin.close();
        }
    } catch (IOException e) {
        throw new Base64ZlibCodec.InvalidDataException("zlib/GZIP decoding error", e);
    }
    return bytes;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) IOException(java.io.IOException)

Example 63 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project hazelcast by hazelcast.

the class MetricsCompressor method readDictionary.

private static String[] readDictionary(byte[] dictionaryBlob) throws IOException {
    try (DataInputStream dis = new DataInputStream(new InflaterInputStream(new ByteArrayInputStream(dictionaryBlob)))) {
        int dictionarySize = dis.readInt();
        String[] dictionary = new String[dictionarySize];
        String lastWord = "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < dictionarySize; i++) {
            int dictionaryId = dis.readInt();
            int commonLen = dis.readUnsignedByte();
            int diffLen = dis.readUnsignedByte();
            sb.append(lastWord, 0, commonLen);
            for (int j = 0; j < diffLen; j++) {
                sb.append(dis.readChar());
            }
            String readWord = sb.toString();
            lastWord = readWord;
            dictionary[dictionaryId] = readWord;
            sb.setLength(0);
        }
        return dictionary;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DataInputStream(java.io.DataInputStream)

Example 64 with InflaterInputStream

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

the class HttpLib method getInputStream.

/**
 * Get the InputStream from an HttpResponse, handling possible compression settings.
 * The application must consume or close the {@code InputStream} (see {@link #finish(InputStream)}).
 * Closing the InputStream may close the HTTP connection.
 * Assumes the status code has been handled e.g. {@link #handleHttpStatusCode} has been called.
 */
public static InputStream getInputStream(HttpResponse<InputStream> httpResponse) {
    String encoding = httpResponse.headers().firstValue(HttpNames.hContentEncoding).orElse("");
    InputStream responseInput = httpResponse.body();
    // "Content-Encoding: chunked, <compression>"
    try {
        switch(encoding) {
            case "":
            case // Proper name for no compression.
            "identity":
                return responseInput;
            case "gzip":
                return new GZIPInputStream(responseInput, 2 * 1024);
            case "inflate":
                return new InflaterInputStream(responseInput);
            // RFC7932
            case "br":
            default:
                throw new UnsupportedOperationException("Not supported: Content-Encoding: " + encoding);
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) UncheckedIOException(java.io.UncheckedIOException) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 65 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project j2objc by google.

the class DeflaterOutputStreamTest method test_write$BII.

/**
 * java.util.zip.DeflaterOutputStream#write(byte[], int, int)
 */
/* J2ObjC removed: not supported by Junit 4.11 (https://github.com/google/j2objc/issues/1318).
    @DisableResourceLeakageDetection(
            why = "DeflaterOutputStream.close() does not work properly if finish() throws an"
                    + " exception; DeflaterOutputStream.finish() throws an exception if the"
                    + " underlying OutputStream has been closed and the Deflater still has data to"
                    + " write.",
            bug = "31797037") */
public void test_write$BII() throws Exception {
    byte[] byteArray = { 1, 3, 4, 7, 8, 3, 6 };
    // Test to see if the correct bytes are saved.
    File f1 = File.createTempFile("writeBII", ".tst");
    FileOutputStream fos1 = new FileOutputStream(f1);
    DeflaterOutputStream dos1 = new DeflaterOutputStream(fos1);
    dos1.write(byteArray, 2, 3);
    dos1.close();
    FileInputStream fis = new FileInputStream(f1);
    InflaterInputStream iis = new InflaterInputStream(fis);
    assertEquals("Incorrect Byte Returned.", 4, iis.read());
    assertEquals("Incorrect Byte Returned.", 7, iis.read());
    assertEquals("Incorrect Byte Returned.", 8, iis.read());
    assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
    assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
    iis.close();
    f1.delete();
    // Test for trying to write more bytes than available from the array
    File f2 = File.createTempFile("writeBII2", ".tst");
    FileOutputStream fos2 = new FileOutputStream(f2);
    DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
    try {
        dos2.write(byteArray, 2, 10);
        fail("IndexOutOfBoundsException not thrown");
    } catch (IndexOutOfBoundsException e) {
    }
    // Test for trying to write a negative number of bytes.
    try {
        dos2.write(byteArray, 2, Integer.MIN_VALUE);
        fail("IndexOutOfBoundsException not thrown");
    } catch (IndexOutOfBoundsException e) {
    }
    // Test for trying to start writing from a byte < 0 from the array.
    try {
        dos2.write(byteArray, Integer.MIN_VALUE, 2);
        fail("IndexOutOfBoundsException not thrown");
    } catch (IndexOutOfBoundsException e) {
    }
    // size.
    try {
        dos2.write(byteArray, 10, 2);
        fail("IndexOutOfBoundsException not thrown");
    } catch (IndexOutOfBoundsException e) {
    }
    dos2.close();
    // Not sure if this test is that important.
    // Checks to see if you can write using the DeflaterOutputStream
    // after
    // the FileOutputStream has been closed.
    FileOutputStream fos3 = new FileOutputStream(f2);
    DeflaterOutputStream dos3 = new DeflaterOutputStream(fos3);
    fos3.close();
    try {
        dos3.write(byteArray, 2, 3);
        fail("IOException not thrown");
    } catch (IOException e) {
    }
    // Close to try and free up the resources.
    try {
        dos3.close();
        fail("IOException not thrown");
    } catch (IOException e) {
    }
    f2.delete();
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) FileOutputStream(java.io.FileOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)200 ByteArrayInputStream (java.io.ByteArrayInputStream)113 InputStream (java.io.InputStream)100 IOException (java.io.IOException)74 Inflater (java.util.zip.Inflater)66 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 GZIPInputStream (java.util.zip.GZIPInputStream)39 DataInputStream (java.io.DataInputStream)33 FileInputStream (java.io.FileInputStream)27 BufferedInputStream (java.io.BufferedInputStream)24 InputStreamReader (java.io.InputStreamReader)13 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)13 HttpURLConnection (java.net.HttpURLConnection)12 URL (java.net.URL)11 File (java.io.File)10 BufferedReader (java.io.BufferedReader)9 OutputStream (java.io.OutputStream)9 Point (java.awt.Point)7 EOFException (java.io.EOFException)7 URLConnection (java.net.URLConnection)7