Example usage for com.google.common.io CountingOutputStream CountingOutputStream

List of usage examples for com.google.common.io CountingOutputStream CountingOutputStream

Introduction

In this page you can find the example usage for com.google.common.io CountingOutputStream CountingOutputStream.

Prototype

public CountingOutputStream(OutputStream out) 

Source Link

Document

Wraps another output stream, counting the number of bytes written.

Usage

From source file:org.n52.iceland.util.http.HttpUtils.java

private void writeObject(HttpServletRequest request, HttpServletResponse response, MediaType contentType,
        Writable writable, EncodingExceptionHandler owserHandler) throws IOException, HTTPException {
    OutputStream out = null;// ww  w . j a v a 2s.  c  o  m
    response.setContentType(writable.getEncodedContentType().toString());

    try {
        out = response.getOutputStream();
        if (HTTPHeaders.supportsGzipEncoding(request) && writable.supportsGZip()) {
            out = new GZIPOutputStream(out);
            response.setHeader(HTTPHeaders.CONTENT_ENCODING, HTTPConstants.GZIP_ENCODING);
        }
        if (isCountingOutputStream) {
            out = new CountingOutputStream(out);
        }

        if (writable.hasForcedHttpStatus()) {
            response.setStatus(writable.getForcedHttpStatus().getCode());
        }

        writable.write(out, new ResponseProxy(response));
        out.flush();
    } catch (EncodingException e) {
        Object writeOwsExceptionReport = owserHandler.handleEncodingException(request, response, e);
        if (writeOwsExceptionReport != null) {
            Writable owserWritable = getWritable(writeOwsExceptionReport, contentType);
            try {
                owserWritable.write(out, new ResponseProxy(response));
                if (out != null) {
                    out.flush();
                }
            } catch (EncodingException ex) {
                throw new HTTPException(HTTPStatus.INTERNAL_SERVER_ERROR, ex);
            }
        }
    } finally {
        if (out instanceof CountingOutputStream) {
            Long bytesWritten = ((CountingOutputStream) out).getCount();
            eventBus.submit(new CountingOutputStreamEvent(bytesWritten));
        }
        if (out != null) {
            LOGGER.debug("Response status = " + response.getStatus());
            out.close();
        }

    }
}

From source file:org.glowroot.agent.fat.storage.util.CappedDatabase.java

private long write(String type, Copier copier) throws IOException {
    synchronized (lock) {
        if (closing) {
            return -1;
        }// w  w w .  j  a  va2s .  co m
        long startTick = ticker.read();
        out.startBlock();
        NonClosingCountingOutputStream countingStreamAfterCompression = new NonClosingCountingOutputStream(out);
        CountingOutputStream countingStreamBeforeCompression = new CountingOutputStream(
                new LZFOutputStream(countingStreamAfterCompression));
        copier.copyTo(countingStreamBeforeCompression);
        countingStreamBeforeCompression.close();
        long endTick = ticker.read();
        CappedDatabaseStats stats = statsByType.get(type);
        if (stats == null) {
            stats = new CappedDatabaseStats();
            statsByType.put(type, stats);
        }
        stats.record(countingStreamBeforeCompression.getCount(), countingStreamAfterCompression.getCount(),
                endTick - startTick);
        return out.endBlock();
    }
}

From source file:org.eclipse.packagedrone.repo.channel.apm.store.BlobStore.java

public long handleCreate(final String id, final IOConsumer<OutputStream> consumer) throws IOException {
    final Path path = makeDataPath(id);

    Files.createDirectories(path.getParent());

    try (CountingOutputStream stream = new CountingOutputStream(
            new BufferedOutputStream(Files.newOutputStream(path, StandardOpenOption.CREATE_NEW)))) {
        consumer.accept(stream);/*w w  w.  j  ava  2s .c o  m*/
        return stream.getCount();
    }
}

