public class ArrayUtils {
public static byte[] sumMultiByteArray(ArrayList<byte[]> arrayList){
int byteArrayLen = 0;
for(int i = 0 ; i < arrayList.size() ; i ++){
byteArrayLen += arrayList.get(i).length;
}
byte[] sumBtyeArray = new byte[byteArrayLen];
int currentIndex = 0;
for(int i = 0 ; i < arrayList.size() ; i ++){
System.arraycopy(arrayList.get(i), 0 , sumBtyeArray, currentIndex, arrayList.get(i).length);
currentIndex += arrayList.get(i).length;
}
return sumBtyeArray;
}
public static int[] sumMultiIntArray(ArrayList<int[]> arrayList){
int byteArrayLen = 0;
for(int i = 0 ; i < arrayList.size() ; i ++){
byteArrayLen += arrayList.get(i).length;
}
int[] sumBtyeArray = new int[byteArrayLen];
int currentIndex = 0;
for(int i = 0 ; i < arrayList.size() ; i ++){
System.arraycopy(arrayList.get(i), 0 , sumBtyeArray, currentIndex, arrayList.get(i).length);
}
return sumBtyeArray;
}
}
最近在做socket传输,因为传输交互过程中需要传输字符命令,由于字符过长,有时候会只发一部分,导致字符串缺失,所以现在统一规定byte[] buffer大小,通过这个工具类来累加传输的byte[]。