Java Array Sub Array subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)

Here you can find the source of subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)

Description

Produces a new byte array containing the elements between the start and end indices.

The start index is inclusive, the end index exclusive.

License

Apache License

Parameter

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.

Return

a new array containing the elements between the start and end indices.

Declaration

static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) 

Method Source Code

//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 (&lt;0)
     *      is promoted to 0, overvalue (&gt;array.length) results
     *      in an empty array.
     * @param endIndexExclusive  elements up to endIndex-1 are present in the
     *      returned subarray. Undervalue (&lt; startIndex) produces
     *      empty array, overvalue (&gt;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;
    }
}

Related

  1. subarray(boolean[] array, int fromIndex, int length)
  2. subArray(byte[] a, int beginIndex, int endIndex)
  3. subArray(byte[] array, int beginIndex, int endIndex)
  4. subarray(byte[] array, int offset, int length)
  5. subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)
  6. subArray(byte[] b, int offset, int length)
  7. subarray(byte[] b, int ofs, int len)
  8. subArray(byte[] byteArray, int beginIndex, int length)
  9. subArray(byte[] data, int offset, int length)