Java Code Examples for java.util.zip.Deflater#DEFLATED

The following examples show how to use java.util.zip.Deflater#DEFLATED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTTPRedirectDeflateEncoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * DEFLATE (RFC1951) compresses the given SAML message.
 * 
 * @param message SAML message
 * 
 * @return DEFLATE compressed message
 * 
 * @throws MessageEncodingException thrown if there is a problem compressing the message
 */
protected String deflateAndBase64Encode(SAMLObject message) throws MessageEncodingException {
    log.debug("Deflating and Base64 encoding SAML message");
    try {
        String messageStr = XMLHelper.nodeToString(marshallMessage(message));

        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
        deflaterStream.write(messageStr.getBytes("UTF-8"));
        deflaterStream.finish();

        return Base64.encodeBytes(bytesOut.toByteArray(), Base64.DONT_BREAK_LINES);
    } catch (IOException e) {
        throw new MessageEncodingException("Unable to DEFLATE and Base64 encode SAML message", e);
    }
}
 
Example 2
Source File: Util.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * @param xmlString String to be encoded
 * @return
 */
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 = Base64Support.encode(byteArrayOutputStream
            .toByteArray(), Base64Support.UNCHUNKED);
    return encodedRequestMessage.trim();

}
 
Example 3
Source File: SAMLUtils.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: XMPPTransmitter.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
private static byte[] deflate(byte[] input) {

    Deflater compressor = new Deflater(Deflater.DEFLATED);
    compressor.setInput(input);
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[CHUNKSIZE];

    while (!compressor.finished()) {
      int count = compressor.deflate(buf);
      bos.write(buf, 0, count);
    }

    return bos.toByteArray();
  }
 
Example 5
Source File: DeflateRFC1951CompressionAlgorithm.java    From Jose4j with Apache License 2.0 5 votes vote down vote up
public byte[] compress(byte[] data)
{
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater))
    {
        deflaterOutputStream.write(data);
        deflaterOutputStream.finish();
        return byteArrayOutputStream.toByteArray();
    }
    catch (IOException e)
    {
        throw new UncheckedJoseException("Problem compressing data.", e);
    }
}
 
Example 6
Source File: LogoutRequestGenerator.java    From development with Apache License 2.0 5 votes vote down vote up
private ByteArrayOutputStream deflateBytes(ByteArrayOutputStream baos) throws IOException {
    ByteArrayOutputStream deflatedBytes = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(deflatedBytes, deflater);
    deflaterStream.write(baos.toByteArray());
    deflaterStream.finish();
    return deflatedBytes;
}
 
Example 7
Source File: SAMLUtils.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Deflate a message to be send over a preferred binding
 *
 * @param message Message to be deflated
 * @return The deflated message as a byte array
 */
public static byte[] deflateMessage(String message) throws SAMLException {
    try {
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                DeflaterOutputStream deflaterOutputStream =
                        new DeflaterOutputStream(byteArrayOutputStream, deflater)) {
            deflaterOutputStream.write(message.getBytes("UTF-8"));
            deflaterOutputStream.finish();
            return byteArrayOutputStream.toByteArray();
        }
    } catch (Exception e) {
        throw new SAMLException("Message Deflation failed", e);
    }
}
 
Example 8
Source File: SAMLSSOUtil.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Compresses the response String
 *
 * @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();
    }
}
 
Example 9
Source File: RESTSecurityTokenServiceImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static int getDeflateLevel() {
    Integer level = null;

    Message m = PhaseInterceptorChain.getCurrentMessage();
    if (m != null) {
        level = PropertyUtils.getInteger(m, "deflate.level");
    }
    if (level == null) {
        level = Deflater.DEFLATED;
    }
    return level;
}
 
Example 10
Source File: DeflateEncoderDecoder.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static int getDeflateLevel() {
    Integer level = null;

    Message m = PhaseInterceptorChain.getCurrentMessage();
    if (m != null) {
        level = PropertyUtils.getInteger(m, "deflate.level");
    }
    if (level == null) {
        level = Deflater.DEFLATED;
    }
    return level;
}
 
