Re: Unexpected low CPU usage

Johan Stuyts <[email protected]> Fri, 18 Jan 2008 21:01:55 +0100
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <op.t445xhquc4vsc3@audioslave>
> I don't mean at the system level, I mean in your SOAP or other server or
> client code.

I did not build any throttling, so unless it is in the components I use,  
there is none.

I have built a stream wrapper that limits the number of bytes that can be  
read from a request to prevent clients from sending requests that are too  
large. The code is short enough to reproduce here:

import java.io.IOException;
import java.io.InputStream;

public class MaxLengthInputStream extends InputStream
{
   public MaxLengthInputStream(InputStream pDelegate, long pMaximumLength)
   {
     super();

     aDelegate = pDelegate;
     aMaximumLength = pMaximumLength;
   }


   public int available() throws IOException
   {
     return aDelegate.available();
   }


   public int read() throws IOException
   {
     handleBytesRead(1);
     return aDelegate.read();
   }


   public int read(byte[] pBuffer, int pOffset, int pLength) throws  
IOException
   {
     int result = aDelegate.read(pBuffer, pOffset, pLength);
     handleBytesRead(result);
     return result;
   }


   public int read(byte[] pBuffer) throws IOException
   {
     int result = aDelegate.read(pBuffer);
     handleBytesRead(result);
     return result;
   }


   public void close() throws IOException
   {
     aDelegate.close();
   }


   private void handleBytesRead(int pNumberOfBytes) throws  
StreamTooLongException
   {
     aNumberOfBytesRead += pNumberOfBytes;
     if (aNumberOfBytesRead > aMaximumLength)
     {
       throw new StreamTooLongException("Stream may not be longer than: "  
+ aMaximumLength + " bytes");
     }
   }

   private InputStream aDelegate;

   private long aMaximumLength;

   private long aNumberOfBytesRead;
}

Johan Stuyts

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com