package com.yonge.audio.utils;

public class ArrayUtil {

	/**
	 * 合并2个数组
	 * @param bt1
	 * @param bt2
	 * @return bt1 + bt2
	 */
	public static byte[] mergeByte(byte[] bt1, byte[] bt2) {
		if (bt2.length == 0) {
			return bt1;
		}

		byte[] bt3 = new byte[bt1.length + bt2.length];

		if (bt1.length > 0) {
			System.arraycopy(bt1, 0, bt3, 0, bt1.length);
		}

		if (bt2.length > 0) {
			System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
		}

		return bt3;
	}

	/**
	 * 根据指定的起始、结束为止提取数组中的数据(起止都包含)，并返回
	 * @param src
	 * @param startIndex
	 * @param endIndex
	 * @return
	 */
	public static byte[] extractByte(byte[] src, int startIndex, int endIndex) {

		if (startIndex > endIndex) {
			throw new RuntimeException("结束索引[" + endIndex + "]不能小于起始索引[" + startIndex + "]");
		}

		byte[] target = new byte[endIndex - startIndex + 1];
		System.arraycopy(src, startIndex, target, 0, target.length);

		return target;
	}

	/**
	 * 合并2个数组
	 * @param bt1
	 * @param bt2
	 * @return bt1 + bt2
	 */
	public static float[] mergeFloat(float[] bt1, float[] bt2) {
		if (bt2.length == 0) {
			return bt1;
		}

		float[] bt3 = new float[bt1.length + bt2.length];

		if (bt1.length > 0) {
			System.arraycopy(bt1, 0, bt3, 0, bt1.length);
		}

		if (bt2.length > 0) {
			System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
		}

		return bt3;
	}

	/**
	 * 根据指定的起始、结束为止提取数组中的数据(起止都包含)，并返回
	 * @param src
	 * @param startIndex
	 * @param endIndex
	 * @return
	 */
	public static float[] extractFloat(float[] src, int startIndex, int endIndex) {
		if (startIndex > endIndex) {
			throw new RuntimeException("结束索引[" + endIndex + "]不能小于起始索引[" + startIndex + "]");
		}

		float[] target = new float[endIndex - startIndex + 1];
		System.arraycopy(src, startIndex, target, 0, target.length);

		return target;
	}

	public static void main(String[] args) {
		byte[] b1 = { 1, 2, 3, 4, 5 };
		//byte[] b2 = { 3, 2, 1 };
		byte[] r = extractByte(b1, 0, 4);
		for (int i = 0; i < r.length; i++) {
			System.out.println(r[i]);
		}
	}
}
