Search in sources :

Example 56 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project Minigames by AddstarMC.

the class Metrics method gzip.

/**
     * GZip compress a string of bytes
     *
     * @param input
     * @return
     */
public static byte[] gzip(String input) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;
    try {
        gzos = new GZIPOutputStream(baos);
        gzos.write(input.getBytes("UTF-8"));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (gzos != null)
            try {
                gzos.close();
            } catch (IOException ignore) {
            }
    }
    return baos.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 57 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project Denizen-For-Bukkit by DenizenScript.

the class MetricsLite method gzip.

/**
     * GZip compress a string of bytes
     *
     * @param input input
     * @return output
     */
public static byte[] gzip(String input) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;
    try {
        gzos = new GZIPOutputStream(baos);
        gzos.write(input.getBytes("UTF-8"));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (gzos != null) {
            try {
                gzos.close();
            } catch (IOException ignore) {
            }
        }
    }
    return baos.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 58 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project zaproxy by zaproxy.

the class ApiResponseConversionUtilsUnitTest method gzip.

private byte[] gzip(byte[] raw) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(raw.length);
    GZIPOutputStream zip = new GZIPOutputStream(bytes);
    zip.write(raw);
    zip.close();
    return bytes.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 59 with GZIPOutputStream

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

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

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

the class DropBoxManagerService method add.

public void add(DropBoxManager.Entry entry) {
    File temp = null;
    InputStream input = null;
    OutputStream output = null;
    final String tag = entry.getTag();
    try {
        int flags = entry.getFlags();
        if ((flags & DropBoxManager.IS_EMPTY) != 0)
            throw new IllegalArgumentException();
        init();
        if (!isTagEnabled(tag))
            return;
        long max = trimToFit();
        long lastTrim = System.currentTimeMillis();
        byte[] buffer = new byte[mBlockSize];
        input = entry.getInputStream();
        // First, accumulate up to one block worth of data in memory before
        // deciding whether to compress the data or not.
        int read = 0;
        while (read < buffer.length) {
            int n = input.read(buffer, read, buffer.length - read);
            if (n <= 0)
                break;
            read += n;
        }
        // If we have at least one block, compress it -- otherwise, just write
        // the data in uncompressed form.
        temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp");
        int bufferSize = mBlockSize;
        if (bufferSize > 4096)
            bufferSize = 4096;
        if (bufferSize < 512)
            bufferSize = 512;
        FileOutputStream foutput = new FileOutputStream(temp);
        output = new BufferedOutputStream(foutput, bufferSize);
        if (read == buffer.length && ((flags & DropBoxManager.IS_GZIPPED) == 0)) {
            output = new GZIPOutputStream(output);
            flags = flags | DropBoxManager.IS_GZIPPED;
        }
        do {
            output.write(buffer, 0, read);
            long now = System.currentTimeMillis();
            if (now - lastTrim > 30 * 1000) {
                // In case data dribbles in slowly
                max = trimToFit();
                lastTrim = now;
            }
            read = input.read(buffer);
            if (read <= 0) {
                FileUtils.sync(foutput);
                // Get a final size measurement
                output.close();
                output = null;
            } else {
                // So the size measurement is pseudo-reasonable
                output.flush();
            }
            long len = temp.length();
            if (len > max) {
                Slog.w(TAG, "Dropping: " + tag + " (" + temp.length() + " > " + max + " bytes)");
                temp.delete();
                // Pass temp = null to createEntry() to leave a tombstone
                temp = null;
                break;
            }
        } while (read > 0);
        long time = createEntry(temp, tag, flags);
        temp = null;
        final Intent dropboxIntent = new Intent(DropBoxManager.ACTION_DROPBOX_ENTRY_ADDED);
        dropboxIntent.putExtra(DropBoxManager.EXTRA_TAG, tag);
        dropboxIntent.putExtra(DropBoxManager.EXTRA_TIME, time);
        if (!mBooted) {
            dropboxIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
        }
        // Call sendBroadcast after returning from this call to avoid deadlock. In particular
        // the caller may be holding the WindowManagerService lock but sendBroadcast requires a
        // lock in ActivityManagerService. ActivityManagerService has been caught holding that
        // very lock while waiting for the WindowManagerService lock.
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SEND_BROADCAST, dropboxIntent));
    } catch (IOException e) {
        Slog.e(TAG, "Can't write: " + tag, e);
    } finally {
        IoUtils.closeQuietly(output);
        IoUtils.closeQuietly(input);
        entry.close();
        if (temp != null)
            temp.delete();
    }
}
Also used : InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Intent(android.content.Intent) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

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