Java Array Starts With startsWith(byte[] haystack, byte[] needle)

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

Description

compare byte arrays

License

Open Source License

Parameter

Parameter Description
haystack byte array to search thru looking for the needle
needle what we want to find in the haystack

Return

true if equal

Declaration

public static boolean startsWith(byte[] haystack, byte[] needle) 

Method Source Code

//package com.java2s;
/**/*from w w  w  .  ja v  a2s.c o m*/
 * static byte array utilities
 *
 * Copyright (c) 2004, Pat Farrell, 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. * 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 {
    /**
     * compare byte arrays
     * @return true if equal
     * @param haystack byte array to search thru looking for the needle
     * @param needle what we want to find in the haystack
     */
    public static boolean startsWith(byte[] haystack, byte[] needle) {
        int i;
        for (i = 0; i < needle.length; i++)
            if (needle[i] != haystack[i])
                break;
        return i == needle.length;
    }

    /** compare byte arrays
     * @return true if equal
     * @param offset offset into haystack
     * @param haystack byte array to search thru looking for the needle
     * @param needle what we want to find in the haystack
     */
    public static boolean startsWith(byte[] haystack, int offset, byte[] needle) {
        int i;
        for (i = 0; i < needle.length; i++)
            if (needle[i] != haystack[i + offset])
                break;
        return i == needle.length;
    }
}

Related

  1. startsWith(byte[] array, byte[] startBytes)
  2. startsWith(byte[] bytes, int offset, byte... prefix)
  3. startsWith(byte[] bytes, String str, int offset)
  4. startsWith(byte[] bytes, String text)
  5. startsWith(byte[] checkMe, byte[] maybePrefix)
  6. startsWith(byte[] nameRaw, byte[] namePrefix)
  7. startsWith(byte[] p, byte id)
  8. startsWith(byte[] prefix, byte[] source)
  9. startsWith(byte[] source, byte[] match)