openrijal
openrijal

Reputation: 583

Using Arrays.copyOfRange() for below API 9

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.

Upvotes: 4

Views: 3299

Answers (1)

alex
alex

Reputation: 6409

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;
    }
}

Upvotes: 20

Related Questions