webwork/src/main/webwork/util FastByteArrayOutputStream.java,1.2,1.3

[email protected] Thu, 13 Nov 2003 14:07:36 -0800
Newsgroups gmane.comp.java.open-symphony.cvs
Message-ID <[email protected]>
Update of /cvsroot/opensymphony/webwork/src/main/webwork/util
In directory sc8-pr-cvs1:/tmp/cvs-serv30078

Modified Files:
	FastByteArrayOutputStream.java 
Log Message:
Use System.arraycopy instead of byte by byte copying. Fixes issue WW-380


Index: FastByteArrayOutputStream.java
===================================================================
RCS file: /cvsroot/opensymphony/webwork/src/main/webwork/util/FastByteArrayOutputStream.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- FastByteArrayOutputStream.java	12 Oct 2002 14:49:20 -0000	1.2
+++ FastByteArrayOutputStream.java	13 Nov 2003 22:07:33 -0000	1.3
@@ -139,6 +139,22 @@
       return new String(toByteArray());
    }
 
+   /**
+   * Create a new buffer and store the
+   * current one in linked list
+   */
+   protected void addBuffer()
+   {
+      if (buffers == null)
+         buffers = new LinkedList();
+
+      buffers.addLast(buffer);
+
+      buffer = new byte[blockSize];
+      size += index;
+      index = 0;
+   }
+
    // OutputStream overrides ----------------------------------------
    public void write(int datum) throws IOException
    {
@@ -148,17 +164,7 @@
       } else
       {
          if (index == blockSize)
-         {
-            // Create new buffer and store current in linked list
-            if (buffers == null)
-               buffers = new LinkedList();
-
-            buffers.addLast(buffer);
-
-            buffer = new byte[blockSize];
-            size += index;
-            index = 0;
-         }
+            addBuffer();
 
          // store the byte
          buffer[index++] = (byte) datum;
@@ -180,12 +186,22 @@
          throw new IOException("Stream closed");
       } else
       {
-         if (index + length >= blockSize)
+         if (index + length > blockSize)
          {
-            // Write byte by byte
-            // FIXME optimize this to use arraycopy's instead
-            for (int i = 0; i < length; i++)
-               write(data[offset+i]);
+            int copyLength;
+            do
+            {
+               if (index==blockSize)
+                  addBuffer();
+
+               copyLength = blockSize-index;
+               if (length < copyLength)
+                  copyLength = length;
+               System.arraycopy(data, offset, buffer, index, copyLength);
+               offset += copyLength;
+               index += copyLength;
+               length -= copyLength;
+            } while (length > 0);
          } else
          {
             // Copy in the subarray
@@ -194,7 +210,7 @@
          }
       }
    }
-
+   
    public void close()
    {
       closed = true;




-------------------------------------------------------
This SF.Net email sponsored by: ApacheCon 2003,
16-19 November in Las Vegas. Learn firsthand the latest
developments in Apache, PHP, Perl, XML, Java, MySQL,
WebDAV, and more! http://www.apachecon.com/