/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.opensearch.hadoop.util; /* * This file contains code from org.apache.lucene.util.ArrayUtil. * All copyrights apply. * License header for that code follows: */ /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.opensearch.hadoop.OpenSearchHadoopIllegalArgumentException; public abstract class ArrayUtils { /** * Returns true if the two specified array slices of bytes are * equal to one another. Two array slices are considered equal if both * slices contain the same number of elements, and all corresponding pairs * of elements in the two slices are equal. In other words, two slices * are equal if they contain the same elements in the same order. Also, * two slice references are considered equal if both are null.
*
* Adapted from the JVM runtime's Array.equals for use with array slices.
*
* @param a one array to be tested for equality
* @param b the other array to be tested for equality
* @return true if the two arrays are equal
*/
public static boolean sliceEquals(byte[] a, int offa, int lena, byte[] b, int offb, int lenb) {
if (a==b)
return true;
if (a==null || b==null)
return false;
if (lena > a.length || lenb > b.length) {
throw new ArrayIndexOutOfBoundsException("Given length is greater than array length");
}
if (offa >= a.length || offb >= b.length) {
throw new ArrayIndexOutOfBoundsException("Given offset is is out of bounds");
}
if (offa + lena > a.length || offb + lenb > b.length) {
throw new ArrayIndexOutOfBoundsException("Given offset and length is out of array bounds");
}
if (lenb != lena)
return false;
for (int i=0; i