[jbpm-commits] JBoss JBPM SVN: r2387 - in jbpm3/branches/aguizar/modules/core/src: test/java/org/jbpm/bytes and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Sep 25 15:29:34 EDT 2008


Author: alex.guizar at jboss.com
Date: 2008-09-25 15:29:34 -0400 (Thu, 25 Sep 2008)
New Revision: 2387

Modified:
   jbpm3/branches/aguizar/modules/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java
   jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayDbTest.java
   jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayTest.java
Log:
revised the chopping and gluing logic

Modified: jbpm3/branches/aguizar/modules/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java
===================================================================
--- jbpm3/branches/aguizar/modules/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java	2008-09-25 19:26:24 UTC (rev 2386)
+++ jbpm3/branches/aguizar/modules/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java	2008-09-25 19:29:34 UTC (rev 2387)
@@ -22,7 +22,6 @@
 package org.jbpm.bytes;
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import org.jbpm.JbpmConfiguration;
@@ -45,36 +44,42 @@
       bytes = new ArrayList();
       int index = 0;
       while ( (byteArray.length-index) > blockSize ) {
-        byte[] byteBlock = new byte[blockSize];
-        System.arraycopy(byteArray, index, byteBlock, 0, blockSize);
-        bytes.add(byteBlock);
+        bytes.add(subArray(byteArray, index, blockSize));
         index+=blockSize;
       }
-      byte[] byteBlock = new byte[byteArray.length-index];
-      System.arraycopy(byteArray, index, byteBlock, 0, byteArray.length-index);
-      bytes.add(byteBlock);
+      bytes.add(subArray(byteArray, index, byteArray.length-index));
     }
     return bytes;
   }
 
+  private static byte[] subArray(byte[] array, int offset, int length) {
+    byte[] subArray = new byte[length];
+    System.arraycopy(array, offset, subArray, 0, length);
+    return subArray;
+  }
+
   public static byte[] glueChopsBackTogether(List bytes) {
-    byte[] value = null;
+    byte[] byteArray = null;
     
-    if (bytes!=null) {
-      Iterator iter = bytes.iterator();
-      while (iter.hasNext()) {
-        byte[] byteBlock = (byte[]) iter.next();
-        if (value==null) {
-          value = byteBlock;
-        } else {
-          byte[] oldValue = value;
-          value = new byte[value.length+byteBlock.length];
-          System.arraycopy(oldValue, 0, value, 0, oldValue.length);
-          System.arraycopy(byteBlock, 0, value, oldValue.length, byteBlock.length);
+    if (bytes!=null && !bytes.isEmpty()) {
+      if (bytes.size() == 1) {
+        byteArray = (byte[]) bytes.get(0);
+      }
+      else {
+        int totalLength = 0;
+        for (int i = 0, n = bytes.size(); i < n; i++) {
+          byte[] block = (byte[]) bytes.get(i);
+          totalLength += block.length;
         }
+        byteArray = new byte[totalLength];
+        int index = 0;
+        for (int i = 0, n = bytes.size(); i < n; i++) {
+          byte[] block = (byte[]) bytes.get(i);
+          System.arraycopy(block, 0, byteArray, index, block.length);
+          index += block.length;
+        }
       }
     }
-
-    return value;
+    return byteArray;
   }
 }

Modified: jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayDbTest.java
===================================================================
--- jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayDbTest.java	2008-09-25 19:26:24 UTC (rev 2386)
+++ jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayDbTest.java	2008-09-25 19:29:34 UTC (rev 2387)
@@ -75,7 +75,7 @@
     assertNull(retrievedByteArray.getBytes());
   }
 
-  private byte[] getMultipleBlockBytes() {
+  private static byte[] getMultipleBlockBytes() {
     String text = "muchos bytes"; // (10 bytes)
     for (int i=0; i<8; i++) text+=text;
     // now text should be 2560 bytes

Modified: jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayTest.java
===================================================================
--- jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayTest.java	2008-09-25 19:26:24 UTC (rev 2386)
+++ jbpm3/branches/aguizar/modules/core/src/test/java/org/jbpm/bytes/ByteArrayTest.java	2008-09-25 19:29:34 UTC (rev 2387)
@@ -21,25 +21,31 @@
  */
 package org.jbpm.bytes;
 
+import java.util.List;
+
 import org.jbpm.AbstractJbpmTestCase;
+import org.jbpm.JbpmConfiguration;
 
 public class ByteArrayTest extends AbstractJbpmTestCase {
 
-  public void testByteChopping2Blocks() {
-    ByteArray byteArray = new ByteArray(new byte[2048]);
-    assertEquals(2, byteArray.byteBlocks.size());
+  public void testByteChoppingBlockCount() {
+    int blockSize = JbpmConfiguration.Configs.getInt("jbpm.byte.block.size");
+    for (int arrayLength = 125; arrayLength <= 1000; arrayLength *= 2) {
+      ByteArray byteArray = new ByteArray(new byte[arrayLength]);
+      int blockCount = arrayLength / blockSize;
+      if (arrayLength % blockSize > 0) blockCount++;
+      List byteBlocks = byteArray.byteBlocks;
+      assertEquals(blockCount, byteBlocks.size());
+    }
   }
 
-  public void testByteChopping3Blocks() {
-    ByteArray byteArray = new ByteArray(new byte[2049]);
-    assertEquals(3, byteArray.byteBlocks.size());
+  public void testReassembling() {
+    for (int arrayLength = 125; arrayLength <= 1000; arrayLength *= 2) {
+      ByteArray byteArray = new ByteArray(new byte[arrayLength]);      
+      assertEquals(arrayLength, byteArray.getBytes().length);
+    }
   }
 
-  public void testReassembling() {
-    ByteArray byteArray = new ByteArray(new byte[2049]);
-    assertEquals(2049, byteArray.getBytes().length);
-  }
-  
   public void testEquals() {
     ByteArray left =  new ByteArray("the same bytes".getBytes());
     ByteArray right = new ByteArray("the same bytes".getBytes());




More information about the jbpm-commits mailing list