All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gc.iotools.stream.utils.ArrayTools Maven / Gradle / Ivy

package com.gc.iotools.stream.utils;

/*
 * Copyright (c) 2008,2009 Davide Simonetti.
 * This source code is released under the BSD License.
 */

/**
 * Miscellaneous utilities for Arrays, i haven't found anywhere.
 * 
 * @author dvd.smnt
 * @since 1.0.9
 */
public final class ArrayTools {
	/**
	 * Find the index of the contained array in the src array.
	 * 
	 * @param src
	 * @param contained
	 * @return
	 */
	public static int indexOf(final byte[] src, final byte[] contained) {
		if (src == null) {
			throw new IllegalArgumentException("Source array can not be null");
		}
		int result = -1;
		if (src.length >= contained.length) {
			boolean found = false;
			int pos = 0;
			for (; (pos <= (src.length - contained.length)) && !found; pos++) {
				boolean equals = true;
				for (int j = 0; (j < contained.length) && equals; j++) {
					equals = (src[pos + j] == contained[j]);
					found = (j == (contained.length - 1)) && equals;
				}
			}
			result = (found ? (pos - 1) : -1);
		}
		return result;
	}

	/**
	 * utility class, shouldn't be instantiated.
	 */
	private ArrayTools() {

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy