Here you can find the source of subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)
Produces a new byte array containing the elements between the start and end indices.
The start index is inclusive, the end index exclusive.
Parameter | Description |
---|---|
array | the array |
startIndexInclusive | the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in an empty array. |
endIndexExclusive | elements up to endIndex-1 are present in the returned subarray. Undervalue (< startIndex) produces empty array, overvalue (>array.length) is demoted to array length. |
static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)
//package com.java2s; //License from project: Apache License public class Main { /**/*from ww w . ja va 2 s .c om*/ * An empty immutable {@code byte} array. */ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; /** * <p>Produces a new {@code byte} array containing the elements * between the start and end indices.</p> * * <p>The start index is inclusive, the end index exclusive. * Null array input produces null output.</p> * * @param array the array * @param startIndexInclusive the starting index. Undervalue (<0) * is promoted to 0, overvalue (>array.length) results * in an empty array. * @param endIndexExclusive elements up to endIndex-1 are present in the * returned subarray. Undervalue (< startIndex) produces * empty array, overvalue (>array.length) is demoted to * array length. * @return a new array containing the elements between * the start and end indices. * @since 2.1 */ static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) { if (array == null) { return null; } if (startIndexInclusive < 0) { startIndexInclusive = 0; } if (endIndexExclusive > array.length) { endIndexExclusive = array.length; } int newSize = endIndexExclusive - startIndexInclusive; if (newSize <= 0) { return EMPTY_BYTE_ARRAY; } byte[] subarray = new byte[newSize]; System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); return subarray; } }