use of java.util.zip.GZIPOutputStream in project robovm by robovm.
the class URLConnectionTest method gzip.
/**
* Returns a gzipped copy of {@code bytes}.
*/
public byte[] gzip(byte[] bytes) throws IOException {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
OutputStream gzippedOut = new GZIPOutputStream(bytesOut);
gzippedOut.write(bytes);
gzippedOut.close();
return bytesOut.toByteArray();
}
use of java.util.zip.GZIPOutputStream in project jvm-tools by aragozin.
the class HeapDumpProcuder method getCompressedHeapDump.
public static File getCompressedHeapDump() throws IOException {
File file = new File(HEAP_DUMP_GZ_PATH);
if (!file.exists()) {
System.out.println("Generating compressing heap dump: " + HEAP_DUMP_GZ_PATH);
FileInputStream fis = new FileInputStream(getHeapDump());
FileOutputStream fos = new FileOutputStream(file);
GZIPOutputStream gzos = new GZIPOutputStream(fos);
byte[] buf = new byte[64 << 10];
int n = 0;
while (true) {
n = fis.read(buf);
if (n < 0) {
break;
}
gzos.write(buf, 0, n);
}
gzos.close();
fis.close();
fos.close();
}
return file;
}
use of java.util.zip.GZIPOutputStream in project platform_frameworks_base by android.
the class DropBoxTest method testDropBoxEntrySerialization.
public void testDropBoxEntrySerialization() throws Exception {
// Make sure DropBoxManager.Entry can be serialized to a Parcel and back
// under a variety of conditions.
Parcel parcel = Parcel.obtain();
File dir = getEmptyDir("testDropBoxEntrySerialization");
new DropBoxManager.Entry("empty", 1000000).writeToParcel(parcel, 0);
new DropBoxManager.Entry("string", 2000000, "String Value").writeToParcel(parcel, 0);
new DropBoxManager.Entry("bytes", 3000000, "Bytes Value".getBytes(), DropBoxManager.IS_TEXT).writeToParcel(parcel, 0);
new DropBoxManager.Entry("zerobytes", 4000000, new byte[0], 0).writeToParcel(parcel, 0);
new DropBoxManager.Entry("emptybytes", 5000000, (byte[]) null, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
try {
new DropBoxManager.Entry("badbytes", 99999, "Bad Bytes Value".getBytes(), DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
fail("IllegalArgumentException expected for non-null byte[] and IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
try {
new DropBoxManager.Entry("badbytes", 99999, (byte[]) null, 0).writeToParcel(parcel, 0);
fail("IllegalArgumentException expected for null byte[] and non-IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
File f = new File(dir, "file.dat");
FileOutputStream os = new FileOutputStream(f);
os.write("File Value".getBytes());
os.close();
new DropBoxManager.Entry("file", 6000000, f, DropBoxManager.IS_TEXT).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
new DropBoxManager.Entry("binfile", 7000000, f, 0).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
new DropBoxManager.Entry("emptyfile", 8000000, (ParcelFileDescriptor) null, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
try {
new DropBoxManager.Entry("badfile", 99999, new File(dir, "nonexist.dat"), 0);
fail("IOException expected for nonexistent file");
} catch (IOException e) {
// expected
}
try {
new DropBoxManager.Entry("badfile", 99999, f, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
fail("IllegalArgumentException expected for non-null file and IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
try {
new DropBoxManager.Entry("badfile", 99999, (ParcelFileDescriptor) null, 0);
fail("IllegalArgumentException expected for null PFD and non-IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
File gz = new File(dir, "file.gz");
GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(gz));
gzout.write("Gzip File Value".getBytes());
gzout.close();
new DropBoxManager.Entry("gzipfile", 9000000, gz, DropBoxManager.IS_TEXT | DropBoxManager.IS_GZIPPED).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
new DropBoxManager.Entry("gzipbinfile", 10000000, gz, DropBoxManager.IS_GZIPPED).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
//
// Switch from writing to reading
//
parcel.setDataPosition(0);
DropBoxManager.Entry e;
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("empty", e.getTag());
assertEquals(1000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, e.getInputStream());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("string", e.getTag());
assertEquals(2000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("String Value", e.getText(100));
assertEquals("String Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("bytes", e.getTag());
assertEquals(3000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("Bytes Value", e.getText(100));
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("zerobytes", e.getTag());
assertEquals(4000000, e.getTimeMillis());
assertEquals(0, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("emptybytes", e.getTag());
assertEquals(5000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, e.getInputStream());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("file", e.getTag());
assertEquals(6000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("File Value", e.getText(100));
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("binfile", e.getTag());
assertEquals(7000000, e.getTimeMillis());
assertEquals(0, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals("File Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("emptyfile", e.getTag());
assertEquals(8000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, e.getInputStream());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("gzipfile", e.getTag());
assertEquals(9000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("Gzip File Value", e.getText(100));
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("gzipbinfile", e.getTag());
assertEquals(10000000, e.getTimeMillis());
assertEquals(0, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals("Gzip File Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
assertEquals(0, parcel.dataAvail());
parcel.recycle();
}
use of java.util.zip.GZIPOutputStream in project platform_frameworks_base by android.
the class DropBoxTest method testAddEntriesInTheFuture.
public void testAddEntriesInTheFuture() throws Exception {
File dir = getEmptyDir("testAddEntriesInTheFuture");
long before = System.currentTimeMillis();
// Near future: should be allowed to persist
FileWriter w0 = new FileWriter(new File(dir, "DropBoxTest@" + (before + 5000) + ".txt"));
w0.write("FUTURE0");
w0.close();
// Far future: should be collapsed
FileWriter w1 = new FileWriter(new File(dir, "DropBoxTest@" + (before + 100000) + ".txt"));
w1.write("FUTURE1");
w1.close();
// Another far future item, this one gzipped
File f2 = new File(dir, "DropBoxTest@" + (before + 100001) + ".txt.gz");
GZIPOutputStream gz2 = new GZIPOutputStream(new FileOutputStream(f2));
gz2.write("FUTURE2".getBytes());
gz2.close();
// Tombstone in the far future
new FileOutputStream(new File(dir, "DropBoxTest@" + (before + 100002) + ".lost")).close();
DropBoxManagerService service = new DropBoxManagerService(getContext(), dir);
DropBoxManager dropbox = new DropBoxManager(getContext(), service.getServiceStub());
// Until a write, the timestamps are taken at face value
DropBoxManager.Entry e0 = dropbox.getNextEntry(null, before);
DropBoxManager.Entry e1 = dropbox.getNextEntry(null, e0.getTimeMillis());
DropBoxManager.Entry e2 = dropbox.getNextEntry(null, e1.getTimeMillis());
DropBoxManager.Entry e3 = dropbox.getNextEntry(null, e2.getTimeMillis());
assertTrue(null == dropbox.getNextEntry(null, e3.getTimeMillis()));
assertEquals("FUTURE0", e0.getText(80));
assertEquals("FUTURE1", e1.getText(80));
assertEquals("FUTURE2", e2.getText(80));
assertEquals(null, e3.getText(80));
assertEquals(before + 5000, e0.getTimeMillis());
assertEquals(before + 100000, e1.getTimeMillis());
assertEquals(before + 100001, e2.getTimeMillis());
assertEquals(before + 100002, e3.getTimeMillis());
e0.close();
e1.close();
e2.close();
e3.close();
// Write something to force a collapse
dropbox.addText("NotDropBoxTest", "FUTURE");
e0 = dropbox.getNextEntry(null, before);
e1 = dropbox.getNextEntry(null, e0.getTimeMillis());
e2 = dropbox.getNextEntry(null, e1.getTimeMillis());
e3 = dropbox.getNextEntry(null, e2.getTimeMillis());
assertTrue(null == dropbox.getNextEntry("DropBoxTest", e3.getTimeMillis()));
assertEquals("FUTURE0", e0.getText(80));
assertEquals("FUTURE1", e1.getText(80));
assertEquals("FUTURE2", e2.getText(80));
assertEquals(null, e3.getText(80));
assertEquals(before + 5000, e0.getTimeMillis());
assertEquals(before + 5001, e1.getTimeMillis());
assertEquals(before + 5002, e2.getTimeMillis());
assertEquals(before + 5003, e3.getTimeMillis());
e0.close();
e1.close();
e2.close();
e3.close();
}
use of java.util.zip.GZIPOutputStream in project rest.li by linkedin.
the class GzipCompressor method deflate.
@Override
public byte[] deflate(InputStream data) throws CompressionException {
ByteArrayOutputStream out;
GZIPOutputStream gzip = null;
try {
out = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(out);
IOUtils.copy(data, gzip);
} catch (IOException e) {
throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
} finally {
if (gzip != null) {
IOUtils.closeQuietly(gzip);
}
}
return out.toByteArray();
}
Aggregations