From source file:io.druid.query.aggregation.atomcube.AtomCubeQueryResource.java

@POST
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE, APPLICATION_SMILE })
public Response doPost(InputStream in, @QueryParam("pretty") String pretty,
        @Context final HttpServletRequest req // used only to get request content-type and remote address
) throws IOException {
    final long start = System.currentTimeMillis();
    final String reqContentType = req.getContentType();
    final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(reqContentType)
            || APPLICATION_SMILE.equals(reqContentType);
    ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper;
    final String contentType = isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE : MediaType.APPLICATION_JSON;
    final ObjectWriter jsonWriter = pretty != null ? objectMapper.writerWithDefaultPrettyPrinter()
            : objectMapper.writer();/*from   w ww. j  a v a 2s  . co  m*/

    AtomCubeQuery _query = objectMapper.readValue(in, AtomCubeQuery.class);
    final AtomCubeQuery atomQ = (AtomCubeQuery) _query.withId(UUID.randomUUID().toString());
    final Map<String, Object> responseContext1 = new MapMaker().makeMap();
    Sequence res = atomQ.run(this.conglomerate.findFactory(atomQ).createRunner(null), responseContext1);
    final Sequence results;
    if (res == null) {
        results = Sequences.empty();
    } else {
        results = res;
    }

    final Yielder yielder = results.toYielder(null, new YieldingAccumulator() {
        @Override
        public Object accumulate(Object accumulated, Object in) {
            yield();
            return in;
        }
    });

    final Map<String, Object> responseContext = new MapMaker().makeMap();
    Response.ResponseBuilder builder = Response.ok(new StreamingOutput() {
        @Override
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            CountingOutputStream os = new CountingOutputStream(outputStream);
            jsonWriter.writeValue(os, yielder);

            os.flush();
            os.close();

            final long queryTime = System.currentTimeMillis() - start;
            emitter.emit(DruidMetrics.makeQueryTimeMetric(null, jsonMapper, atomQ, req.getRemoteAddr())
                    .setDimension("success", "true").build("query/time", queryTime));
            emitter.emit(DruidMetrics.makeQueryTimeMetric(null, jsonMapper, atomQ, req.getRemoteAddr())
                    .build("query/bytes", os.getCount()));

            requestLogger.log(new RequestLogLine(new DateTime(), req.getRemoteAddr(), atomQ,
                    new QueryStats(ImmutableMap.<String, Object>of("query/time", queryTime, "query/bytes",
                            os.getCount(), "success", true))));
        }
    }, contentType).header("X-Druid-Query-Id", atomQ.getId());

    String responseCtxString = jsonMapper.writeValueAsString(responseContext);

    return builder.header("X-Druid-Response-Context", responseCtxString).build();
}

From source file:bear.plugins.java.JenkinsCache.java

