/* Copyright (C) 2012 Yuvi Panda
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package in.yuvi.http.fluent;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.*;
import org.apache.http.Header;

public class CountingRequestEntity implements HttpEntity {
    private final HttpEntity delegate;

    private final ProgressListener listener;

    public static final double PROGRESS_TRIGGER_PERCENTAGE = 0.2 / 100;

    public CountingRequestEntity(final HttpEntity entity,
            final ProgressListener listener) {
        super();
        this.delegate = entity;
        this.listener = listener;
    }

    public long getContentLength() {
        return this.delegate.getContentLength();
    }

    public boolean isRepeatable() {
        return this.delegate.isRepeatable();
    }

    public void writeTo(final OutputStream out) throws IOException {
        this.delegate.writeTo(new CountingOutputStream(out, this.listener, this.getContentLength()));
    }

    public static class CountingOutputStream extends FilterOutputStream {

        private final ProgressListener listener;

        private long transferred;
        private long total;
        private long runningCount;
        private final long progressTriggerThreshold;

        public CountingOutputStream(final OutputStream out,
                final ProgressListener listener, final long totalLength) {
            super(out);
            this.listener = listener;
            this.transferred = 0;
            this.total = totalLength;
            this.runningCount = 0;
            this.progressTriggerThreshold = (long)(this.total *  PROGRESS_TRIGGER_PERCENTAGE);
        }

        public void write(byte[] b, int off, int len) throws IOException {
            out.write(b, off, len);
            transferred += len;
            runningCount += len;
            if(runningCount >= progressTriggerThreshold || transferred == total) {
                listener.onProgress(transferred, total);
                runningCount = 0;
            }
        }

        public void write(int b) throws IOException {
            out.write(b);
            transferred++;
            runningCount++;
            if(runningCount >= progressTriggerThreshold || transferred == total) {
                this.listener.onProgress(transferred, total);
                runningCount = 0;
            }
        }
    }

    @Override
    @Deprecated
    public void consumeContent() throws IOException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public InputStream getContent() throws IOException, IllegalStateException {
        return delegate.getContent();
    }

    @Override
    public Header getContentEncoding() {
        return delegate.getContentEncoding();
    }

    @Override
    public boolean isChunked() {
        return delegate.isChunked();
    }

    @Override
    public boolean isStreaming() {
        return delegate.isStreaming();
    }

    @Override
    public Header getContentType() {
        return delegate.getContentType();
    }
}

@@ -946,28 +946,22 @@
 ;%0A%0A    p
-ublic static
+rivate
  final d
@@ -997,20 +997,8 @@
 TAGE
- = 0.2 / 100
 ;%0A%0A 
@@ -1180,24 +1180,261 @@
 = listener;%0A
+        // Percenetage of total size after which to trigger progress updates. Defaults to 0.5%25%0A        this.PROGRESS_TRIGGER_PERCENTAGE = Double.valueOf(System.getProperty(%22in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD%22, %220.5%22)) / 100;%0A
     %7D%0A%0A    p
@@ -1776,16 +1776,45 @@
 Length()
+, PROGRESS_TRIGGER_PERCENTAGE
 ));%0A    
@@ -2152,16 +2152,16 @@
 am out,%0A
-
         
@@ -2223,16 +2223,58 @@
 alLength
+, final double PROGRESS_TRIGGER_PERCENTAGE
 ) %7B%0A    

Use a system property to control progress threshold