Search in sources :

Example 66 with Deflater

use of java.util.zip.Deflater in project camel by apache.

the class ZipDataFormat method marshal.

public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
    // ask for a mandatory type conversion to avoid a possible NPE beforehand as we do copy from the InputStream
    final InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph);
    final Deflater deflater = new Deflater(compressionLevel);
    final DeflaterOutputStream zipOutput = new DeflaterOutputStream(stream, deflater);
    try {
        IOHelper.copy(is, zipOutput);
    } finally {
        IOHelper.close(is, zipOutput);
        /*
            * As we create the Deflater our self and do not use the stream default
            * (see {@link java.util.zip.DeflaterOutputStream#usesDefaultDeflater})
            * we need to close the Deflater to not risk a OutOfMemoryException
            * in native code parts (see {@link java.util.zip.Deflater#end})
            */
        deflater.end();
    }
}
Also used : Deflater(java.util.zip.Deflater) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 67 with Deflater

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

the class AppZLibMgr method compress.

/**
     * 压缩
     *
     * @param data
     *
     * @return byte[] 压缩后的数据
     */
public static byte[] compress(byte[] data) {
    byte[] output = new byte[0];
    Deflater compresser = new Deflater();
    compresser.reset();
    compresser.setInput(data);
    compresser.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!compresser.finished()) {
            int i = compresser.deflate(buf);
            bos.write(buf, 0, i);
        }
        output = bos.toByteArray();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    compresser.end();
    return output;
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 68 with Deflater

use of java.util.zip.Deflater in project cloudstack by apache.

the class SAMLUtils method encodeSAMLRequest.

public static String encodeSAMLRequest(XMLObject authnRequest) throws MarshallingException, IOException {
    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(authnRequest);
    Element authDOM = marshaller.marshall(authnRequest);
    StringWriter requestWriter = new StringWriter();
    XMLHelper.writeNode(authDOM, requestWriter);
    String requestMessage = requestWriter.toString();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    deflaterOutputStream.write(requestMessage.getBytes(Charset.forName("UTF-8")));
    deflaterOutputStream.close();
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
    encodedRequestMessage = URLEncoder.encode(encodedRequestMessage, HttpUtils.UTF_8).trim();
    return encodedRequestMessage;
}
Also used : Marshaller(org.opensaml.xml.io.Marshaller) StringWriter(java.io.StringWriter) Deflater(java.util.zip.Deflater) Element(org.w3c.dom.Element) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 69 with Deflater

use of java.util.zip.Deflater in project jdk8u_jdk by JetBrains.

the class ZipFileSystem method close.

@Override
public void close() throws IOException {
    beginWrite();
    try {
        if (!isOpen)
            return;
        // set closed
        isOpen = false;
    } finally {
        endWrite();
    }
    if (!streams.isEmpty()) {
        // unlock and close all remaining streams
        Set<InputStream> copy = new HashSet<>(streams);
        for (InputStream is : copy) is.close();
    }
    // lock and sync
    beginWrite();
    try {
        sync();
        // close the ch just in case no update
        ch.close();
    } finally {
        // and sync dose not close the ch
        endWrite();
    }
    synchronized (inflaters) {
        for (Inflater inf : inflaters) inf.end();
    }
    synchronized (deflaters) {
        for (Deflater def : deflaters) def.end();
    }
    IOException ioe = null;
    synchronized (tmppaths) {
        for (Path p : tmppaths) {
            try {
                Files.deleteIfExists(p);
            } catch (IOException x) {
                if (ioe == null)
                    ioe = x;
                else
                    ioe.addSuppressed(x);
            }
        }
    }
    provider.removeFileSystem(zfpath, this);
    if (ioe != null)
        throw ioe;
}
Also used : Deflater(java.util.zip.Deflater) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException)

Example 70 with Deflater

use of java.util.zip.Deflater in project oxCore by GluuFederation.

the class CompressionHelper method deflate.

public static byte[] deflate(byte[] data, boolean nowrap) throws IOException {
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, nowrap);
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            os.write(buffer, 0, count);
        }
    } finally {
        IOUtils.closeQuietly(os);
    }
    return os.toByteArray();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Deflater (java.util.zip.Deflater)85 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)25 IOException (java.io.IOException)18 Test (org.junit.Test)13 Inflater (java.util.zip.Inflater)9 OutputStream (java.io.OutputStream)7 InflaterInputStream (java.util.zip.InflaterInputStream)7 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 Field (java.lang.reflect.Field)3 CRC32 (java.util.zip.CRC32)3 BufferedOutputStream (java.io.BufferedOutputStream)2 CharArrayReader (java.io.CharArrayReader)2 DataOutputStream (java.io.DataOutputStream)2 FileInputStream (java.io.FileInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Reader (java.io.Reader)2