tags:

views:

78

answers:

4

Hello,

I would like to divide a large byte array into smaller chunks (say 64 bytes). Please help me with this.

A: 

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;
    }
Vash
A: 

See Arrays.copyOfRange for help. You could use this in a loop to split your array into several smaller chunks.

moxn
+1  A: 

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);
Max
This is incorrect: it's not a matter of being faster, `Arrays.copyOfRange` also allocates a new `array` while `System.arraycopy` just copy elements in another `array` passed as parameter. So with second one you save the allocation.. that's why it is faster. If you check the definition of `Array.copyOfRange` you will see that it invokes `System.arraycopy`..
Jack
Yup, just checked, you are right.
Max
A: 

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.

Jack
Very interesting benchmark given that Array.copyOfRange() calls System.arraycopy: http://pastebin.com/SpSyx8Cd
Max