commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Becke <be...@u.washington.edu>
Subject Re: HTTPClient Feature Patch
Date Sat, 22 Feb 2003 14:46:31 GMT
I would recommend against doing it in this way.  Primarily because it 
involves polling (the second thread calling 
getWrittenRequestBodyBytes() repeatedly) and because of the added 
synchronization overhead.

In my experience it is easier and more reusable to handle this at the 
InputStream level.  This involves adding a wrapper class around the 
InputStream that is being put/posted.  It would look something like:

class ProgressInputStream extends FilterInputStream {
	private static final long EVENT_THRESHOLD = 1024;

	private long bytesRead;
	private long totalBytesRead;
	private ProgressListener listener;

	public ProgressInputStream(InputStream is, ProgressListener listener) {
		super(is);
		this.listener = listener;
	}

	private void bytesRead(int count) {
		bytesRead+=count;
		if (bytesRead > EVENT_THRESHOLD) {
			totalBytesRead += bytesRead;
			bytesRead = 0;
			listener.progressAchieved(totalBytesRead);
		}		
	}

	public int read() {
		int byte = super.read();
		if (byte != -1) {
			bytesRead(1);
		}
		return byte;
	}
	
	public int read(byte[] b, int offset, int length) {
		int bytesRead = super.read(b, offset, length);
		if (bytesRead > 0 ) {
			bytesRead(bytesRead);
		}
		return bytesRead;
	}
}

interface ProgressListener {
	void progressAchieved(long totalBytesRead);
}

Then you would want to implement the ProgressListener interface with 
something that updates the progress in the UI, preferably from another 
thread.

Mike

On Friday, February 21, 2003, at 03:15 AM, Thoralf Rickert wrote:

> Hi!
>
> I'm not subribed to the list, so please make a CC to my address...
>
> I'm working on a small WebDAV application based on the 
> org.apache.webdav library. For user feedback it is necessary to know 
> how many bytes a Put request has already finished 
> (0%...25%...50%...75%...100%) (especially for big files). Because the 
> WebDAV PutMethod extends the httpclient PutMethod which extends the
>     org.apache.commons.httpclient.methods.EntityEnclosingMethod,
> I would like to "submit" a small patch, which makes it possible to get 
> the number of already sent bytes in a "controlling thread"....Here is 
> the code:
>
> package org.apache.commons.httpclient.methods;
> [...]
> public abstract class EntityEnclosingMethod extends GetMethod {
> [...]
>   private long writtenBytes = 0;
> [...]
>   protected boolean writeRequestBody(HttpState state,
>                                      HttpConnection conn)
>       throws IOException, HttpException {
>
>     [....]
>     writtenBytes = 0;
>     byte[] tmp = new byte[4096];
>     //int total = 0;
>     int i = 0;
>     while ((i = instream.read(tmp)) >= 0) {
>       outstream.write(tmp, 0, i);
>       //total += i;
>       writtenBytes += i;
>     }
>     [....]
>   }
>
>   /**
>    * Returns the Number of Request Body Bytes send to the server
>    */
>   public long getWrittenRequestBodyBytes() {
>     return this.writtenBytes;
>   }
> }
>
> If I would start the HttpConnection in a subthread I could call the 
> getWrittenRequestBodyBytes() method (or however you call it) during 
> upload to get the number of transfered bytes. So I can show the user, 
> that the application still works...
>
> Do you think, that this small patch is useful for you too or do you 
> know another way?
>
> Thanks,
> Thoralf Rickert
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message