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;
}
}
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();
}
}
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;
}
}
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;
}
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();
}
Aggregations