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:ch.cern.security.saml2.utils.xml.XMLUtils.java

/**
 * Compress the xml string and encodes it in Base64
 * //from   w w  w.  jav  a  2  s .co  m
 * @param xmlString
 * @param isDebugEnabled 
 * @return xml string encoded
 * @throws IOException
 * @throws UnsupportedEncodingException
 */
public static String xmlDeflateAndEncode(String xmlString, boolean isDebugEnabled)
        throws IOException, UnsupportedEncodingException {

    if (isDebugEnabled)
        nc.notice(xmlString);

    // Deflate the SAMLResponse value
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
    deflaterStream.write(xmlString.getBytes());
    deflaterStream.finish();

    // Encoded the deflatedResponse in base64
    Base64 base64encoder = new Base64();
    String base64response = new String(base64encoder.encode(bytesOut.toByteArray()), "UTF-8");

    if (isDebugEnabled)
        nc.notice(base64response);

    return base64response;
}

From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java

public static String encode(List<SecurityKeyData> devices, String encyrptionKeyName) throws Exception {
    ArrayList<KeyHolder> keys = new ArrayList<KeyHolder>();
    for (SecurityKeyData dr : devices) {
        KeyHolder kh = new KeyHolder();
        kh.setCounter(dr.getCounter());//from  ww w .j  a v  a 2 s.  c  o  m
        kh.setEnrollmentTime(dr.getEnrollmentTime());
        kh.setKeyHandle(dr.getKeyHandle());
        kh.setPublicKey(dr.getPublicKey());
        kh.setTransports(dr.getTransports());
        keys.add(kh);
    }

    String json = gson.toJson(keys);
    EncryptedMessage msg = new EncryptedMessage();

    SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName);
    if (key == null) {
        throw new Exception("Queue message encryption key not found");
    }

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    msg.setMsg(cipher.doFinal(json.getBytes("UTF-8")));
    msg.setIv(cipher.getIV());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DeflaterOutputStream compressor = new DeflaterOutputStream(baos,
            new Deflater(Deflater.BEST_COMPRESSION, true));

    compressor.write(gson.toJson(msg).getBytes("UTF-8"));
    compressor.flush();
    compressor.close();

    String b64 = new String(Base64.encodeBase64(baos.toByteArray()));

    return b64;

}

From source file:gov.nasa.ensemble.common.CommonUtils.java

public static byte[] compress(byte[] input) {
    try {//from  ww  w .  j  a  va  2s . com
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(baos);
        dos.write(input);
        dos.close();
        return baos.toByteArray();
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Encode a SAML2 request for the HTTP-redirect binding. The encoded message is not URL encoded !
 * //from   ww  w .j av a2  s. c  o  m
 * @param request
 *            the request
 * @return the encoded request
 * @throws IOException
 */
public static String httpRedirectEncode(final String samlMessage) throws IOException {
    String deflatedRequest = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    DeflaterOutputStream deflaterOutputStream = null;

    try {
        final Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        byteArrayOutputStream = new ByteArrayOutputStream();
        deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);

        // Deflated then Base 64 encoded then Url Encoded for HTTP REDIRECT Binding
        deflaterOutputStream.write(samlMessage.getBytes());
        deflaterOutputStream.finish();
        deflater.finish();

        deflatedRequest = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);

        if (SamlHelper.LOGGER.isDebugEnabled()) {
            SamlHelper.LOGGER.debug(String.format("SAML 2.0 Request: %s", samlMessage));
            SamlHelper.LOGGER.debug(String.format("Encoded HTTP-Redirect Request: %s", deflatedRequest));
        }
    } finally {
        if (byteArrayOutputStream != null) {
            byteArrayOutputStream.close();
        }
        if (deflaterOutputStream != null) {
            deflaterOutputStream.close();
        }
    }

    return deflatedRequest;
}

