/** * Applies compression to the specified plain text if requested. * * @param jweHeader The JWE header. Must not be {@code null}. * @param bytes The plain text bytes. Must not be {@code null}. * * @return The bytes to encrypt. * * @throws JOSEException If compression failed or the requested * compression algorithm is not supported. */ public static byte[] applyCompression(final JWEHeader jweHeader, final byte[] bytes) throws JOSEException { CompressionAlgorithm compressionAlg = jweHeader.getCompressionAlgorithm(); if (compressionAlg == null) { return bytes; } else if (compressionAlg.equals(CompressionAlgorithm.DEF)) { try { return DeflateUtils.compress(bytes); } catch (Exception e) { throw new JOSEException("Couldn't compress plain text: " + e.getMessage(), e); } } else { throw new JOSEException("Unsupported compression algorithm: " + compressionAlg); } }
return DeflateUtils.decompress(bytes);