KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bytes > ByteBlockChopper


1 package org.jbpm.bytes;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 /**
8  * is used by {@link org.jbpm.bytes.ByteArray} to chop a
9  * byte arrays into a list of chunks and glue them back together.
10  */

11 public abstract class ByteBlockChopper {
12
13   private static final int BLOCKSIZE = 1024;
14   
15   public static List JavaDoc chopItUp(byte[] byteArray) {
16     List JavaDoc bytes = null;
17     if ( (byteArray!=null)
18          && (byteArray.length>0) ){
19       bytes = new ArrayList JavaDoc();
20       int index = 0;
21       while ( (byteArray.length-index) > BLOCKSIZE ) {
22         byte[] byteBlock = new byte[BLOCKSIZE];
23         System.arraycopy(byteArray, index, byteBlock, 0, BLOCKSIZE);
24         bytes.add(byteBlock);
25         index+=BLOCKSIZE;
26       }
27       byte[] byteBlock = new byte[byteArray.length-index];
28       System.arraycopy(byteArray, index, byteBlock, 0, byteArray.length-index);
29       bytes.add(byteBlock);
30     }
31     return bytes;
32   }
33
34   public static byte[] glueChopsBackTogether(List JavaDoc bytes) {
35     byte[] value = null;
36     
37     if (bytes!=null) {
38       Iterator JavaDoc iter = bytes.iterator();
39       while (iter.hasNext()) {
40         byte[] byteBlock = (byte[]) iter.next();
41         if (value==null) {
42           value = byteBlock;
43         } else {
44           byte[] oldValue = value;
45           value = new byte[value.length+byteBlock.length];
46           System.arraycopy(oldValue, 0, value, 0, oldValue.length);
47           System.arraycopy(byteBlock, 0, value, oldValue.length, byteBlock.length);
48         }
49       }
50     }
51     
52     return value;
53   }
54 }
55
Popular Tags