use of java.util.zip.Deflater 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.Deflater in project teaTime by ancfdy.
the class AppZLibMgr method compress.
/**
* 压缩
*
* @param data
*
* @return byte[] 压缩后的数据
*/
public static byte[] compress(byte[] data) {
byte[] output = new byte[0];
Deflater compresser = new Deflater();
compresser.reset();
compresser.setInput(data);
compresser.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!compresser.finished()) {
int i = compresser.deflate(buf);
bos.write(buf, 0, i);
}
output = bos.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
compresser.end();
return output;
}
use of java.util.zip.Deflater in project cloudstack by apache.
the class SAMLUtils method encodeSAMLRequest.
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;
}
use of java.util.zip.Deflater in project jdk8u_jdk by JetBrains.
the class ZipFileSystem method close.
@Override
public void close() throws IOException {
beginWrite();
try {
if (!isOpen)
return;
// set closed
isOpen = false;
} finally {
endWrite();
}
if (!streams.isEmpty()) {
// unlock and close all remaining streams
Set<InputStream> copy = new HashSet<>(streams);
for (InputStream is : copy) is.close();
}
// lock and sync
beginWrite();
try {
sync();
// close the ch just in case no update
ch.close();
} finally {
// and sync dose not close the ch
endWrite();
}
synchronized (inflaters) {
for (Inflater inf : inflaters) inf.end();
}
synchronized (deflaters) {
for (Deflater def : deflaters) def.end();
}
IOException ioe = null;
synchronized (tmppaths) {
for (Path p : tmppaths) {
try {
Files.deleteIfExists(p);
} catch (IOException x) {
if (ioe == null)
ioe = x;
else
ioe.addSuppressed(x);
}
}
}
provider.removeFileSystem(zfpath, this);
if (ioe != null)
throw ioe;
}
use of java.util.zip.Deflater in project oxCore by GluuFederation.
the class CompressionHelper method deflate.
public static byte[] deflate(byte[] data, boolean nowrap) throws IOException {
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, nowrap);
deflater.setInput(data);
deflater.finish();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer);
os.write(buffer, 0, count);
}
} finally {
IOUtils.closeQuietly(os);
}
return os.toByteArray();
}
Aggregations