hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: visualizing upload/download status in a statusbar
Date Fri, 10 Oct 2008 12:02:31 GMT
On Thu, 2008-10-09 at 07:41 -0700, Marcel Schiffel wrote:
> Hi,
> hope you had a relaxing vacation. I'm still facing the problems described
> two weeks ago and would be very grateful for some additional hints. Here's
> an extract of the client code: 
> 

...

> 
> Many Thanks
> Marcel
> 


import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.httpclient.methods.RequestEntity;

public class CoutingRequestEntity implements RequestEntity {
  
    private final RequestEntity entity;
    private final ProgressListener listener;
    
    public CoutingRequestEntity(
            final RequestEntity entity, 
            final ProgressListener listener) {
        super();
        this.entity = entity;
        this.listener = listener;
    }

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

    public String getContentType() {
        return this.entity.getContentType();
    }

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

    public void writeRequest(final OutputStream out) throws IOException
{
        this.entity.writeRequest(
                new CountingOutputStream(out, this.listener));
    }
    
    public static interface ProgressListener {
        
        void transferred(long num);
        
    }

    public static class CountingOutputStream extends FilterOutputStream
{

        private final ProgressListener listener;
        private long transferred;
        
        public CountingOutputStream(
                final OutputStream out, 
                final ProgressListener listener) {
            super(out);
            this.listener = listener;
            this.transferred = 0;
        }

        public void write(byte[] b, int off, int len) throws IOException
{
            super.write(b, off, len);
            this.transferred += len;
            this.listener.transferred(this.transferred);
        }

        public void write(int b) throws IOException {
            super.write(b);
            this.transferred++;
            this.listener.transferred(this.transferred);
        }
        
    }
}

Hope this helps

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message