public static File download2(String jdkVersion, File jenkinsCache, File tempDestDir, String jenkinsUri,
        String user, String pass) {
    try {// w ww .  java 2 s . c o m
        Optional<JDKFile> optional = load(jenkinsCache, jenkinsUri, jdkVersion);

        if (!optional.isPresent()) {
            throw new RuntimeException("could not find: " + jdkVersion);
        }

        String uri = optional.get().filepath;

        //                agent.get()

        //                agent.get()

        SSLContext sslContext = SSLContext.getInstance("TLSv1");

        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                System.out.println("getAcceptedIssuers =============");
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkClientTrusted =============");
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkServerTrusted =============");
            }
        } }, new SecureRandom());

        SSLSocketFactory sf = new SSLSocketFactory(sslContext);

        Scheme httpsScheme = new Scheme("https", 443, sf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);

        DefaultHttpClient httpClient = new DefaultHttpClient(
                new PoolingClientConnectionManager(schemeRegistry));

        MechanizeAgent agent = new MechanizeAgent();
        Cookie cookie2 = agent.cookies().addNewCookie("gpw_e24", ".", "oracle.com");
        cookie2.getHttpCookie().setPath("/");
        cookie2.getHttpCookie().setSecure(false);

        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("gpw_e24", ".");
        cookie.setDomain("oracle.com");
        cookie.setPath("/");
        cookie.setSecure(true);

        cookieStore.addCookie(cookie);

        httpClient.setCookieStore(cookieStore);

        HttpPost httppost = new HttpPost("https://login.oracle.com");

        httppost.setHeader("Authorization",
                "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8"));

        HttpResponse response = httpClient.execute(httppost);

        int code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("unable to auth: " + code);
        }

        //                EntityUtils.consumeQuietly(response.getEntity());

        httppost = new HttpPost(uri);

        response = httpClient.execute(httppost);

        code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("to download: " + uri);
        }

        File file = new File(tempDestDir, optional.get().name);
        HttpEntity entity = response.getEntity();

        final long length = entity.getContentLength();

        final CountingOutputStream os = new CountingOutputStream(new FileOutputStream(file));

        System.out.printf("Downloading %s to %s...%n", uri, file);

        Thread progressThread = new Thread(new Runnable() {
            double lastProgress;

            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    long copied = os.getCount();

                    double progress = copied * 100D / length;

                    if (progress != lastProgress) {
                        System.out.printf("\rProgress: %s%%", LangUtils.toConciseString(progress, 1));
                    }

                    lastProgress = progress;

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }, "progressThread");

        progressThread.start();

        ByteStreams.copy(entity.getContent(), os);

        progressThread.interrupt();

        System.out.println("Download complete.");

        return file;
    } catch (Exception e) {
        throw Exceptions.runtime(e);
    }
}

From source file:org.glowroot.agent.embedded.util.CappedDatabase.java

private long write(String type, Copier copier) throws IOException {
    long blockStartIndex;
    synchronized (lock) {
        if (closed) {
            return -1;
        }//from   w w w. ja  v  a 2s. com
        long startTick = ticker.read();
        out.startBlock();
        NonClosingCountingOutputStream countingStreamAfterCompression = new NonClosingCountingOutputStream(out);
        CountingOutputStream countingStreamBeforeCompression = new CountingOutputStream(
                newLZFOutputStream(countingStreamAfterCompression));
        copier.copyTo(countingStreamBeforeCompression);
        countingStreamBeforeCompression.close();
        long endTick = ticker.read();
        CappedDatabaseStats stats = statsByType.get(type);
        if (stats == null) {
            stats = new CappedDatabaseStats();
            statsByType.put(type, stats);
        }
        stats.record(countingStreamBeforeCompression.getCount(), countingStreamAfterCompression.getCount(),
                endTick - startTick);
        blockStartIndex = out.endBlock();
    }
    // fsync (if really needed here) does not need to be done under lock
    out.fsyncIfReallyNeeded();
    return blockStartIndex;
}

From source file:com.google.cloud.dataflow.sdk.coders.StandardCoder.java

/**
 * Returns the size in bytes of the encoded value using this coder.
 *///w  w  w . java 2s.  co  m
protected long getEncodedElementByteSize(T value, Context context) throws Exception {
    try {
        CountingOutputStream os = new CountingOutputStream(ByteStreams.nullOutputStream());
        encode(value, os, context);
        return os.getCount();
    } catch (Exception exn) {
        throw new IllegalArgumentException(
                "Unable to encode element '" + value + "' with coder '" + this + "'.", exn);
    }
}

From source file:com.android.tradefed.util.SizeLimitedOutputStream.java

/**
 * Creates a new tmp file, closing the old one as necessary
 * <p>//from   w w  w  . j a  v  a  2 s  .c  o m
 * Exposed for unit testing.
 * </p>
 *
 * @throws IOException
 * @throws FileNotFoundException
 */
