Java Array Sub Array subarray(byte[] array, int offset, int length)

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

Description

NOTE: subarray method implemented for arrays of type byte Extracts a sub array from an array of a primitive type.

License

Open Source License

Parameter

Parameter Description
array array to extract from
offset offset in input array to start from
length length of the new sub array

Return

the sub array

Declaration

public static byte[] subarray(byte[] array, int offset, int length) 

Method Source Code

//package com.java2s;
/**//from w  w w .  ja  v a2s.c  om
 * <PRE>
 * Name   : com.solidmatrix.regxmaker.util.shared.ArrayUtils
 * Project: RegXmaker Library
 * Version: 1.1
 * Tier   : N/A (Function Class)
 * Author : Gennadiy Shafranovich
 * Purpose: General utilities for array searching and matching
 *
 * Copyright (C) 2001, 2004 SolidMatrix Technologies, Inc.
 * This file is part of RegXmaker Library.
 *
 * RegXmaker Library is is free software; you can redistribute it and/or modify
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * RegXmaker library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Comments: Full, with javadoc.
 *
 * Modification History
 *
 * 02-19-2001 GS Created
 *
 * 02-19-2001 GS Ready for testing
 *
 * 02-23-2001 GS added byte to boolean convertion methods for patch library
 *
 * 06-18-2001 GS Fixed possible error in indexOf() methods.
 *
 * 06-21-2001 GS Fixed bug in indexOf() method. Values seemed to be
 *               offset by one so -1 was inserted in many conditional
 *               statements.
 *
 * 07-01-2001 GS Fixed bug in indexOf() method that cause an infinite
 *               loop while searching.
 *
 * 07-05-2004 YS Added licensing information
 * </PRE>
 */

public class Main {
    /**
      * NOTE: subarray method implemented for arrays of type byte
      *
     * Extracts a sub array from an array of a primitive type.
      * Throws the following exceptions:
      *
      * NullPointerException - if input array is null
      *
      * IllegalArgumentException - if the length of the new array is negative
      *
      * ArrayIndexOutOfBoundsException - If the offset is negative or is larger than array size. OR If offset + length is larger than the array size, making extraction impossible.
     *
     * @param   array array to extract from 
     * @param   offset  offset in input array to start from
     * @param   length  length of the new sub array
     * @return  the sub array
     */
    public static byte[] subarray(byte[] array, int offset, int length) {
        validateOffsetLength(array, array.length, offset, length);

        byte[] sub = new byte[length];
        for (int i = 0; i < length; i++)
            sub[i] = array[offset + i];

        return sub;
    }

    /**
      * NOTE: subarray method implemented for arrays of type short
      *
     * Extracts a sub array from an array of a primitive type.
      * Throws the following exceptions:
      *
      * NullPointerException - if input array is null
      *
      * IllegalArgumentException - if the length of the new array is negative
      *
      * ArrayIndexOutOfBoundsException - If the offset is negative or is larger than array size. OR If offset + length is larger than the array size, making extraction impossible.
     *
     * @param   array array to extract from 
     * @param   offset  offset in input array to start from
     * @param   length  length of the new sub array
     * @return  the sub array
     */
    public static short[] subarray(short[] array, int offset, int length) {
        validateOffsetLength(array, array.length, offset, length);

        short[] sub = new short[length];
        for (int i = 0; i < length; i++)
            sub[i] = array[offset + i];

        return sub;
    }

    /**
      * NOTE: subarray method implemented for arrays of type int
      *
     * Extracts a sub array from an array of a primitive type.
      * Throws the following exceptions:
      *
      * NullPointerException - if input array is null
      *
      * IllegalArgumentException - if the length of the new array is negative
      *
      * ArrayIndexOutOfBoundsException - If the offset is negative or is larger than array size. OR If offset + length is larger than the array size, making extraction impossible.
     *
     * @param   array array to extract from 
     * @param   offset  offset in input array to start from
     * @param   length  length of the new sub array
     * @return  the sub array
     */
    public static int[] subarray(int[] array, int offset, int length) {
        validateOffsetLength(array, array.length, offset, length);

        int[] sub = new int[length];
        for (int i = 0; i < length; i++)
            sub[i] = array[offset + i];

        return sub;
    }

