use of java.util.zip.DeflaterOutputStream in project OpenAM by OpenRock.
the class SAML2Utils method encodeForRedirect.
/**
* Returns the encoded request message.
* The SAML Request message must be
* encoded before being transmitted.
* The Request message is encoded as follows:
* 1. URL Encoded using the DEFLATE compression method.
* 2. Then the message is base-64 encoded according to
* the rules specified in RFC2045.
*
* @param str String to be encoded.
* @return String the encoded String value or null on error.
*/
public static String encodeForRedirect(final String str) {
String classMethod = "SAML2Utils.encodeForRedirect: ";
byte[] input;
try {
input = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
debug.error(classMethod + "cannot get byte array: ", uee);
return null;
}
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out, deflater);
try {
deflaterOutputStream.write(input);
} catch (IOException e) {
debug.error(classMethod + "There was a problem compressing the input", e);
return null;
} finally {
IOUtils.closeIfNotNull(deflaterOutputStream);
}
String encoded = URLEncDec.encode(Base64.encode(out.toByteArray()));
if (debug.messageEnabled()) {
debug.message(classMethod + "out string length : " + encoded.length());
debug.message(classMethod + "out string is ===>" + encoded + "<===");
}
return encoded;
}
use of java.util.zip.DeflaterOutputStream in project android_frameworks_base by DirtyUnicorns.
the class BlobBackupHelper method deflate.
// Also versions the deflated blob internally in case we need to revise it
private byte[] deflate(byte[] data) {
byte[] result = null;
if (data != null) {
try {
ByteArrayOutputStream sink = new ByteArrayOutputStream();
DataOutputStream headerOut = new DataOutputStream(sink);
// write the header directly to the sink ahead of the deflated payload
headerOut.writeInt(mCurrentBlobVersion);
DeflaterOutputStream out = new DeflaterOutputStream(sink);
out.write(data);
// finishes and commits the compression run
out.close();
result = sink.toByteArray();
if (DEBUG) {
Log.v(TAG, "Deflated " + data.length + " bytes to " + result.length);
}
} catch (IOException e) {
Log.w(TAG, "Unable to process payload: " + e.getMessage());
}
}
return result;
}
use of java.util.zip.DeflaterOutputStream in project camel by apache.
the class ZipDataFormat method marshal.
public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
// ask for a mandatory type conversion to avoid a possible NPE beforehand as we do copy from the InputStream
final InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph);
final Deflater deflater = new Deflater(compressionLevel);
final DeflaterOutputStream zipOutput = new DeflaterOutputStream(stream, deflater);
try {
IOHelper.copy(is, zipOutput);
} finally {
IOHelper.close(is, zipOutput);
/*
* As we create the Deflater our self and do not use the stream default
* (see {@link java.util.zip.DeflaterOutputStream#usesDefaultDeflater})
* we need to close the Deflater to not risk a OutOfMemoryException
* in native code parts (see {@link java.util.zip.Deflater#end})
*/
deflater.end();
}
}
use of java.util.zip.DeflaterOutputStream in project teaTime by ancfdy.
the class AppZLibMgr method compress.
/**
* 压缩
*
* @param data
* 待压
*缩数据
* @param os
* 输出流
*/
public static void compress(byte[] data, OutputStream os) {
DeflaterOutputStream dos = new DeflaterOutputStream(os);
try {
dos.write(data, 0, data.length);
dos.finish();
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
use of java.util.zip.DeflaterOutputStream in project voltdb by VoltDB.
the class Encoder method compressAndBase64Encode.
public static String compressAndBase64Encode(String string) {
try {
byte[] inBytes = string.getBytes(Constants.UTF8ENCODING);
ByteArrayOutputStream baos = new ByteArrayOutputStream((int) (string.length() * 0.7));
DeflaterOutputStream dos = new DeflaterOutputStream(baos);
dos.write(inBytes);
dos.close();
byte[] outBytes = baos.toByteArray();
return Base64.encodeToString(outBytes, false);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations