Hello,
I would like to divide a large byte array into smaller chunks (say 64 bytes). Please help me with this.
Hello,
I would like to divide a large byte array into smaller chunks (say 64 bytes). Please help me with this.
You can use the method Arrays.copyOfRange(original, from, to)
public static byte[][] divideArray(byte[] source, int chucksize) {
byte[][] ret = new byte[(int)Math.ceil(source.length / (double)chucksize)][chucksize];
int start = 0;
for(int i = 0; i < ret.length; i++) {
ret[i] = Arrays.copyOfRange(source,start, start + chucksize);
start += chucksize ;
}
return ret;
}
Or You can use as Max suggested the System.arraycopy
public static byte[][] divideArray(byte[] source, int chucksize) {
byte[][] ret = new byte[(int)Math.ceil(source.length / (double)chucksize)][chucksize];
int start = 0;
for(int i = 0; i < ret.length; i++) {
if(start + chucksize > source.length) {
System.arraycopy(source, start, ret[i], 0, source.length - start);
} else {
System.arraycopy(source, start, ret[i], 0, chucksize);
}
start += chucksize ;
}
return ret;
}
See Arrays.copyOfRange for help. You could use this in a loop to split your array into several smaller chunks.
Well, System.arraycopy(src, fromPos, dest, toPos, length) is generally considered faster than Arrays.copyOfRange.
byte[] source = ...read it from somewhere...;
byte[] newArray = new byte[64];
System.arraycopy(source, 0, newArray, 0, 64);
You have two choices:
System.arraycopy(...)
Array.copyOfRange(...)
both of them work the same way but while first one only manages copy, second one is meant to be used to allocate the new chunk at the same time.
I benchmarked them with a result that System.arraycopy
is faster if you manage to allocate chunks all together before splitting your array but slightly slower if you allocate them whle you copy: in this case you should use Array.copyOfRange
.