You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by jo...@apache.org on 2008/04/24 08:35:22 UTC

svn commit: r651168 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/caching/CachingOutputStream.java src/test/org/apache/cocoon/caching/ src/test/org/apache/cocoon/caching/CachingOutputStreamTestCase.java status.xml

Author: joerg
Date: Wed Apr 23 23:35:19 2008
New Revision: 651168

URL: http://svn.apache.org/viewvc?rev=651168&view=rev
Log:
COCOON-2192: Fix CachingOutputStream not caching all content or leading to ArrayIndexOutOfBoundsException when using write(byte[], int, int).

Added:
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/caching/
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/caching/CachingOutputStreamTestCase.java
Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/caching/CachingOutputStream.java
    cocoon/branches/BRANCH_2_1_X/status.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/caching/CachingOutputStream.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/caching/CachingOutputStream.java?rev=651168&r1=651167&r2=651168&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/caching/CachingOutputStream.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/caching/CachingOutputStream.java Wed Apr 23 23:35:19 2008
@@ -27,7 +27,6 @@
  * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
  * @version CVS $Id$
  */
-
 public final class CachingOutputStream
 extends OutputStream {
 
@@ -55,7 +54,7 @@
         this.receiver.write(b);
         int newcount = this.bufCount + 1;
         if (newcount > this.buf.length) {
-            byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)];
+            byte newbuf[] = new byte[this.buf.length << 1];
             System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);
             this.buf = newbuf;
         }
@@ -70,7 +69,7 @@
     public void write(byte b[], int off, int len) throws IOException {
         this.receiver.write(b, off, len);
         if (len == 0) return;
-        int newcount = this.bufCount + (len-off);
+        int newcount = this.bufCount + len;
         if (newcount > this.buf.length) {
             byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)];
             System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);

Added: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/caching/CachingOutputStreamTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/caching/CachingOutputStreamTestCase.java?rev=651168&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/caching/CachingOutputStreamTestCase.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/caching/CachingOutputStreamTestCase.java Wed Apr 23 23:35:19 2008
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.caching;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+public class CachingOutputStreamTestCase extends TestCase {
+
+    public void testWriteInt() throws IOException {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        CachingOutputStream cachingOutputStream = new CachingOutputStream(byteArrayOutputStream);
+
+        for (int i = 0; i < 1000; i++) {
+            cachingOutputStream.write(i);
+        }
+
+        // the content in the original destination
+        byte[] content = byteArrayOutputStream.toByteArray();
+        // the content collected in the CachingOutputStream
+        byte[] cachedContent = cachingOutputStream.getContent();
+
+        assertEquals("Length of cached content is wrong:", content.length, cachedContent.length);
+        assertTrue("Cached content differs", Arrays.equals(content, cachedContent));
+        
+        // Test to exceed CachingOutputStream's buffer of 1024.
+        for (int i = 0; i < 1000; i++) {
+            cachingOutputStream.write(i);
+        }
+
+        // the content in the original destination
+        content = byteArrayOutputStream.toByteArray();
+        // the content collected in the CachingOutputStream
+        cachedContent = cachingOutputStream.getContent();
+
+        assertEquals("Length of cached content is wrong:", content.length, cachedContent.length);
+        assertTrue("Cached content differs", Arrays.equals(content, cachedContent));
+    }
+
+    public void testWriteByteArray() throws IOException {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        CachingOutputStream cachingOutputStream = new CachingOutputStream(byteArrayOutputStream);
+
+        byte[] data = new byte[1000];
+        new Random().nextBytes(data);
+        cachingOutputStream.write(data);
+
+        // the content in the original destination
+        byte[] content = byteArrayOutputStream.toByteArray();
+        // the content collected in the CachingOutputStream
+        byte[] cachedContent = cachingOutputStream.getContent();
+
+        assertEquals("Length of cached content is wrong:", content.length, cachedContent.length);
+        assertTrue("Cached content differs", Arrays.equals(data, cachedContent));
+        assertTrue("Cached content differs", Arrays.equals(content, cachedContent));
+
+        // Test to exceed CachingOutputStream's buffer of 1024.
+        cachingOutputStream.write(data);
+
+        // the content in the original destination
+        content = byteArrayOutputStream.toByteArray();
+        // the content collected in the CachingOutputStream
+        cachedContent = cachingOutputStream.getContent();
+
+        assertEquals("Length of cached content is wrong:", content.length, cachedContent.length);
+        assertTrue("Cached content differs", Arrays.equals(content, cachedContent));
+    }
+
+    public void testWriteByteArrayPart() throws IOException {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        CachingOutputStream cachingOutputStream = new CachingOutputStream(byteArrayOutputStream);
+
+        byte[] data = new byte[1000];
+        new Random().nextBytes(data);
+        for (int i = 0; i < 10; i++) {
+            cachingOutputStream.write(data, i * 100, 100);
+        }
+
+        // the content in the original destination
+        byte[] content = byteArrayOutputStream.toByteArray();
+        // the content collected in the CachingOutputStream
+        byte[] cachedContent = cachingOutputStream.getContent();
+
+        assertEquals("Length of cached content is wrong:", content.length, cachedContent.length);
+        assertTrue("Cached content differs", Arrays.equals(data, cachedContent));
+        assertTrue("Cached content differs", Arrays.equals(content, cachedContent));
+
+        // Test to exceed CachingOutputStream's buffer of 1024.
+        cachingOutputStream.write(data, 200, 100);
+
+        // the content in the original destination
+        content = byteArrayOutputStream.toByteArray();
+        // the content collected in the CachingOutputStream
+        cachedContent = cachingOutputStream.getContent();
+
+        assertEquals("Length of cached content is wrong:", content.length, cachedContent.length);
+        assertTrue("Cached content differs", Arrays.equals(content, cachedContent));
+    }
+
+}

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/status.xml?rev=651168&r1=651167&r2=651168&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Wed Apr 23 23:35:19 2008
@@ -182,6 +182,10 @@
 
   <changes>
   <release version="2.1.12" date="TBD">
+    <action dev="JH" type="fix" fixes-bug="COCOON-2192" due-to="Steven Dolg" due-to-email="steven.dolg@indoqa.com">
+        Core: Fix CachingOutputStream not caching all content or leading to ArrayIndexOutOfBoundsException when using
+        write(byte[], int, int).
+    </action>
     <action dev="JH" type="update" fixes-bug="COCOON-2168">
       Core: Set the default output buffer size of the pipeline to 1,048,576 (1 MB) rather than -1 (complete buffering)
       to avoid potential OutOfMemoryErrors on too large output.