masonjm     2004/12/08 18:31:02

  Modified:    src/webdav/server/org/apache/slide/webdav/method
                        GetMethod.java
  Log:
  Configurable buffer size. Contributed by John Rousseau. Bug 32546.
  
  Revision  Changes    Path
  1.57      +35 -25    
jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/GetMethod.java
  
  Index: GetMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/GetMethod.java,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- GetMethod.java    6 Dec 2004 08:54:13 -0000       1.56
  +++ GetMethod.java    9 Dec 2004 02:31:02 -0000       1.57
  @@ -70,8 +70,11 @@
       // -------------------------------------------------------------- 
Constants
   
   
  -    protected final int BUFFER_SIZE = 2048;
  +    /** The default buffer size */
  +    protected static final int DEFAULT_BUFFER_SIZE = 2048;
   
  +    /** The maximum buffer size to use */
  +    protected int maxBufferSize = DEFAULT_BUFFER_SIZE;
   
   
       /**
  @@ -79,19 +82,6 @@
        */
       protected static final String mimeSeparation = "SLIDE_MIME_BOUNDARY";
   
  -
  -    /**
  -     * The input buffer size to use when serving resources.
  -     */
  -    protected int input = 2048;
  -
  -
  -    /**
  -     * The output buffer size to use when serving resources.
  -     */
  -    protected int output = 2048;
  -
  -
       /**
        * Print content.
        */
  @@ -123,6 +113,13 @@
        */
       public GetMethod(NamespaceAccessToken token, WebdavServletConfig config) 
{
           super(token, config);
  +
  +        // Try to get the max buffer size from the config, else we default to
  +        // the DEFAULT_BUFFER_SIZE
  +        String maxBufferSizeString = 
  +            token.getNamespaceConfig().getParameter("max-get-buffer-size");
  +        if (maxBufferSizeString != null)
  +            maxBufferSize = Integer.parseInt(maxBufferSizeString);
       }
   
       /**
  @@ -276,7 +273,7 @@
       
                           // do this before setting content length, as Tomcat 
5 seems to be picky
                           // about this
  -                        resp.setBufferSize(output);
  +                        
resp.setBufferSize(getBufferSize(resourceInfo.length));
   
                           if ( ((ranges == null) || (ranges.isEmpty())) ) {
                               // full content response
  @@ -397,10 +394,10 @@
           IOException exception = null;
   
           InputStream istream = new BufferedInputStream
  -            (resourceInputStream, input);
  +            (resourceInputStream, getBufferSize(resourceInfo.length));
   
           // Copy the input stream to the output stream
  -        exception = copyRange(istream, ostream);
  +        exception = copyRange(istream, ostream, resourceInfo.length);
   
           // Clean up the input and output streams
           try {
  @@ -446,7 +443,8 @@
           IOException exception = null;
   
           InputStream istream =
  -            new BufferedInputStream(resourceInputStream, input);
  +            new BufferedInputStream(resourceInputStream, 
  +                                    getBufferSize(range.end - range.start));
           exception = copyRange(istream, ostream, range.start, range.end);
   
           // Clean up the input and output streams
  @@ -495,7 +493,8 @@
           while ( (exception == null) && (ranges.hasMoreElements()) ) {
   
               InputStream istream =
  -                new BufferedInputStream(resourceInputStream, input);
  +                new BufferedInputStream(resourceInputStream, 
  +                                        getBufferSize(resourceInfo.length));
   
               Range currentRange = (Range) ranges.nextElement();
   
  @@ -551,11 +550,12 @@
        * @return Exception which occured during processing
        */
       private IOException copyRange(InputStream istream,
  -                                  ServletOutputStream ostream) {
  +                                  ServletOutputStream ostream,
  +                                  long resourceLength) {
   
           // Copy the input stream to the output stream
           IOException exception = null;
  -        byte buffer[] = new byte[input];
  +        byte buffer[] = new byte[getBufferSize(resourceLength)];
           int len = buffer.length;
           while (true) {
               try {
  @@ -598,7 +598,7 @@
           IOException exception = null;
           long bytesToRead = end - start + 1;
   
  -        byte buffer[] = new byte[input];
  +        byte buffer[] = new byte[getBufferSize(bytesToRead)];
           int len = buffer.length;
           while ( (bytesToRead > 0) && (len >= buffer.length)) {
               try {
  @@ -622,6 +622,16 @@
   
       }
   
  +    /**
  +     * Get a reasonable buffer size.
  +     */
  +    private int getBufferSize(long resourceLength)
  +    {
  +        if (resourceLength <= 0)
  +            return DEFAULT_BUFFER_SIZE;
  +
  +        return (int)Math.min(resourceLength, maxBufferSize);
  +    }
   
       /**
        * Parse the range header.
  
  
  

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

Reply via email to