Search in sources :

Example 51 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project yyl_example by Relucent.

the class GZIP method zip.

public static byte[] zip(byte[] data) throws IOException {
    ByteArrayOutputStream output = null;
    GZIPOutputStream stream = null;
    ByteArrayInputStream input = null;
    try {
        input = new ByteArrayInputStream(data);
        stream = new GZIPOutputStream(output = new ByteArrayOutputStream());
        Streams.copy(input, stream);
        stream.finish();
        return output.toByteArray();
    } finally {
        Streams.closeQuietly(stream);
        Streams.closeQuietly(output);
        Streams.closeQuietly(input);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 52 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project android_frameworks_base by ResurrectionRemix.

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 53 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project android_frameworks_base by ResurrectionRemix.

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 54 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project android_frameworks_base by ResurrectionRemix.

the class DropBoxTest method testAddFile.

public void testAddFile() throws Exception {
    File dir = getEmptyDir("testAddFile");
    long before = System.currentTimeMillis();
    File f0 = new File(dir, "f0.txt");
    File f1 = new File(dir, "f1.txt.gz");
    File f2 = new File(dir, "f2.dat");
    File f3 = new File(dir, "f2.dat.gz");
    FileWriter w0 = new FileWriter(f0);
    GZIPOutputStream gz1 = new GZIPOutputStream(new FileOutputStream(f1));
    FileOutputStream os2 = new FileOutputStream(f2);
    GZIPOutputStream gz3 = new GZIPOutputStream(new FileOutputStream(f3));
    w0.write("FILE0");
    gz1.write("FILE1".getBytes());
    os2.write("DATA2".getBytes());
    gz3.write("DATA3".getBytes());
    w0.close();
    gz1.close();
    os2.close();
    gz3.close();
    DropBoxManagerService service = new DropBoxManagerService(getContext(), dir);
    DropBoxManager dropbox = new DropBoxManager(getContext(), service.getServiceStub());
    dropbox.addFile("DropBoxTest", f0, DropBoxManager.IS_TEXT);
    dropbox.addFile("DropBoxTest", f1, DropBoxManager.IS_TEXT | DropBoxManager.IS_GZIPPED);
    dropbox.addFile("DropBoxTest", f2, 0);
    dropbox.addFile("DropBoxTest", f3, DropBoxManager.IS_GZIPPED);
    DropBoxManager.Entry e0 = dropbox.getNextEntry("DropBoxTest", before);
    DropBoxManager.Entry e1 = dropbox.getNextEntry("DropBoxTest", e0.getTimeMillis());
    DropBoxManager.Entry e2 = dropbox.getNextEntry("DropBoxTest", e1.getTimeMillis());
    DropBoxManager.Entry e3 = dropbox.getNextEntry("DropBoxTest", e2.getTimeMillis());
    assertTrue(null == dropbox.getNextEntry("DropBoxTest", e3.getTimeMillis()));
    assertTrue(e0.getTimeMillis() > before);
    assertTrue(e1.getTimeMillis() > e0.getTimeMillis());
    assertTrue(e2.getTimeMillis() > e1.getTimeMillis());
    assertTrue(e3.getTimeMillis() > e2.getTimeMillis());
    assertEquals(DropBoxManager.IS_TEXT, e0.getFlags());
    assertEquals(DropBoxManager.IS_TEXT, e1.getFlags());
    assertEquals(0, e2.getFlags());
    assertEquals(0, e3.getFlags());
    assertEquals("FILE0", e0.getText(80));
    byte[] buf1 = new byte[80];
    assertEquals("FILE1", new String(buf1, 0, e1.getInputStream().read(buf1)));
    assertTrue(null == e2.getText(80));
    byte[] buf2 = new byte[80];
    assertEquals("DATA2", new String(buf2, 0, e2.getInputStream().read(buf2)));
    assertTrue(null == e3.getText(80));
    byte[] buf3 = new byte[80];
    assertEquals("DATA3", new String(buf3, 0, e3.getInputStream().read(buf3)));
    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 55 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project screenbird by adamhub.

the class CompressUtil method doCompressFile.

public static void doCompressFile(String inFileName) {
    try {
        File file = new File(inFileName);
        FileOutputStream fos = new FileOutputStream(file + ".gz");
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream in = new BufferedInputStream(fin);
        byte[] buffer = new byte[1024];
        int i;
        while ((i = in.read(buffer)) >= 0) {
            gzos.write(buffer, 0, i);
        }
        in.close();
        gzos.close();
    } catch (IOException e) {
        System.out.println("Exception is" + e);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

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