Search in sources :

Example 56 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project OpenAM by OpenRock.

the class SAML2Utils method encodeForRedirect.

/**
     * Returns the encoded request message.
     * The SAML Request message must be
     * encoded before being transmitted.
     * The Request message is encoded as follows:
     * 1. URL Encoded using the DEFLATE compression method.
     * 2. Then the message is base-64 encoded according to
     * the rules specified in RFC2045.
     *
     * @param str String to be encoded.
     * @return String the encoded String value or null on error.
     */
public static String encodeForRedirect(final String str) {
    String classMethod = "SAML2Utils.encodeForRedirect: ";
    byte[] input;
    try {
        input = str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException uee) {
        debug.error(classMethod + "cannot get byte array: ", uee);
        return null;
    }
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out, deflater);
    try {
        deflaterOutputStream.write(input);
    } catch (IOException e) {
        debug.error(classMethod + "There was a problem compressing the input", e);
        return null;
    } finally {
        IOUtils.closeIfNotNull(deflaterOutputStream);
    }
    String encoded = URLEncDec.encode(Base64.encode(out.toByteArray()));
    if (debug.messageEnabled()) {
        debug.message(classMethod + "out string length : " + encoded.length());
        debug.message(classMethod + "out string is ===>" + encoded + "<===");
    }
    return encoded;
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 57 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project android_frameworks_base by DirtyUnicorns.

the class BlobBackupHelper method deflate.

// Also versions the deflated blob internally in case we need to revise it
private byte[] deflate(byte[] data) {
    byte[] result = null;
    if (data != null) {
        try {
            ByteArrayOutputStream sink = new ByteArrayOutputStream();
            DataOutputStream headerOut = new DataOutputStream(sink);
            // write the header directly to the sink ahead of the deflated payload
            headerOut.writeInt(mCurrentBlobVersion);
            DeflaterOutputStream out = new DeflaterOutputStream(sink);
            out.write(data);
            // finishes and commits the compression run
            out.close();
            result = sink.toByteArray();
            if (DEBUG) {
                Log.v(TAG, "Deflated " + data.length + " bytes to " + result.length);
            }
        } catch (IOException e) {
            Log.w(TAG, "Unable to process payload: " + e.getMessage());
        }
    }
    return result;
}
Also used : DataOutputStream(java.io.DataOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 58 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream 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 59 with DeflaterOutputStream

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

the class AppZLibMgr method compress.

/**
     * 压缩
     *
     * @param data
     *            待压
     *缩数据
     * @param os
     *            输出流
     */
public static void compress(byte[] data, OutputStream os) {
    DeflaterOutputStream dos = new DeflaterOutputStream(os);
    try {
        dos.write(data, 0, data.length);
        dos.finish();
        dos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) IOException(java.io.IOException)

Example 60 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project voltdb by VoltDB.

the class Encoder method compressAndBase64Encode.

public static String compressAndBase64Encode(String string) {
    try {
        byte[] inBytes = string.getBytes(Constants.UTF8ENCODING);
        ByteArrayOutputStream baos = new ByteArrayOutputStream((int) (string.length() * 0.7));
        DeflaterOutputStream dos = new DeflaterOutputStream(baos);
        dos.write(inBytes);
        dos.close();
        byte[] outBytes = baos.toByteArray();
        return Base64.encodeToString(outBytes, false);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DeflaterOutputStream (java.util.zip.DeflaterOutputStream)76 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 Deflater (java.util.zip.Deflater)25 IOException (java.io.IOException)24 OutputStream (java.io.OutputStream)13 InflaterInputStream (java.util.zip.InflaterInputStream)9 DataOutputStream (java.io.DataOutputStream)8 GZIPOutputStream (java.util.zip.GZIPOutputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BufferedOutputStream (java.io.BufferedOutputStream)3 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 ImageException (cbit.image.ImageException)2 DeflateCompressor (com.linkedin.r2.filter.compression.streaming.DeflateCompressor)2 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 PipedInputStream (java.io.PipedInputStream)2