Hi.  I needed to count the bytes sent/received for each http connection.

The approach I wanted to take was to grab the input and output streams of the http connection when they were created and wrap them in byte counting input/output streams.

I did not see a mechanism to do this, so I added a new interface and an extra parameter object to the httpconnectionparamters class.

The same approach may be useful, e.g., for someone to plug in i/o streams that debug output what is going out/in on the wire.

I submit the following code/idea to be included in the httpclient code. (As then I won't have to maintain my own codebase with this change in it).


diff -Naur ../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java --- ../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 2005-06-23 15:38:01.000000000 +1200 +++ commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 2005-04-10 15:04:09.000000000 +1200
@@ -741,11 +741,6 @@
             }
inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize); outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize); - IOWrapper ioWrapper = (IOWrapper)this.params.getParameter("iowrapper");
-            if(ioWrapper!=null) {
-                inputStream = ioWrapper.wrapInput(inputStream);
-                outputStream = ioWrapper.wrapOutput(outputStream);
-            }
             isOpen = true;
         } catch (IOException e) {
             // Connection wasn't opened properly
@@ -804,11 +799,6 @@
         }
inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize); outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize); - IOWrapper ioWrapper = (IOWrapper)this.params.getParameter("iowrapper");
-        if(ioWrapper!=null) {
-            inputStream = ioWrapper.wrapInput(inputStream);
-            outputStream = ioWrapper.wrapOutput(outputStream);
-        }
         usingSecureSocket = true;
         tunnelEstablished = true;
     }
@@ -1372,8 +1362,4 @@

/** The local interface on which the connection is created, or null for the default */
     private InetAddress localAddress;
-    public interface IOWrapper {
-        InputStream wrapInput(InputStream is);
-        OutputStream wrapOutput(OutputStream os);
-    }
 }


Sample code to use it:
public static class ByteCountingWrapper implements HttpConnection.IOWrapper {
        int read;
        int sent;
        private IOUtil.CountInputStream cis;
        private IOUtil.CountOutputStream cos;
        public InputStream wrapInput(InputStream is) {
            if(cis!=null) {
                read+=cis.getBytesRead();
            }
            return cis = new IOUtil.CountInputStream(is);
        }
        public OutputStream wrapOutput(OutputStream out) {
            if(cos!=null) {
                sent+=cos.getCount();
            }
            return cos = new IOUtil.CountOutputStream(out);
        }
    }
        HttpClient client = new HttpClient();
        HttpConnectionManager mgr = client.getHttpConnectionManager();
        HttpConnectionManagerParams params = mgr.getParams();
        ByteCountingWrapper bcw = new ByteCountingWrapper();
        params.setParameter("iowrapper", bcw);
...

Regards, Peter
http://pingability.com - Web Site Monitoring Service

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to