class SegmentsUtil {

	public static int find(
		MemorySegment[] segments1, int offset1, int numBytes1,
		MemorySegment[] segments2, int offset2, int numBytes2) {
		if (numBytes2 == 0) { // quick way 1.
			return offset1;
		}
		if (inFirstSegment(segments1, offset1, numBytes1) &&
			inFirstSegment(segments2, offset2, numBytes2)) {
			byte first = segments2[0].get(offset2);
			int end = numBytes1 - numBytes2 + offset1;
			for (int i = offset1; i <= end; i++) {
				// quick way 2: equal first byte.
				if (segments1[0].get(i) == first &&
					segments1[0].equalTo(segments2[0], i, offset2, numBytes2)) {
					return i;
				}
			}
			return -1;
		} else {
			return findInMultiSegments(segments1, offset1, numBytes1, segments2, offset2, numBytes2);
		}
	}

}