Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 50413 invoked from network); 22 Feb 2003 14:46:43 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 22 Feb 2003 14:46:43 -0000 Received: (qmail 15004 invoked by uid 97); 22 Feb 2003 14:48:19 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 14997 invoked from network); 22 Feb 2003 14:48:18 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 22 Feb 2003 14:48:18 -0000 Received: (qmail 50183 invoked by uid 500); 22 Feb 2003 14:46:40 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 50163 invoked from network); 22 Feb 2003 14:46:40 -0000 Received: from mxout3.cac.washington.edu (140.142.32.166) by daedalus.apache.org with SMTP; 22 Feb 2003 14:46:40 -0000 Received: from smtp.washington.edu (smtp.washington.edu [140.142.32.139]) by mxout3.cac.washington.edu (8.12.1+UW01.12/8.12.1+UW02.12) with ESMTP id h1MEkZAa030758; Sat, 22 Feb 2003 06:46:35 -0800 Received: from u.washington.edu (pool-129-44-175-110.bos.east.verizon.net [129.44.175.110]) (authenticated bits=0) by smtp.washington.edu (8.12.1+UW01.12/8.12.1+UW02.12) with ESMTP id h1MEkXZH006405 (version=TLSv1/SSLv3 cipher=DES-CBC3-SHA bits=168 verify=NOT); Sat, 22 Feb 2003 06:46:34 -0800 Date: Sat, 22 Feb 2003 09:46:31 -0500 Subject: Re: HTTPClient Feature Patch Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v551) Cc: Thoralf Rickert To: "Jakarta Commons Developers List" From: Michael Becke In-Reply-To: <3E55E022.4020109@m84.de> Message-Id: <6E461CE2-4674-11D7-8607-00306557E112@u.washington.edu> Content-Transfer-Encoding: 7bit X-Mailer: Apple Mail (2.551) X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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