Example usage for java.util.zip Deflater Deflater

List of usage examples for java.util.zip Deflater Deflater

Introduction

In this page you can find the example usage for java.util.zip Deflater Deflater.

Prototype

public Deflater(int level, boolean nowrap) 

Source Link

Document

Creates a new compressor using the specified compression level.

Usage

From source file:jfs.sync.encryption.JFSEncryptedStream.java

private void internalClose() throws IOException {
    delegate.close();/*from  ww  w.j a va  2 s  . co  m*/
    byte[] bytes = delegate.toByteArray();
    final byte[] originalBytes = bytes;
    long l = bytes.length;

    byte marker = COMPRESSION_NONE;

    if (log.isDebugEnabled()) {
        log.debug("close() checking for compressions for");
    } // if

    CompressionThread dt = new CompressionThread(originalBytes) {

        @Override
        public void run() {
            try {
                ByteArrayOutputStream deflaterStream = new ByteArrayOutputStream();
                Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
                OutputStream dos = new DeflaterOutputStream(deflaterStream, deflater, COMPRESSION_BUFFER_SIZE);
                dos.write(originalBytes);
                dos.close();
                compressedValue = deflaterStream.toByteArray();
            } catch (Exception e) {
                log.error("run()", e);
            } // try/catch
        } // run()

    };

    CompressionThread bt = new CompressionThread(originalBytes) {

        @Override
        public void run() {
            try {
                if (originalBytes.length > BZIP_MAX_LENGTH) {
                    compressedValue = originalBytes;
                } else {
                    ByteArrayOutputStream bzipStream = new ByteArrayOutputStream();
                    OutputStream bos = new BZip2CompressorOutputStream(bzipStream);
                    bos.write(originalBytes);
                    bos.close();
                    compressedValue = bzipStream.toByteArray();
                } // if
            } catch (Exception e) {
                log.error("run()", e);
            } // try/catch
        } // run()

    };

    CompressionThread lt = new CompressionThread(originalBytes) {

        /*
         * // "  -a{N}:  set compression mode - [0, 1], default: 1 (max)\n" +
         * "  -d{N}:  set dictionary - [0,28], default: 23 (8MB)\n"
         * +"  -fb{N}: set number of fast bytes - [5, 273], default: 128\n"
         * +"  -lc{N}: set number of literal context bits - [0, 8], default: 3\n"
         * +"  -lp{N}: set number of literal pos bits - [0, 4], default: 0\n"
         * +"  -pb{N}: set number of pos bits - [0, 4], default: 2\n"
         * +"  -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n"+"  -eos:   write End Of Stream marker\n");
         */
        private int dictionarySize = 1 << 23;

        private int lc = 3;

        private int lp = 0;

        private int pb = 2;

        private int fb = 128;

        public int algorithm = 2;

        public int matchFinderIndex = 1; // 0, 1, 2

        @Override
        public void run() {
            try {
                Encoder encoder = new Encoder();
                encoder.SetEndMarkerMode(false);
                encoder.SetAlgorithm(algorithm); // Whatever that means
                encoder.SetDictionarySize(dictionarySize);
                encoder.SetNumFastBytes(fb);
                encoder.SetMatchFinder(matchFinderIndex);
                encoder.SetLcLpPb(lc, lp, pb);

                ByteArrayOutputStream lzmaStream = new ByteArrayOutputStream();
                ByteArrayInputStream inStream = new ByteArrayInputStream(originalBytes);

                encoder.WriteCoderProperties(lzmaStream);
                encoder.Code(inStream, lzmaStream, -1, -1, null);
                compressedValue = lzmaStream.toByteArray();
            } catch (Exception e) {
                log.error("run()", e);
            } // try/catch
        } // run()

    };

    dt.start();
    bt.start();
    lt.start();

    try {
        dt.join();
        bt.join();
        lt.join();
    } catch (InterruptedException e) {
        log.error("run()", e);
    } // try/catch

    if (dt.compressedValue.length < l) {
        marker = COMPRESSION_DEFLATE;
        bytes = dt.compressedValue;
        l = bytes.length;
    } // if

    if (lt.compressedValue.length < l) {
        marker = COMPRESSION_LZMA;
        bytes = lt.compressedValue;
        l = bytes.length;
    } // if

    if (bt.compressedValue.length < l) {
        marker = COMPRESSION_BZIP2;
        bytes = bt.compressedValue;
        if (log.isWarnEnabled()) {
            log.warn("close() using bzip2 and saving " + (l - bytes.length) + " bytes.");
        } // if
        l = bytes.length;
    } // if

    if (log.isInfoEnabled()) {
        if (marker == COMPRESSION_NONE) {
            if (log.isInfoEnabled()) {
                log.info("close() using no compression");
            } // if
        } // if
        if (marker == COMPRESSION_LZMA) {
            if (log.isInfoEnabled()) {
                log.info("close() using lzma");
            } // if
        } // if
    } // if

    ObjectOutputStream oos = new ObjectOutputStream(baseOutputStream);
    oos.writeByte(marker);
    oos.writeLong(originalBytes.length);
    oos.flush();
    OutputStream out = baseOutputStream;
    if (cipher != null) {
        out = new CipherOutputStream(out, cipher);
    } // if
    out.write(bytes);
    out.close();
    delegate = null;
    baseOutputStream = null;
}

