Example usage for java.util.zip DeflaterOutputStream write

List of usage examples for java.util.zip DeflaterOutputStream write

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:org.wso2.carbon.identity.saml.inbound.util.SAMLSSOUtil.java

/**
 * Compresses the response String/*w w  w  .j  a va  2s.  c  o m*/
 *
 * @param response
 * @return
 * @throws IOException
 */
public static String compressResponse(String response) throws IOException {

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    try {
        deflaterOutputStream.write(response.getBytes(StandardCharsets.UTF_8));
    } finally {
        deflaterOutputStream.close();
    }
    return Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
}

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

protected String encodeRequestMessage(RequestAbstractType requestMessage, String binding)
        throws SSOAgentException {

    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;//  w ww  .j a v  a  2 s  .  c o m
    try {
        authDOM = marshaller.marshall(requestMessage);
        StringWriter rspWrt = new StringWriter();
        XMLHelper.writeNode(authDOM, rspWrt);
        if (SAMLConstants.SAML2_REDIRECT_BINDING_URI.equals(binding)) {
            //Compress the message, Base 64 encode and URL encode
            Deflater deflater = new Deflater(Deflater.DEFLATED, true);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream,
                    deflater);
            deflaterOutputStream.write(rspWrt.toString().getBytes(Charset.forName("UTF-8")));
            deflaterOutputStream.close();
            String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
                    Base64.DONT_BREAK_LINES);
            return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();
        } else if (SAMLConstants.SAML2_POST_BINDING_URI.equals(binding)) {
            return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
        } else {
            LOGGER.log(Level.FINE,
                    "Unsupported SAML2 HTTP Binding. Defaulting to " + SAMLConstants.SAML2_POST_BINDING_URI);
            return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
        }
    } catch (MarshallingException e) {
        throw new SSOAgentException("Error occurred while encoding SAML2 request", e);
    } catch (UnsupportedEncodingException e) {
        throw new SSOAgentException("Error occurred while encoding SAML2 request", e);
    } catch (IOException e) {
        throw new SSOAgentException("Error occurred while encoding SAML2 request", e);
    }
}

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

public static String encodeRequestMessage(RequestAbstractType requestMessage)
        throws MarshallingException, IOException, ConfigurationException {
    DefaultBootstrap.bootstrap();//  w  w  w .j  a v  a  2 s . c o m
    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:org.wso2.carbon.identity.sso.saml.tomcat.agent.SSOManager.java

private String encodeRequestMessage(RequestAbstractType requestMessage)
        throws MarshallingException, IOException {

    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = marshaller.marshall(requestMessage);

    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();/*  ww w  . ja  v a 2 s  . c  o m*/

    /* Encoding the compressed message */
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
            Base64.DONT_BREAK_LINES);
    return URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();
}

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

/**
 * Compresses the response String//from   w  w  w .  ja  v  a 2 s .  c  o  m
 *
 * @param response
 * @return
 * @throws IOException
 */
public static String compressResponse(String response) throws IOException {

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    try {
        deflaterOutputStream.write(response.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
    } finally {
        deflaterOutputStream.close();
    }
}

From source file:org.wso2.identity.scenarios.commons.SAML2SSOTestBase.java

/**
 * Base64 encoding of the SAML request.//  w ww .j av a  2  s  .  c  o  m
 *
 * @param requestMessage SAML authentication request.
 * @param binding SAML binding type.
 * @return Base64 encoded SAML request.
 * @throws Exception
 */
protected String encodeRequestMessage(SignableSAMLObject requestMessage, String binding) throws Exception {

    doBootstrap();
    System.setProperty(XML_DOCUMENT_BUILDER_FACTORY, XML_DOCUMENT_BUILDER_FACTORY_IMPL);
    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(requestMessage);
    Element authDOM = null;
    authDOM = marshaller.marshall(requestMessage);
    StringWriter rspWrt = new StringWriter();
    XMLHelper.writeNode(authDOM, rspWrt);

    if (SAMLConstants.SAML2_REDIRECT_BINDING_URI.equals(binding)) {
        //Compress the message, Base 64 encode and URL encode
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
        deflaterOutputStream.write(rspWrt.toString().getBytes(Charset.forName(StandardCharsets.UTF_8.name())));
        deflaterOutputStream.close();
        String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(),
                Base64.DONT_BREAK_LINES);
        return URLEncoder.encode(encodedRequestMessage, StandardCharsets.UTF_8.name()).trim();
    } else if (SAMLConstants.SAML2_POST_BINDING_URI.equals(binding)) {
        return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
    } else {
        log.warn("Unsupported SAML2 HTTP Binding. Defaulting to " + SAMLConstants.SAML2_POST_BINDING_URI);
        return Base64.encodeBytes(rspWrt.toString().getBytes(), Base64.DONT_BREAK_LINES);
    }
}

From source file:prz.PRZ.java

/**
 *
 * @param bytes//  w w  w. ja v  a 2  s  .  c  o m
 * @return
 */
public static byte[] huffman_test(byte[] bytes) {
    Deflater d = new Deflater(9, true);
    d.setStrategy(Deflater.HUFFMAN_ONLY);
    DeflaterOutputStream dfos;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        dfos = new DeflaterOutputStream(baos, d);
        dfos.write(bytes);
        dfos.finish();
        byte[] y = baos.toByteArray();
        System.out.println("HUFF-> 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:prz.PRZ.java

/**
 *
 * @param bytes// ww w  . j av  a 2  s . c om
 * @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:rascal.storage.loose.AbstractLooseStorageNodeChannelIntegrationTest.java

@Before
public void setUpTestData() throws IOException {
    testData = RandomTestDataUtils.createRandomData();
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    DeflaterOutputStream out = new DeflaterOutputStream(outBuffer, new Deflater(DEFLATED_DATA_COMPRESS_LEVER));
    out.write(testData);
    out.close();/* w  w  w .  j a  va  2s. c  o m*/
    deflatedTestData = outBuffer.toByteArray();
}