From source file:com.uber.hoodie.common.HoodieJsonPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
    try {//  w  ww. ja  v  a 2  s.com
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
        // Its important to call this.
        // Deflater takes off-heap native memory and does not release until GC kicks in
        deflater.end();
    }
    return baos.toByteArray();
}

From source file:com.uber.hoodie.common.TestRawTripPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true);
    try {//from  w  w w .  ja va  2  s  .c o m
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
    }
    return baos.toByteArray();
}

From source file:com.alibaba.citrus.service.requestcontext.session.valueencoder.AbstractSessionValueEncoder.java

private byte[] compress(byte[] data) throws SessionValueEncoderException {
    if (!doCompress()) {
        return data;
    }//from w ww . j a v a 2s.  c o m

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater def = new Deflater(Deflater.BEST_COMPRESSION, false);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, def);

    try {
        dos.write(data);
    } catch (Exception e) {
        throw new SessionValueEncoderException(e);
    } finally {
        try {
            dos.close();
        } catch (IOException e) {
        }

        def.end();
    }

    return baos.toByteArray();
}

From source file:com.cloud.agent.api.SecurityIngressRulesCmd.java

public String compressStringifiedRules() {
    StringBuilder ruleBuilder = new StringBuilder();
    for (SecurityIngressRulesCmd.IpPortAndProto ipPandP : getRuleSet()) {
        ruleBuilder.append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":")
                .append(ipPandP.getEndPort()).append(":");
        for (String cidr : ipPandP.getAllowedCidrs()) {
            ruleBuilder.append(cidr).append(",");
        }//ww w . ja v  a  2s .  c  o m
        ruleBuilder.append("NEXT");
        ruleBuilder.append(" ");
    }
    String stringified = ruleBuilder.toString();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        //Note : not using GZipOutputStream since that is for files
        //GZipOutputStream gives a different header, although the compression is the same
        DeflaterOutputStream dzip = new DeflaterOutputStream(out);
        dzip.write(stringified.getBytes());
        dzip.close();
    } catch (IOException e) {
        s_logger.warn("Exception while compressing ingress rules");
        return null;
    }
    return Base64.encodeBase64String(out.toByteArray());
}

From source file:com.cloud.agent.api.SecurityGroupRulesCmd.java

public String compressStringifiedRules() {
    StringBuilder ruleBuilder = new StringBuilder();
    for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getIngressRuleSet()) {
        ruleBuilder.append("I:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort())
                .append(":").append(ipPandP.getEndPort()).append(":");
        for (String cidr : ipPandP.getAllowedCidrs()) {
            ruleBuilder.append(cidr).append(",");
        }/*from   www  . jav  a 2s .  c om*/
        ruleBuilder.append("NEXT");
        ruleBuilder.append(" ");
    }
    for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getEgressRuleSet()) {
        ruleBuilder.append("E:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort())
                .append(":").append(ipPandP.getEndPort()).append(":");
        for (String cidr : ipPandP.getAllowedCidrs()) {
            ruleBuilder.append(cidr).append(",");
        }
        ruleBuilder.append("NEXT");
        ruleBuilder.append(" ");
    }
    String stringified = ruleBuilder.toString();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        //Note : not using GZipOutputStream since that is for files
        //GZipOutputStream gives a different header, although the compression is the same
        DeflaterOutputStream dzip = new DeflaterOutputStream(out);
        dzip.write(stringified.getBytes());
        dzip.close();
    } catch (IOException e) {
        s_logger.warn("Exception while compressing security group rules");
        return null;
    }
    return Base64.encodeBase64String(out.toByteArray());
}

From source file:de.tntinteractive.portalsammler.engine.SecureStore.java

private byte[] compress(final byte[] content) throws IOException {
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final DeflaterOutputStream deflate = new DeflaterOutputStream(buffer);
    deflate.write(content);
    deflate.close();// ww w  .j  av  a 2  s . c o  m
    return buffer.toByteArray();
}