From source file:prz.PRZ.java

/**
 *
 * @param bytes/*from   w  ww. j  ava2s.  c  o m*/
 * @return
 */
public static byte[] deflate_test(byte[] bytes) {
    Deflater d = new Deflater(9, true);
    DeflaterOutputStream dfos;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        dfos = new DeflaterOutputStream(baos, d);
        dfos.write(bytes);
        dfos.finish();
        byte[] y = baos.toByteArray();
        System.out.println("LZHF-> BitLength:" + (bytes.length * 8) + "| BestLength: " + (y.length * 8)
                + "| Ratio: " + ((double) y.length / (bytes.length)));
        return y;
    } catch (IOException ex) {
        Logger.getLogger(PRZ.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:gridool.db.partitioning.phihash.csv.distmm.InMemoryIndexHelper.java

private static OutputStream prepareFkOutputStream(final String fkIdxName, final int bucket,
        final Map<String, OutputStream> outputMap) throws IOException {
    String fname = fkIdxName + bucket + ".fk.gz";
    OutputStream out = outputMap.get(fname);
    if (out == null) {
        File file = getFkIndexFile(fname);
        FileOutputStream fos = new FileOutputStream(file, true);
        DeflaterOutputStream zos = new DeflaterOutputStream(fos,
                new Deflater(Deflater.DEFAULT_COMPRESSION, false), COMPRESSOR_BUFSIZE);
        out = new FastBufferedOutputStream(zos, FKCHUNK_IO_BUFSIZE);
        outputMap.put(fname, out);// w  w  w  .  j  a  v a 2s  .c  o m
    }
    return out;
}

From source file:com.vmware.demo.SamlService.java

public String generateSAMLRequest(String assertionConsumerServiceURL, String nameIdFormat) {
    String samlRequest = "";

    try {//  w  w  w .  j  av a 2 s  .c o  m
        // Generate ID
        String randId = "A71AB3E13";

        // Create an issuer Object
        IssuerBuilder issuerBuilder = new IssuerBuilder();
        Issuer issuer = issuerBuilder.buildObject("urn:oasis:names:tc:SAML:2.0:assertion", "Issuer", "samlp");
        issuer.setValue(issuerString);

        // Create NameIDPolicy
        NameIDPolicyBuilder nameIdPolicyBuilder = new NameIDPolicyBuilder();
        NameIDPolicy nameIdPolicy = nameIdPolicyBuilder.buildObject();
        if (StringUtils.isNotEmpty(nameIdFormat)) {
            nameIdPolicy.setFormat(nameIdFormat);
        }
        nameIdPolicy.setSPNameQualifier(issuerString);
        nameIdPolicy.setAllowCreate(true);

        // Create AuthnContextClassRef
        AuthnContextClassRefBuilder authnContextClassRefBuilder = new AuthnContextClassRefBuilder();
        AuthnContextClassRef authnContextClassRef = authnContextClassRefBuilder
                .buildObject("urn:oasis:names:tc:SAML:2.0:assertion", "AuthnContextClassRef", "saml");
        authnContextClassRef
                .setAuthnContextClassRef("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");

        // Create RequestedAuthnContext
        RequestedAuthnContextBuilder requestedAuthnContextBuilder = new RequestedAuthnContextBuilder();
        RequestedAuthnContext requestedAuthnContext = requestedAuthnContextBuilder.buildObject();
        requestedAuthnContext.setComparison(AuthnContextComparisonTypeEnumeration.EXACT);
        requestedAuthnContext.getAuthnContextClassRefs().add(authnContextClassRef);

        AuthnRequestBuilder authRequestBuilder = new AuthnRequestBuilder();
        AuthnRequest authRequest = authRequestBuilder.buildObject("urn:oasis:names:tc:SAML:2.0:protocol",
                "AuthnRequest", "samlp");
        authRequest.setForceAuthn(false);
        authRequest.setIsPassive(false);
        authRequest.setIssueInstant(new DateTime());
        authRequest.setProtocolBinding("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
        authRequest.setAssertionConsumerServiceURL(assertionConsumerServiceURL);
        authRequest.setIssuer(issuer);
        authRequest.setNameIDPolicy(nameIdPolicy);
        authRequest.setRequestedAuthnContext(requestedAuthnContext);
        authRequest.setID(randId);
        authRequest.setVersion(SAMLVersion.VERSION_20);
        Marshaller marshaller = org.opensaml.Configuration.getMarshallerFactory().getMarshaller(authRequest);
        org.w3c.dom.Element authDOM = marshaller.marshall(authRequest);
        StringWriter rspWrt = new StringWriter();
        XMLHelper.writeNode(authDOM, rspWrt);
        String messageXML = rspWrt.toString();
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
        deflaterOutputStream.write(messageXML.getBytes());
        deflaterOutputStream.close();
        samlRequest = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
        //samlRequest = URLEncoder.encode(samlRequest);

        logger.info("samlRequest: " + samlRequest);

    } catch (MarshallingException e) {
        logger.error("General Error", e);
    } catch (IOException e) {
        logger.error("General Error", e);
    }
    return samlRequest;
}

From source file:org.wso2.carbon.apim.sso.Util.java

/**
 * @param xmlString String to be encoded
 * @return/*from   ww w  . j a va2 s .c o  m*/
 */
public static String deflateAndEncode(String xmlString) throws Exception {
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);

    deflaterOutputStream.write(xmlString.getBytes());
    deflaterOutputStream.close();

    // Encoding the compressed message
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);
    return encodedRequestMessage.trim();

}

From source file:org.socraticgrid.workbench.security.wso2.Saml2Util.java

/**
 * Compressing and Encoding the response
 *
 * @param xmlString String to be encoded
 * @return compressed and encoded String
 *///  w ww  .  j av a 2  s  .  com
public static String encode(String xmlString) throws Exception {
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);

    deflaterOutputStream.write(xmlString.getBytes());
    deflaterOutputStream.close();

    // Encoding the compressed message
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);
    return encodedRequestMessage.trim();
}

