Search in sources :

Example 31 with CountingOutputStream

use of com.google.common.io.CountingOutputStream in project gradle by gradle.

the class ByteOutput method start.

/**
 * Starts writing to the given offset. Can be beyond the current length of the file.
 */
public DataOutputStream start(long offset) throws IOException {
    file.seek(offset);
    bufferedOutputStream.clear();
    countingOutputStream = new CountingOutputStream(bufferedOutputStream);
    return new DataOutputStream(countingOutputStream);
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) DataOutputStream(java.io.DataOutputStream)

Example 32 with CountingOutputStream

use of com.google.common.io.CountingOutputStream in project gerrit by GerritCodeReview.

the class RestApiServlet method replyBinaryResult.

@SuppressWarnings("resource")
static long replyBinaryResult(@Nullable HttpServletRequest req, HttpServletResponse res, BinaryResult bin) throws IOException {
    final BinaryResult appResult = bin;
    try {
        if (bin.getAttachmentName() != null) {
            res.setHeader("Content-Disposition", "attachment; filename=\"" + bin.getAttachmentName() + "\"");
        }
        if (bin.isBase64()) {
            if (req != null && JSON_TYPE.equals(req.getHeader(HttpHeaders.ACCEPT))) {
                bin = stackJsonString(res, bin);
            } else {
                bin = stackBase64(res, bin);
            }
        }
        if (bin.canGzip() && acceptsGzip(req)) {
            bin = stackGzip(res, bin);
        }
        res.setContentType(bin.getContentType());
        long len = bin.getContentLength();
        if (0 <= len && len < Integer.MAX_VALUE) {
            res.setContentLength((int) len);
        } else if (0 <= len) {
            res.setHeader("Content-Length", Long.toString(len));
        }
        if (req == null || !"HEAD".equals(req.getMethod())) {
            try (CountingOutputStream dst = new CountingOutputStream(res.getOutputStream())) {
                bin.writeTo(dst);
                return dst.getCount();
            }
        }
        return 0;
    } finally {
        appResult.close();
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Example 33 with CountingOutputStream

use of com.google.common.io.CountingOutputStream in project tutorials by eugenp.

the class GuavaCountingOutputStreamTest method givenData_whenCountReachesLimit_thenThrowException.

@Test(expected = RuntimeException.class)
public void givenData_whenCountReachesLimit_thenThrowException() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CountingOutputStream cos = new CountingOutputStream(out);
    byte[] data = new byte[1024];
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    int b;
    while ((b = in.read()) != -1) {
        cos.write(b);
        if (cos.getCount() >= MAX) {
            throw new RuntimeException("Write limit reached");
        }
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 34 with CountingOutputStream

use of com.google.common.io.CountingOutputStream in project jib by google.

the class PullAndCacheBaseImageLayerStep method call.

/**
 * Depends on {@code pullAuthorizationFuture}.
 */
@Override
public CachedLayer call() throws IOException, RegistryException, LayerPropertyNotFoundException, ExecutionException, InterruptedException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, layerDigest))) {
        RegistryClient registryClient = new RegistryClient(pullAuthorizationFuture.get(), buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
        // Checks if the layer already exists in the cache.
        CachedLayer cachedLayer = new CacheReader(cache).getLayer(layerDigest);
        if (cachedLayer != null) {
            return cachedLayer;
        }
        CacheWriter cacheWriter = new CacheWriter(cache);
        CountingOutputStream layerOutputStream = cacheWriter.getLayerOutputStream(layerDigest);
        registryClient.pullBlob(layerDigest, layerOutputStream);
        return cacheWriter.getCachedLayer(layerDigest, layerOutputStream);
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) Timer(com.google.cloud.tools.jib.Timer) CacheWriter(com.google.cloud.tools.jib.cache.CacheWriter) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) CacheReader(com.google.cloud.tools.jib.cache.CacheReader)

Example 35 with CountingOutputStream

use of com.google.common.io.CountingOutputStream in project druid by druid-io.

the class PrefetchableTextFilesFirehoseFactoryTest method setup.

@BeforeClass
public static void setup() throws IOException {
    NullHandling.initializeForTests();
    TEST_DIR = tempDir.newFolder();
    for (int i = 0; i < 100; i++) {
        try (CountingOutputStream cos = new CountingOutputStream(Files.newOutputStream(new File(TEST_DIR, "test_" + i).toPath()));
            Writer writer = new BufferedWriter(new OutputStreamWriter(cos, StandardCharsets.UTF_8))) {
            for (int j = 0; j < 100; j++) {
                final String a = StringUtils.format("%d,%03d,%03d\n", (20171220 + i), i, j);
                writer.write(a);
            }
            writer.flush();
            // Every file size must be same
            if (FILE_SIZE == -1) {
                FILE_SIZE = cos.getCount();
            } else {
                Assert.assertEquals(FILE_SIZE, cos.getCount());
            }
        }
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter) BeforeClass(org.junit.BeforeClass)

Aggregations

CountingOutputStream (com.google.common.io.CountingOutputStream)35 IOException (java.io.IOException)10 OutputStream (java.io.OutputStream)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 Path (java.nio.file.Path)3 Test (org.junit.Test)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)2 ResourceResponse (ddf.catalog.operation.ResourceResponse)2 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 InputStream (java.io.InputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2