use of java.util.zip.DeflaterOutputStream in project voltdb by VoltDB.
the class FileArchiver method archive.
/*
public static void compressFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.archive(infilename, outfilename, storage, COMPRESSION_ZIP);
}
public static void decompressFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.unarchive(
infilename, outfilename, storage, COMPRESSION_ZIP);
}
public static void copyFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.archive(
infilename, outfilename, storage, COMPRESSION_NONE);
}
public static void restoreFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.unarchive(
infilename, outfilename, storage, COMPRESSION_NONE);
}
*/
public static void archive(String infilename, String outfilename, FileAccess storage, int compressionType) throws IOException {
InputStream in = null;
OutputStream f = null;
DeflaterOutputStream deflater = null;
boolean completed = false;
// if there is no file
if (!storage.isStreamElement(infilename)) {
return;
}
try {
byte[] b = new byte[COPY_BLOCK_SIZE];
in = storage.openInputStreamElement(infilename);
f = storage.openOutputStreamElement(outfilename);
switch(compressionType) {
case COMPRESSION_ZIP:
f = deflater = new DeflaterOutputStream(f, new Deflater(Deflater.BEST_SPEED), b.length);
break;
case COMPRESSION_GZIP:
f = deflater = new GZIPOutputStream(f, b.length);
break;
case COMPRESSION_NONE:
break;
default:
throw new RuntimeException("FileArchiver" + compressionType);
}
while (true) {
int l = in.read(b, 0, b.length);
if (l == -1) {
break;
}
f.write(b, 0, l);
}
completed = true;
} catch (Throwable e) {
throw FileUtil.toIOException(e);
} finally {
try {
if (in != null) {
in.close();
}
if (f != null) {
if (deflater != null) {
deflater.finish();
}
f.close();
}
if (!completed && storage.isStreamElement(outfilename)) {
storage.removeElement(outfilename);
}
} catch (Throwable e) {
throw FileUtil.toIOException(e);
}
}
}
use of java.util.zip.DeflaterOutputStream in project android_frameworks_base by ResurrectionRemix.
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 CloudStack-archive by CloudStack-extras.
the class SecurityGroupRulesCmd method compressStringifiedRules.
/*
* Compress the security group rules using zlib compression to allow the call to the hypervisor
* to scale beyond 8k cidrs.
*/
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(",");
}
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());
}
use of java.util.zip.DeflaterOutputStream in project graylog2-server by Graylog2.
the class TestHelper method zlibCompress.
public static byte[] zlibCompress(String what, int level) throws IOException {
final ByteArrayInputStream compressMe = new ByteArrayInputStream(what.getBytes(StandardCharsets.UTF_8));
final ByteArrayOutputStream compressedMessage = new ByteArrayOutputStream();
try (DeflaterOutputStream out = new DeflaterOutputStream(compressedMessage, new Deflater(level))) {
ByteStreams.copy(compressMe, out);
}
return compressedMessage.toByteArray();
}
use of java.util.zip.DeflaterOutputStream in project jdk8u_jdk by JetBrains.
the class MyInputStream method main.
public static void main(String[] args) throws Exception {
/* initialise stuff */
File fn = new File("x.ReadBounds");
FileOutputStream fout = new FileOutputStream(fn);
for (int i = 0; i < 32; i++) {
fout.write(i);
}
fout.close();
byte[] b = new byte[64];
for (int i = 0; i < 64; i++) {
b[i] = 1;
}
/* test all input streams */
FileInputStream fis = new FileInputStream(fn);
doTest(fis);
doTest1(fis);
fis.close();
BufferedInputStream bis = new BufferedInputStream(new MyInputStream(1024));
doTest(bis);
doTest1(bis);
bis.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
doTest(bais);
doTest1(bais);
bais.close();
FileOutputStream fos = new FileOutputStream(fn);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(12345);
oos.writeObject("Today");
oos.writeObject(new Integer(32));
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fn));
doTest(ois);
doTest1(ois);
ois.close();
DataInputStream dis = new DataInputStream(new MyInputStream(1024));
doTest(dis);
doTest1(dis);
dis.close();
LineNumberInputStream lis = new LineNumberInputStream(new MyInputStream(1024));
doTest(lis);
doTest1(lis);
lis.close();
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
pos.connect(pis);
pos.write(b, 0, 64);
doTest(pis);
doTest1(pis);
pis.close();
PushbackInputStream pbis = new PushbackInputStream(new MyInputStream(1024));
doTest(pbis);
doTest1(pbis);
pbis.close();
StringBufferInputStream sbis = new StringBufferInputStream(new String(b));
doTest(sbis);
doTest1(sbis);
sbis.close();
SequenceInputStream sis = new SequenceInputStream(new MyInputStream(1024), new MyInputStream(1024));
doTest(sis);
doTest1(sis);
sis.close();
ZipInputStream zis = new ZipInputStream(new FileInputStream(fn));
doTest(zis);
doTest1(zis);
zis.close();
byte[] data = new byte[256];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(bos);
dos.write(data, 0, data.length);
dos.close();
InflaterInputStream ifs = new InflaterInputStream(new ByteArrayInputStream(bos.toByteArray()));
doTest(ifs);
doTest1(ifs);
ifs.close();
/* cleanup */
fn.delete();
}
Aggregations