Can anyone explain me the logic behind strange behavior of Arrays.copyOfRange(byte[], int, int))? I can illustrate what I mean with simple example:
byte[] bytes = new byte[] {1, 1, 1};
Arrays.copyOfRange(bytes, 3, 4); // Returns single element (0) array
Arrays.copyOfRange(bytes, 4, 5); // Throws ArrayIndexOutOfBoundsException
In both cases I copy ranges outside of array boundaries (i.e. start >= array.length
), so the condition for error is at least strange for me (if from < 0
or from > original.length
). In my opinion it should be: if from < 0
or from >= original.length
. Maybe I'm missing something?
Look, how copyOfRange works:
The most important part is this:
So when you're calling Arrays.copyOfRange(bytes, 3, 4); the last param of System.arraycopy "length - the number of array elements to be copied" is 0. The call of arraycopy looks like System.arraycopy(original, from, copy, 0,0);