Search in sources :

Example 41 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project Libraries-for-Android-Developers by eoecn.

the class JsonStreamerEntity method writeTo.

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    if (outstream == null) {
        throw new IllegalStateException("Output stream cannot be null.");
    }
    // Record the time when uploading started.
    long now = System.currentTimeMillis();
    // Keys used by the HashMaps.
    Set<String> keys;
    // Use GZIP compression when sending streams, otherwise just use
    // a buffered output stream to speed things up a bit.
    OutputStream upload;
    if (null != contentEncoding) {
        upload = new GZIPOutputStream(new BufferedOutputStream(outstream), BUFFER_SIZE);
    } else {
        upload = new BufferedOutputStream(outstream);
    }
    // Always send a JSON object.
    upload.write('{');
    // Send the K/V values.
    keys = kvParams.keySet();
    for (String key : keys) {
        // Write the JSON object's key.
        upload.write(escape(key));
        upload.write(':');
        // Evaluate the value (which cannot be null).
        Object value = kvParams.get(key);
        if (value instanceof Boolean) {
            upload.write((Boolean) value ? JSON_TRUE : JSON_FALSE);
        } else if (value instanceof Long) {
            upload.write((((Number) value).longValue() + "").getBytes());
        } else if (value instanceof Double) {
            upload.write((((Number) value).doubleValue() + "").getBytes());
        } else if (value instanceof Float) {
            upload.write((((Number) value).floatValue() + "").getBytes());
        } else if (value instanceof Integer) {
            upload.write((((Number) value).intValue() + "").getBytes());
        } else {
            upload.write(value.toString().getBytes());
        }
        upload.write(',');
    }
    // Buffer used for reading from input streams.
    byte[] buffer = new byte[BUFFER_SIZE];
    // Send the stream params.
    keys = streamParams.keySet();
    for (String key : keys) {
        RequestParams.StreamWrapper entry = streamParams.get(key);
        // Write the JSON object's key.
        upload.write(escape(key));
        // All uploads are sent as an object containing the file's details.
        upload.write(':');
        upload.write('{');
        // Send the streams's name.
        upload.write(STREAM_NAME);
        upload.write(':');
        upload.write(escape(entry.name));
        upload.write(',');
        // Send the streams's content type.
        upload.write(STREAM_TYPE);
        upload.write(':');
        upload.write(escape(entry.contentType));
        upload.write(',');
        // Prepare the file content's key.
        upload.write(STREAM_CONTENTS);
        upload.write(':');
        upload.write('"');
        // Upload the file's contents in Base64.
        Base64OutputStream outputStream = new Base64OutputStream(upload, Base64.NO_CLOSE | Base64.NO_WRAP);
        // Read from input stream until no more data's left to read.
        int bytesRead;
        while ((bytesRead = entry.inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        // Close the Base64 output stream.
        outputStream.close();
        // End the file's object and prepare for next one.
        upload.write('"');
        upload.write('}');
        upload.write(',');
    }
    // Include the elapsed time taken to upload everything.
    // This might be useful for somebody, but it serves us well since
    // there will almost always be a ',' as the last sent character.
    upload.write(STREAM_ELAPSED);
    upload.write(':');
    long elapsedTime = System.currentTimeMillis() - now;
    upload.write((elapsedTime + "}").getBytes());
    Log.i(LOG_TAG, "Uploaded JSON in " + Math.floor(elapsedTime / 1000) + " seconds");
    // Flush the contents up the stream.
    upload.flush();
    upload.close();
}
Also used : OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 42 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project buck by facebook.

the class CachingBuildEngine method updateAndStoreManifest.

// Update the on-disk manifest with the new dep-file rule key and push it to the cache.
private void updateAndStoreManifest(BuildRule rule, RuleKey key, ImmutableSet<SourcePath> inputs, RuleKeyAndInputs manifestKey, ArtifactCache cache) throws IOException {
    Preconditions.checkState(useManifestCaching(rule));
    final Path manifestPath = getManifestPath(rule);
    Manifest manifest = new Manifest();
    // If we already have a manifest downloaded, use that.
    if (rule.getProjectFilesystem().exists(manifestPath)) {
        try (InputStream inputStream = rule.getProjectFilesystem().newFileInputStream(manifestPath)) {
            manifest = new Manifest(inputStream);
        }
    } else {
        // Ensure the path to manifest exist
        rule.getProjectFilesystem().createParentDirs(manifestPath);
    }
    // this efficiently and it's not clear how much benefit this will give us.
    if (manifest.size() >= maxDepFileCacheEntries) {
        manifest = new Manifest();
    }
    // Update the manifest with the new output rule key.
    manifest.addEntry(fileHashCache, key, pathResolver, manifestKey.getInputs(), inputs);
    // Serialize the manifest to disk.
    try (OutputStream outputStream = rule.getProjectFilesystem().newFileOutputStream(manifestPath)) {
        manifest.serialize(outputStream);
    }
    final Path tempFile = Files.createTempFile("buck.", ".manifest");
    // `ArtifactCache` interface uses raw paths.
    try (InputStream inputStream = rule.getProjectFilesystem().newFileInputStream(manifestPath);
        OutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(tempFile)))) {
        ByteStreams.copy(inputStream, outputStream);
    }
    cache.store(ArtifactInfo.builder().addRuleKeys(manifestKey.getRuleKey()).build(), BorrowablePath.borrowablePath(tempFile)).addListener(() -> {
        try {
            Files.deleteIfExists(tempFile);
        } catch (IOException e) {
            LOG.warn(e, "Error occurred while deleting temporary manifest file for %s", manifestPath);
        }
    }, MoreExecutors.directExecutor());
}
Also used : Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) BorrowablePath(com.facebook.buck.io.BorrowablePath) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 43 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project vert.x by eclipse.

the class TestUtils method compressGzip.

/**
   * @param source
   * @return gzipped data
   * @throws Exception
   */
public static byte[] compressGzip(String source) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(source.getBytes());
    gos.close();
    return baos.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 44 with GZIPOutputStream

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

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();
    DropBoxManager dropbox = (DropBoxManager) getContext().getSystemService(Context.DROPBOX_SERVICE);
    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) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 45 with GZIPOutputStream

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

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(service);
    // 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();
    service.stop();
}
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)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)322 ByteArrayOutputStream (java.io.ByteArrayOutputStream)135 FileOutputStream (java.io.FileOutputStream)96 IOException (java.io.IOException)93 OutputStream (java.io.OutputStream)66 File (java.io.File)62 Test (org.junit.Test)48 BufferedOutputStream (java.io.BufferedOutputStream)30 FileInputStream (java.io.FileInputStream)28 OutputStreamWriter (java.io.OutputStreamWriter)27 GZIPInputStream (java.util.zip.GZIPInputStream)25 InputStream (java.io.InputStream)24 ByteBuffer (java.nio.ByteBuffer)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 BufferedWriter (java.io.BufferedWriter)18 DropBoxManager (android.os.DropBoxManager)15 BufferedReader (java.io.BufferedReader)11 DataOutputStream (java.io.DataOutputStream)11 FileNotFoundException (java.io.FileNotFoundException)11 InputStreamReader (java.io.InputStreamReader)11