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

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

Description

NOTE: subarray method implemented for arrays of type byte subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array.

License

Open Source License

Declaration

public static byte[] subarrayEnd(byte[] array, int offset) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w .  j  a  va  2s  .  co m*/
 * <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
      *
      * subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array. Similar to the System.arrayCopy method.
     */
    public static byte[] subarrayEnd(byte[] array, int offset) {
        return subarray(array, offset, array.length - offset);
    }

    /**
     * NOTE: subarray method implemented for arrays of type short
      *
      * subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array. Similar to the System.arrayCopy method.
     */
    public static short[] subarrayEnd(short[] array, int offset) {
        return subarray(array, offset, array.length - offset);
    }

    /**
     * NOTE: subarray method implemented for arrays of type int
      *
      * subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array. Similar to the System.arrayCopy method.
     */
    public static int[] subarrayEnd(int[] array, int offset) {
        return subarray(array, offset, array.length - offset);
    }

    /**
     * NOTE: subarray method implemented for arrays of type long
      *
      * subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array. Similar to the System.arrayCopy method.
     */
    public static long[] subarrayEnd(long[] array, int offset) {
        return subarray(array, offset, array.length - offset);
    }

    /**
     * NOTE: subarray method implemented for arrays of type float
      *
      * subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array. Similar to the System.arrayCopy method.
     */
    public static float[] subarrayEnd(float[] array, int offset) {
        return subarray(array, offset, array.length - offset);
    }

    /**
     * NOTE: subarray method implemented for arrays of type double
      *
      * subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array. Similar to the System.arrayCopy method.
     */
    public static double[] subarrayEnd(double[] array, int offset) {
        return subarray(array, offset, array.length - offset);
    }

    /**
     * NOTE: subarray method implemented for arrays of type char
      *
      * subarray method that makes an array copy of the original array starting at offset and ending at the end of the original array. Similar to the System.arrayCopy method.
     */
    public static char[] subarrayEnd(char[] array, int offset) {
        return subarray(array, offset, array.length - offset);
    }

    /**
      * 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. subArray(T[] array, int start, int end)
  2. subArray(T[] array, int start, int end)
  3. subArray(T[] array, int startIndex, int endIndex)
  4. subArray(T[] origin, int start, int length)
  5. subarrayChar2Double(char[] orig, int off, int len)
  6. subarrayEquals(int[] array, int[] subarray, int startIndex)
  7. subarrayOf(final String[] array, final int start, final int end)
  8. subByteArray(byte[] array, int offset, int length)
  9. subset(boolean[] array, int start, int end)