Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2010-2015, Inc. or its affiliates. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
 package com.amazonaws.metrics;
Used to help capture the throughput metrics.

Note this class is only relevant when metric is enabled. Otherwise it should not even be involved in the call stack to minimize runtime overhead.

 public class MetricInputStreamEntity extends InputStreamEntity {
     private final static int BUFFER_SIZE = 2048;
     private final ByteThroughputHelper helper;
     public MetricInputStreamEntity(ThroughputMetricType metricType,
             InputStream instreamlong length) {
          = new ByteThroughputHelper(metricType);
     public MetricInputStreamEntity(ThroughputMetricType metricType,
             final InputStream instreamlong length,
             final ContentType contentType) {
          = new ByteThroughputHelper(metricType);
     public void writeTo(final OutputStream outstreamthrows IOException {
         if (outstream instanceof MetricAware) {
             // hchar: There is currently no implementation of output stream that
             // has metric gathering capability but there could be!
             // So the code here is for future proof purposes.
             MetricAware aware = (MetricAware)outstream;
             if (aware.isMetricActivated()) {
                 // let the underlying output stream takes care of byte counting

Copied from {{@link org.apache.http.entity.InputStreamEntity.writeTo(} but modified to capture metrics for the output stream.
     private void writeToWithMetrics(final OutputStream outstreamthrows IOException {
         if (outstream == null) {
             throw new IllegalArgumentException("Output stream may not be null");
         final InputStream content = getContent();
         final long length = getContentLength();
         InputStream instream = content;
         try {
             byte[] buffer = new byte[];
             int l;
             if (length < 0) {
                 // consume until EOF
                 while ((l = != -1) {
                     long startNano = .startTiming();
                     outstream.write(buffer, 0, l);
             } else {
                 // consume no more than length
                 long remaining = length;
                 while (remaining > 0) {
                     l =, 0, (int)Math.min(remaining));
                     if (l == -1) {
                     long startNano = .startTiming();
                     outstream.write(buffer, 0, l);
                     remaining -= l;
        } finally {
New to GrepCode? Check out our FAQ X