    /**
      * NOTE: subarray method implemented for arrays of type long
      *
     * Extracts a sub array from an array of a primitive type.
      * Throws the following exceptions:
      *
      * NullPointerException - if input array is null
      *
      * IllegalArgumentException - if the length of the new array is negative
      *
      * ArrayIndexOutOfBoundsException - If the offset is negative or is larger than array size. OR If offset + length is larger than the array size, making extraction impossible.
     *
     * @param   array array to extract from 
     * @param   offset  offset in input array to start from
     * @param   length  length of the new sub array
     * @return  the sub array
     */
    public static long[] subarray(long[] array, int offset, int length) {
        validateOffsetLength(array, array.length, offset, length);

        long[] sub = new long[length];
        for (int i = 0; i < length; i++)
            sub[i] = array[offset + i];

        return sub;
    }

    /**
      * NOTE: subarray method implemented for arrays of type float
      *
     * Extracts a sub array from an array of a primitive type.
      * Throws the following exceptions:
      *
      * NullPointerException - if input array is null
      *
      * IllegalArgumentException - if the length of the new array is negative
      *
      * ArrayIndexOutOfBoundsException - If the offset is negative or is larger than array size. OR If offset + length is larger than the array size, making extraction impossible.
     *
     * @param   array array to extract from 
     * @param   offset  offset in input array to start from
     * @param   length  length of the new sub array
     * @return  the sub array
     */
    public static float[] subarray(float[] array, int offset, int length) {
        validateOffsetLength(array, array.length, offset, length);

        float[] sub = new float[length];
        for (int i = 0; i < length; i++)
            sub[i] = array[offset + i];

        return sub;
    }

    /**
      * NOTE: subarray method implemented for arrays of type double
      *
     * Extracts a sub array from an array of a primitive type.
      * Throws the following exceptions:
      *
      * NullPointerException - if input array is null
      *
      * IllegalArgumentException - if the length of the new array is negative
      *
      * ArrayIndexOutOfBoundsException - If the offset is negative or is larger than array size. OR If offset + length is larger than the array size, making extraction impossible.
     *
     * @param   array array to extract from 
     * @param   offset  offset in input array to start from
     * @param   length  length of the new sub array
     * @return  the sub array
     */
    public static double[] subarray(double[] array, int offset, int length) {
        validateOffsetLength(array, array.length, offset, length);

        double[] sub = new double[length];
        for (int i = 0; i < length; i++)
            sub[i] = array[offset + i];

        return sub;
    }

    /**
      * NOTE: subarray method implemented for arrays of type char
      *
     * Extracts a sub array from an array of a primitive type.
      * Throws the following exceptions:
      *
      * NullPointerException - if input array is null
      *
      * IllegalArgumentException - if the length of the new array is negative
      *
      * ArrayIndexOutOfBoundsException - If the offset is negative or is larger than array size. OR If offset + length is larger than the array size, making extraction impossible.
     *
     * @param   array array to extract from 
     * @param   offset  offset in input array to start from
     * @param   length  length of the new sub array
     * @return  the sub array
     */
    public static char[] subarray(char[] array, int offset, int length) {
        validateOffsetLength(array, array.length, offset, length);

        char[] sub = new char[length];
        for (int i = 0; i < length; i++)
            sub[i] = array[offset + i];

        return sub;
    }

    /**
     * Validates an array, offset, and length values to make sure they are
      * valid for extraction of subarray.
     *
     * @param   array  input array
     * @param   arrayLength length of input array
     * @param   offset  offset in input array
     * @param   length  length of the new subaray
     */
    private static void validateOffsetLength(Object array, int arrayLength, int offset, int length) {
        if (array == null)
            throw new NullPointerException("Input array may not be null");

        if (arrayLength < 0)
            throw new IllegalArgumentException("Array length may not be negative");

        if (offset < 0)
            throw new ArrayIndexOutOfBoundsException("Offset may not be negative");

        if (offset >= arrayLength)
            throw new ArrayIndexOutOfBoundsException("Offset may not be larger than input array size");

        if (length < 0)
            throw new IllegalArgumentException("Length of subarray may not be negative");

        if (offset + length > arrayLength)
            throw new ArrayIndexOutOfBoundsException(
                    "Extracted subarray may not go over the size of the main array");
    }
}

Related

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