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