package com.firefly.codec.utils;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.firefly.utils.log.Log;
import com.firefly.utils.log.LogFactory;

public abstract class ByteArrayUtils {
	
	private static Log log = LogFactory.getInstance().getLog("firefly-system");
	
	public static List<byte[]> splitData(byte[] data, int size, int part) {
		List<byte[]> list = splitData(data, size);
		if(list == null)
			return null;
		
		List<byte[]> ret = new ArrayList<>();
		int index = 0;
		for (int i = 0; i < part - 1; i++) {
			ret.add(list.get(i));
			index++;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
		try {
			while(index < list.size()) {
				out.write(list.get(index));
				index++;
			}
		} catch (Throwable t) {
			log.error("merge data error", t);
		}
		ret.add(out.toByteArray());
		return ret;
	}
	
	public static List<byte[]> splitData(byte[] data, int size) {
		if(size > data.length)
			return null;
		
		List<byte[]> list = new ArrayList<>();
		int srcPos = 0;
		int len = size;
		byte[] dest = null;
		while(true) {
			dest = new byte[len];
			System.arraycopy(data, srcPos, dest, 0, len);
			list.add(dest);
			srcPos = srcPos + len;
			if(srcPos >= data.length)
				break;
			len = Math.min(data.length - srcPos, size);
		}
		return list;
	}
}

@@ -31,46 +31,8 @@
 s;%0A%0A
-import java.io.ByteArrayOutputStream;%0A
 impo
@@ -83,355 +83,231 @@
 t;%0A%0A
-import com.firefly.utils.log.Log;%0Aimport com.firefly.utils.log.LogFactory;%0A%0Apublic abstract class ByteArrayUtils %7B%0A%09%0A%09private static Log log = LogFactory.getInstance().getLog(%22firefly-system%22);%0A%09%0A%09public static List%3Cbyte%5B%5D%3E splitData(byte%5B%5D data, int size, int part) %7B%0A%09%09List%3Cbyte%5B%5D%3E list = splitData(data, size);%0A%09%09if(list == null)%0A%09%09%09return null
+public abstract class ByteArrayUtils %7B%0A%09%0A%09public static List%3Cbyte%5B%5D%3E splitData(byte%5B%5D data, int size, int part) %7B%0A%09%09if(size %3E data.length)%0A%09%09%09throw new IllegalArgumentException(%22the part size is greater than data's length%22)
 ;%0A%09%09
@@ -322,18 +322,19 @@
 byte%5B%5D%3E 
-re
+lis
 t = new 
@@ -358,25 +358,90 @@
 int 
-index = 0
+srcPos = 0;%0A%09%09int len = size;%0A%09%09byte%5B%5D dest = null;%0A%09%09int blocks = part - 1
 ;%0A%09%09for
- 
 (int
@@ -452,24 +452,22 @@
  0; i %3C 
-part - 1
+blocks
 ; i++) %7B
@@ -474,300 +474,341 @@
 %0A%09%09%09
-ret.add(list.get(i));%0A%09%09%09index++;%0A%09%09%7D%0A%09%09ByteArrayOutputStream out = new ByteArrayOutputStream(data.length
+dest = new byte%5Blen%5D;%0A%09%09%09System.arraycopy(data, srcPos, dest, 0, dest.length);%0A%09%09%09list.add(dest
 );%0A%09%09
+%09
-try %7B%0A%09%09%09while(index %3C list.size()) %7B%0A%09%09%09%09out.write(list.get(index))
+srcPos = srcPos + len;%0A%09%09%09if(srcPos %3E= data.length)%0A%09%09%09%09return list
 ;%0A%09%09%09
-%09
-index++
+len = Math.min(data.length - srcPos, size)
 ;%0A
-%09
 %09%09%7D%0A%09%09
-%7D catch (Throwable t) %7B%0A%09%09%09log.error(%22merge data error%22, t);%0A%09%09%7D%0A%09%09ret.add(out.toByteArray()
+dest = new byte%5Bdata.length - srcPos%5D;%0A%09%09System.arraycopy(data, srcPos, dest, 0, dest.length);%0A%09%09list.add(dest
 );%0A%09
@@ -815,18 +815,19 @@
 %09return 
-re
+lis
 t;%0A%09%7D%0A%09%0A
@@ -921,19 +921,89 @@
 %0A%09%09%09
-return null
+throw new IllegalArgumentException(%22the part size is greater than data's length%22)
 ;%0A%09%09

optimize ByteArrayUtils performance