Search in sources :

Example 61 with GZIPOutputStream

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

the class InputMethodSubtypeArray method compress.

private static byte[] compress(final byte[] data) {
    try (final ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
        final GZIPOutputStream zipper = new GZIPOutputStream(resultStream)) {
        zipper.write(data);
        zipper.finish();
        return resultStream.toByteArray();
    } catch (Exception e) {
        Slog.e(TAG, "Failed to compress the data.", e);
        return null;
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 62 with GZIPOutputStream

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

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)

Example 63 with GZIPOutputStream

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

the class InputMethodSubtypeArray method compress.

private static byte[] compress(final byte[] data) {
    try (final ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
        final GZIPOutputStream zipper = new GZIPOutputStream(resultStream)) {
        zipper.write(data);
        zipper.finish();
        return resultStream.toByteArray();
    } catch (Exception e) {
        Slog.e(TAG, "Failed to compress the data.", e);
        return null;
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 64 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project midpoint by Evolveum.

the class RUtil method getByteArrayFromXml.

public static byte[] getByteArrayFromXml(String xml, boolean compress) {
    byte[] array;
    GZIPOutputStream gzip = null;
    try {
        if (compress) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(xml.getBytes("utf-8"));
            gzip.close();
            out.close();
            array = out.toByteArray();
        } else {
            array = xml.getBytes("utf-8");
        }
    } catch (Exception ex) {
        throw new SystemException("Couldn't save full xml object, reason: " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(gzip);
    }
    return array;
}
Also used : SystemException(com.evolveum.midpoint.util.exception.SystemException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Example 65 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project Glowstone by GlowstoneMC.

the class Metrics method compress.

/**
     * Gzips the given String.
     *
     * @param str The string to gzip.
     * @return The gzipped String.
     * @throws IOException If the compression failed.
     */
private static byte[] compress(final String str) throws IOException {
    if (str == null) {
        return null;
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(outputStream);
    gzip.write(str.getBytes("UTF-8"));
    gzip.close();
    return outputStream.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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