一、前言
关于gaejtools源码包中的appengine.util.ByteArrayUtil字节数组工具类,进行字节数组内容的分割split、合并merge操作,详情参见源码说明部分。
二、源码说明
package appengine.util;@b@@b@import java.util.ArrayList;@b@import java.util.Iterator;@b@import java.util.List;@b@import org.apache.commons.collections.CollectionUtils;@b@@b@public class ByteArrayUtil@b@{@b@ public static List<byte[]> split(byte[] bytes, int shardSize)@b@ {@b@ List bytesList = new ArrayList();@b@ int wholeSize = bytes.length;@b@ if (wholeSize < shardSize) {@b@ bytesList.add(bytes);@b@ } else {@b@ int shardCount = wholeSize / shardSize;@b@ int lastSize = wholeSize % shardSize;@b@ for (int index = 0; index < shardCount; ++index) {@b@ byte[] shard = new byte[shardSize];@b@ System.arraycopy(bytes, index * shardSize, shard, 0, shardSize);@b@ bytesList.add(shard);@b@ }@b@ byte[] shard = new byte[lastSize];@b@ System.arraycopy(bytes, shardCount * shardSize, shard, 0, lastSize);@b@ bytesList.add(shard);@b@ }@b@ return bytesList;@b@ }@b@@b@ public static byte[] merge(List<byte[]> bytesList)@b@ {@b@ if (CollectionUtils.isEmpty(bytesList))@b@ return new byte[0];@b@@b@ int totalSize = 0;@b@ Iterator i = bytesList.iterator();@b@ while (i.hasNext()) {@b@ byte[] next = (byte[])i.next();@b@ if (next != null)@b@ totalSize += next.length;@b@ }@b@@b@ return merge(bytesList, totalSize);@b@ }@b@@b@ public static byte[] merge(List<byte[]> bytesList, int totalSize)@b@ {@b@ if (CollectionUtils.isEmpty(bytesList))@b@ return new byte[0];@b@@b@ byte[] bytes = new byte[totalSize];@b@ int offset = 0;@b@ Iterator i = bytesList.iterator();@b@ while (i.hasNext()) {@b@ byte[] shard = (byte[])i.next();@b@ if (shard != null) {@b@ System.arraycopy(shard, 0, bytes, offset, shard.length);@b@ offset += shard.length;@b@ }@b@ }@b@ return bytes;@b@ }@b@}