Java Array Sub Array subArray(final byte[] source, final int start, final int length)

Here you can find the source of subArray(final byte[] source, final int start, final int length)

Description

Works like String.substring()

License

Open Source License

Parameter

Parameter Description
source Source of the byte array
start starting byte
length length of the array to return

Return

the portion of the byte array specified.

Declaration

public static byte[] subArray(final byte[] source, final int start, final int length) 

Method Source Code

//package com.java2s;
/*//ww w .j a v  a  2 s . c  om
 * Copyright (c) 2014 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial API and implementation
 */

public class Main {
    /**
     * Works like String.substring()
     *
     * @param source Source of the byte array
     * @param start starting byte
     * @param length length of the array to return
     *
     * @return the portion of the byte array specified.
     */
    public static byte[] subArray(final byte[] source, final int start, final int length) {
        if (start < 0) {
            throw new ArrayIndexOutOfBoundsException("Start index: " + start);
        }

        if (source == null) {
            throw new IllegalArgumentException("Source array was null");
        }

        if ((start + length) > source.length) {
            throw new ArrayIndexOutOfBoundsException("length index: " + length);
        }

        final byte[] retval = new byte[length];
        System.arraycopy(source, start, retval, 0, length);

        return retval;
    }
}

Related

  1. subArray(double[] arr, int start, int fim)
  2. subArray(double[] array, int startIndex, int length)
  3. subarray(double[] orig, int off, int len)
  4. subarray(double[] orig, int off, int len)
  5. subArray(E[] array, int start)
  6. subArray(final byte[] src, final int srcPos, final int length)
  7. subArray(final char[] source, int from, int to)
  8. subArray(final int[] input, final int start, final int end)
  9. subArray(final long[] array, final int start, final int end)