Java Array Starts With startsWith(byte[] source, byte[] match)

Here you can find the source of startsWith(byte[] source, byte[] match)

Description

Does this byte array begin with match array content?

License

Educational Community License

Parameter

Parameter Description
source Byte array to examine
match Byte array to locate in <code>source</code>

Return

true If the starting bytes are equal

Declaration

public static boolean startsWith(byte[] source, byte[] match) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
 *                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
 *
 * Licensed under the Educational Community License Version 1.0 (the "License");
 * By obtaining, using and/or copying this Original Work, you agree that you have read,
 * understand, and will comply with the terms and conditions of the Educational Community License.
 * You may obtain a copy of the License at:
 *
 *      http://cvs.sakaiproject.org/licenses/license_1_0.html
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *//* ww w  .j av a2 s  . com*/

public class Main {
    /**
     * Does this byte array begin with match array content?
     * 
     * @param source
     *            Byte array to examine
     * @param match
     *            Byte array to locate in <code>source</code>
     * @return true If the starting bytes are equal
     */
    public static boolean startsWith(byte[] source, byte[] match) {
        return startsWith(source, 0, match);
    }

    /**
     * Does this byte array begin with match array content? The check is NOT case sensitive!
     * 
     * @param source
     *            Byte array to examine
     * @param offset
     *            An offset into the <code>source</code> array
     * @param match
     *            Byte array to locate in <code>source</code>
     * @return true If the starting bytes are equal
     */
    public static boolean startsWith(byte[] source, int offset, byte[] match) {

        if (match.length > (source.length - offset)) {
            return false;
        }

        for (int i = 0; i < match.length; i++) {
            if (source[offset + i] != match[i]) {
                // ignore cases
                if (source[offset + i] >= 65 && source[offset + i] <= 90 && source[offset + i] + 32 == match[i]) {
                    continue;
                } else if (source[offset + i] >= 97 && source[offset + i] <= 122
                        && source[offset + i] - 32 == match[i]) {
                    continue;
                } else
                    return false;
            }
        }
        return true;
    }
}

Related

  1. startsWith(byte[] checkMe, byte[] maybePrefix)
  2. startsWith(byte[] haystack, byte[] needle)
  3. startsWith(byte[] nameRaw, byte[] namePrefix)
  4. startsWith(byte[] p, byte id)
  5. startsWith(byte[] prefix, byte[] source)
  6. startsWith(byte[] source, byte[] match)
  7. startsWith(byte[] source, int offset, byte[] match)
  8. startsWith(byte[] target, byte[] search, int offset)
  9. startsWith(byte[] target, int offset, byte[] litmusPaper)