Java String Match regionMatches(byte[] ba1, int pos1, byte[] ba2, int pos2, int len)

Here you can find the source of regionMatches(byte[] ba1, int pos1, byte[] ba2, int pos2, int len)

Description

Tests if two byte regions are equal.

License

Open Source License

Parameter

Parameter Description
ba1 the first set of bytes
pos1 the starting position within the first set bytes
ba2 the second set of bytes
pos2 the starting position within the second set bytes
len the number of bytes to compare

Return

true if the specified sub-region of the first set of bytes matches the specified sub-region of the second set of bytes false otherwise.

Declaration

public static final boolean regionMatches(byte[] ba1, int pos1,
        byte[] ba2, int pos2, int len) 

Method Source Code

//package com.java2s;
/*//from w w  w .j  av a2  s. c  o  m
 * Copyright SparseWare Inc. All Rights Reserved.
 *
 * Licensed 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.
 */

public class Main {
    /**
     * Tests if two byte regions are equal.
     *
     * @param ba1   the first set of bytes
     * @param pos1  the starting position within the first set bytes
     * @param ba2   the second set of bytes
     * @param pos2  the starting position within the second set bytes
     * @param len   the number of bytes to compare
     *
     * @return   <code>true</code> if the specified sub-region of the first set of bytes
     *           matches the specified sub-region of the second set of bytes <code>false</code>
     *           otherwise.
     */
    public static final boolean regionMatches(byte[] ba1, int pos1,
            byte[] ba2, int pos2, int len) {
        int to = +pos1;
        int po = pos2;

        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((pos2 < 0) || (pos1 < 0) || (pos1 > ((long) ba1.length - len))
                || (pos2 > ((long) ba2.length - len))) {
            return false;
        }

        while (len-- > 0) {
            byte c1 = ba1[to++];
            byte c2 = ba2[po++];

            if (c1 == c2) {
                continue;
            }

            return false;
        }

        return true;
    }
}

Related

  1. regionMatches(boolean ignoreCase, CharSequence thisSeq, int toffset, CharSequence otherSeq, int ooffset, int len)
  2. regionMatches(byte[] subValue, byte[] superValue, int offset)
  3. regionMatches(char[] source, char[] target, int sIndex)
  4. regionMatches(CharSequence a, int i, CharSequence b, int j, int len)
  5. regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length)