Here you can find the source of subArray(byte[] a, int beginIndex, int endIndex)
Parameter | Description |
---|---|
a | array data to be copied. |
beginIndex | - the beginning index, inclusive. |
endIndex | - the ending index, exclusive. |
a
. If parameters are not correct, will return new byte[0].
public static byte[] subArray(byte[] a, int beginIndex, int endIndex)
//package com.java2s; /*// w ww .j av a 2 s . c o m * Copyright (c) 2000 - 2005 Nokia Corporation and/or its subsidiary(-ies). * All rights reserved. * This component and the accompanying materials are made available * under the terms of "Eclipse Public License v1.0" * which accompanies this distribution, and is available * at the URL "http://www.eclipse.org/legal/epl-v10.html". * * Initial Contributors: * Nokia Corporation - initial contribution. * * Contributors: * * Description: * */ public class Main { /** * Returns the new array that is the sub-array of the specified array . * The returned array begins at the specified beginIndex and extends to * the byte at index endIndex - 1. Thus the length of the new array is * endIndex-beginIndex. If <code>a</code> is <code>null</code> or empty, * this function returns <code>null</code>. Also returns <code>null</code> * if the beginIndex is negative, or endIndex is larger than the length of * the <code>a</code> array, or beginIndex is larger than endIndex. * * @param a array data to be copied. * @param beginIndex - the beginning index, inclusive. * @param endIndex - the ending index, exclusive. * @return sub-array of the <code>a</code>. If parameters are not correct, * will return new byte[0]. */ public static byte[] subArray(byte[] a, int beginIndex, int endIndex) { if (a == null || a.length == 0 || beginIndex < 0 || endIndex > a.length || beginIndex > endIndex) { return new byte[0]; } else { byte[] b = new byte[endIndex - beginIndex]; System.arraycopy(a, beginIndex, b, 0, b.length); return b; } } }