Search in sources :

Example 71 with GZIPOutputStream

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();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 72 with GZIPOutputStream

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;
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 73 with GZIPOutputStream

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();
}
Also used : DropBoxManager(android.os.DropBoxManager) InputStreamReader(java.io.InputStreamReader) GZIPOutputStream(java.util.zip.GZIPOutputStream) Parcel(android.os.Parcel) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File)

Example 74 with GZIPOutputStream

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();
}
Also used : DropBoxManager(android.os.DropBoxManager) DropBoxManagerService(com.android.server.DropBoxManagerService) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 75 with GZIPOutputStream

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();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)835 ByteArrayOutputStream (java.io.ByteArrayOutputStream)339 IOException (java.io.IOException)254 FileOutputStream (java.io.FileOutputStream)251 OutputStream (java.io.OutputStream)185 File (java.io.File)179 Test (org.junit.Test)93 BufferedOutputStream (java.io.BufferedOutputStream)84 GZIPInputStream (java.util.zip.GZIPInputStream)77 FileInputStream (java.io.FileInputStream)72 InputStream (java.io.InputStream)64 ByteArrayInputStream (java.io.ByteArrayInputStream)60 OutputStreamWriter (java.io.OutputStreamWriter)53 ObjectOutputStream (java.io.ObjectOutputStream)39 DataOutputStream (java.io.DataOutputStream)38 BufferedWriter (java.io.BufferedWriter)30 ByteBuffer (java.nio.ByteBuffer)28 Path (java.nio.file.Path)28 ArrayList (java.util.ArrayList)28 NodeSettings (org.knime.core.node.NodeSettings)28