From source file:org.wso2.carbon.identity.sso.saml.SAMLTestRequestBuilder.java

public static String encodeRequestMessage(RequestAbstractType requestMessage)
        throws MarshallingException, IOException, ConfigurationException {
    DefaultBootstrap.bootstrap();/* www .  ja v a 2 s .  com*/
    System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
            "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;
    authDOM = marshaller.marshall(requestMessage);

    /* Compress the message */
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    StringWriter rspWrt = new StringWriter();
    XMLHelper.writeNode(authDOM, rspWrt);
    deflaterOutputStream.write(rspWrt.toString().getBytes());
    deflaterOutputStream.close();

    /* Encoding the compressed message */
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);

    byteArrayOutputStream.write(byteArrayOutputStream.toByteArray());
    byteArrayOutputStream.toString();

    return encodedRequestMessage;
}

From source file:ddf.security.samlp.SimpleSignTest.java

/**
 * Deflates a value and Base64 encodes the result. This code is copied from RestSecurity because it would cause a circular dependency to use it directly..
 *
 * @param value value to deflate and Base64 encode
 * @return String// w  w  w .j  a va  2 s  . c om
 * @throws IOException if the value cannot be converted
 */
public static String deflateAndBase64Encode(String value) throws IOException {
    ByteArrayOutputStream valueBytes = new ByteArrayOutputStream();
    try (OutputStream tokenStream = new DeflaterOutputStream(valueBytes,
            new Deflater(Deflater.DEFLATED, true))) {
        tokenStream.write(value.getBytes(StandardCharsets.UTF_8));
        tokenStream.close();

        return new String(Base64.encodeBase64(valueBytes.toByteArray()));
    }
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

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());
    deflaterOutputStream.close();//ww  w.  j a  v a  2 s  .  c o  m
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);
    encodedRequestMessage = URLEncoder.encode(encodedRequestMessage, HttpUtils.UTF_8).trim();
    return encodedRequestMessage;
}

From source file:org.phpmaven.phar.PharJavaPackager.java

private void packFile(final ByteArrayOutputStream fileEntriesBaos,
        final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath)
        throws IOException {
    if (DEBUG) {/*from  w w  w .ja v a 2  s .c om*/
        System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes.");
    }

    final byte[] fileBytes = filePath.getBytes("UTF-8");
    writeIntLE(fileEntriesBaos, fileBytes.length);
    fileEntriesBaos.write(fileBytes);
    // TODO Complain with files larger than 4 bytes file length
    writeIntLE(fileEntriesBaos, (int) fileToPack.length());
    writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000));

    final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack);
    if (DEBUG) {
        System.out.println("read " + uncompressed.length + " bytes from file.");
    }
    final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
    //        final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream);
    //        gzipStream.write(uncompressed);
    //        gzipStream.flush();
    final CRC32 checksum = new CRC32();
    checksum.update(uncompressed);
    final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(uncompressed);
    deflater.finish();
    final byte[] buf = new byte[Short.MAX_VALUE];
    while (!deflater.needsInput()) {
        final int bytesRead = deflater.deflate(buf);
        compressedStream.write(buf, 0, bytesRead);
    }

    final byte[] compressed = compressedStream.toByteArray();
    if (DEBUG) {
        System.out.println("compressed to " + compressed.length + " bytes.");
    }

    //        final Inflater decompresser = new Inflater();
    //        decompresser.setInput(compressed);
    //        byte[] result = new byte[5000];
    //        try {
    //            int resultLength = decompresser.inflate(result);
    //            final String str = new String(result, 0, resultLength);
    //            int i = 42;
    //        } catch (DataFormatException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        decompresser.end();

    compressedFilesBaos.write(compressed);
    writeIntLE(fileEntriesBaos, compressed.length);

    writeIntLE(fileEntriesBaos, checksum.getValue());

    // bits: 0x00001000, gzip
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0x10);
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0);

    // 0 bytes manifest
    writeIntLE(fileEntriesBaos, 0);
}