Example 11
Source File: CompressionUtils.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
public static byte[] deflate(byte[] tokenBytes, boolean nowrap) {
    Deflater compresser = new Deflater(Deflater.DEFLATED, nowrap);

    compresser.setInput(tokenBytes);
    compresser.finish();

    byte[] output = new byte[tokenBytes.length * 2];

    int compressedDataLength = compresser.deflate(output);

    byte[] result = new byte[compressedDataLength];
    System.arraycopy(output, 0, result, 0, compressedDataLength);
    return result;
}
 
Example 12
Source File: DeflateUtil.java    From keycloak with Apache License 2.0 5 votes vote down vote up
/**
 * Apply DEFLATE encoding
 *
 * @param message
 *
 * @return
 *
 * @throws IOException
 */
public static byte[] encode(byte[] message) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(baos, deflater);
    deflaterStream.write(message);
    deflaterStream.finish();

    return baos.toByteArray();
}
 
Example 13
Source File: SamlHelper.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Converts plain-text xml to SAML-spec compliant string for HTTP-Redirect binding
 *
 * @param xml
 * @return
 * @throws IOException
 */
private String xmlToEncodedString(String xml) throws IOException {
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    deflaterOutputStream.write(xml.getBytes());
    deflaterOutputStream.close();
    String base64 = Base64.encodeBase64String(byteArrayOutputStream.toByteArray());
    return URLEncoder.encode(base64, "UTF-8");
}
 
Example 14
Source File: DeflateEncodingProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public DeflateEncodingProvider() {
    this(Deflater.DEFLATED);
}
 
Example 15
Source File: DeflatingStreamSinkConduit.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public DeflatingStreamSinkConduit(final ConduitFactory<StreamSinkConduit> conduitFactory, final HttpServerExchange exchange) {
    this(conduitFactory, exchange, Deflater.DEFLATED);
}
 
Example 16
Source File: GzipHeader.java    From webarchive-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Read in gzip header.
 * 
 * Advances the stream past the gzip header.
 * @param in InputStream.
 * 
 * @throws IOException Throws if does not start with GZIP Header.
 */
public void readHeader(InputStream in) throws IOException {
    CRC32 crc = new CRC32();
    crc.reset();
    if (!testGzipMagic(in, crc)) {
        throw new NoGzipMagicException();
    }
    this.length += 2;
    if (readByte(in, crc) != Deflater.DEFLATED) {
        throw new IOException("Unknown compression");
    }
    this.length++;
   
    // Get gzip header flag.
    this.flg = readByte(in, crc);
    this.length++;
    
    // Get MTIME.
    this.mtime = readInt(in, crc);
    this.length += 4;
    
    // Read XFL and OS.
    this.xfl = readByte(in, crc);
    this.length++;
    this.os = readByte(in, crc);
    this.length++;
    
    // Skip optional extra field -- stuff w/ alexa stuff in it.
    final int FLG_FEXTRA = 4;
    if ((this.flg & FLG_FEXTRA) == FLG_FEXTRA) {
        int count = readShort(in, crc);
        this.length +=2;
        this.fextra = new byte[count];
        readByte(in, crc, this.fextra, 0, count);
        this.length += count;
    }   
    
    // Skip file name.  It ends in null.
    final int FLG_FNAME  = 8;
    if ((this.flg & FLG_FNAME) == FLG_FNAME) {
        while (readByte(in, crc) != 0) {
            this.length++;
        }
    }   
    
    // Skip file comment.  It ends in null.
    final int FLG_FCOMMENT = 16;   // File comment
    if ((this.flg & FLG_FCOMMENT) == FLG_FCOMMENT) {
        while (readByte(in, crc) != 0) {
            this.length++;
        }
    }
    
    // Check optional CRC.
    final int FLG_FHCRC  = 2;
    if ((this.flg & FLG_FHCRC) == FLG_FHCRC) {
        int calcCrc = (int)(crc.getValue() & 0xffff);
        if (readShort(in, crc) != calcCrc) {
            throw new IOException("Bad header CRC");
        }
        this.length += 2;
    }
}