synchronized void generateNextFile() throws IOException, FileNotFoundException {
    // close current stream
    close();
    mCurrentFilePos = getNextIndex(mCurrentFilePos);
    FileUtil.deleteFile(mFiles[mCurrentFilePos]);
    mFiles[mCurrentFilePos] = FileUtil.createTempFile(mTempFilePrefix, mTempFileSuffix);
    mCurrentOutputStream = new CountingOutputStream(
            new BufferedOutputStream(new FileOutputStream(mFiles[mCurrentFilePos]), BUFF_SIZE));
}

From source file:ddf.catalog.resource.download.ReliableResourceDownloader.java

public ResourceResponse setupDownload(Metacard metacard, DownloadStatusInfo downloadStatusInfo) {
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    String resourceName = resource.getName();

    fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
    countingFbos = new CountingOutputStream(fbos);
    streamReadByClient = new ReliableResourceInputStream(fbos, countingFbos, downloadState, downloadIdentifier,
            resourceResponse);/*from w ww . jav  a 2s  .c  o  m*/

    this.metacard = metacard;

    // Create new ResourceResponse to return that will encapsulate the
    // ReliableResourceInputStream that will be read by the client simultaneously as the product
    // is cached to disk (if caching is enabled)
    ResourceImpl newResource = new ResourceImpl(streamReadByClient, mimeType, resourceName);
    resourceResponse = new ResourceResponseImpl(resourceResponse.getRequest(), resourceResponse.getProperties(),
            newResource);

    // Get handle to retrieved product's InputStream
    resourceInputStream = resource.getInputStream();

    eventListener.setDownloadMap(downloadIdentifier, resourceResponse);
    downloadStatusInfo.addDownloadInfo(downloadIdentifier, this, resourceResponse);

    if (downloaderConfig.isCacheEnabled()) {

        CacheKey keyMaker = null;
        String key = null;
        try {
            keyMaker = new CacheKey(metacard, resourceResponse.getRequest());
            key = keyMaker.generateKey();
        } catch (IllegalArgumentException e) {
            LOGGER.info("Cannot create cache key for resource with metacard ID = {}", metacard.getId());
            return resourceResponse;
        }

        if (!resourceCache.isPending(key)) {

            // Fully qualified path to cache file that will be written to.
            // Example:
            // <INSTALL-DIR>/data/product-cache/<source-id>-<metacard-id>
            // <INSTALL-DIR>/data/product-cache/ddf.distribution-abc123
            filePath = FilenameUtils.concat(resourceCache.getProductCacheDirectory(), key);
            reliableResource = new ReliableResource(key, filePath, mimeType, resourceName, metacard);
            resourceCache.addPendingCacheEntry(reliableResource);

            try {
                fos = FileUtils.openOutputStream(new File(filePath));
                doCaching = true;
                this.downloadState.setCacheEnabled(true);
            } catch (IOException e) {
                LOGGER.info("Unable to open cache file {} - no caching will be done.", filePath);
            }
        } else {
            LOGGER.debug("Cache key {} is already pending caching", key);
        }
    }

    return resourceResponse;
}

From source file:com.wuba.utils.SizeLimitedOutputStream.java

/**
 * Creates a new tmp file, closing the old one as necessary
 * <p>// w w w  .  j a  va2  s. c om
 * Exposed for unit testing.
 * </p>
 *
 * @throws IOException
 * @throws FileNotFoundException
 */
synchronized void generateNextFile() throws IOException, FileNotFoundException {
    // close current stream
    close();
    mCurrentFilePos = getNextIndex(mCurrentFilePos);
    if (mFiles[mCurrentFilePos] != null) {
        mFiles[mCurrentFilePos].delete();
    }
    mFiles[mCurrentFilePos] = FileUtil.createTempFile(mTempFilePrefix, mTempFileSuffix);
    mCurrentOutputStream = new CountingOutputStream(
            new BufferedOutputStream(new FileOutputStream(mFiles[mCurrentFilePos]), BUFF_SIZE));
}