Table of Content

Copy the range of Byte Array to another array in Java


Declaration :

Explanation :

Purpose The java.util.Arrays.copyOfRange(byte[] original int from int to) method copies the specified range of the specified array into a new array.The final index of the range (to) which must be greater than or equal to from may be greater than original.length in which case (byte)0 is placed in all elements of the copy whose index is greater than or equal to original.length – from. The length of the returned array will be to – from.
Parameters original ===> This is the array from which a range is to to be copied.
from ===> This is the initial index of the range to be copied inclusive.
to ===> This is the final index of the range to be copied exclusive.
Return Value This method returns a new array containing the specified range from the original array truncated or padded with zeros to obtain the required length.
Exception ArrayIndexOutOfBoundsException — If from < 0 or from > original.length()
,IllegalArgumentException — If if from > to.
,NullPointerException — If original is null.

Java Program : Example

Below example will explain java.util.Arrays.copyOfRange() method.

Output of Program :