Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Arrays.copyOfRange() for below API 9

Tags:

java

android

I am developing an app which uses the method copyOfRange(byte[] original, int start, int end) from Arrays.copyOfRange().

It has been introduced in only for API 9 and above. But, I have read somewhere that internally it uses System.arraycopy() which has been introduced in API 1 itself.

My question is, in Android, is there a difference in using Arrays.copyOfRange() or System.arraycopy() and if I am able to use System.arraycopy() will it work for lower versions of APIs???

Also, if I could get some sample code on copying a byteArray using System.arraycopy().

Regards.

like image 283
openrijal Avatar asked Dec 01 '22 04:12

openrijal


1 Answers

Arrays.copyOfRange() is just a convenience method for System.arrayCopy()

public class ArraysCompat {
    public byte[] copyOfRange(byte[] from, int start, int end){
        int length = end - start;
        byte[] result = new byte[length];
        System.arraycopy(from, start, result, 0, length);
        return result;
    }
}
like image 106
alex Avatar answered Dec 20 '22 11:12

alex