Java Array Starts With startsWith(byte[] p, byte id)

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

Description

A convenience function to determine whether the first non-zero byte is equal to a specific value.

License

Apache License

Parameter

Parameter Description
p - array of bytes to check
id - expected value of the first non-zero byte

Return

whether the first non-zero byte is equal to id

Declaration

public static boolean startsWith(byte[] p, byte id) 

Method Source Code

//package com.java2s;
/*//from  w  w w.  j  av a 2  s  .c  o m
Copyright 2012, Jernej Kovacic
    
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 {
    /**
     * A convenience function to determine whether the first non-zero byte is
     * equal to a specific value. Typically used to determine whether a blob
     * represents an EC point in compressed or uncompressed form.
     * 
     * @param p - array of bytes to check
     * @param id - expected value of the first non-zero byte
     * 
     * @return whether the first non-zero byte is equal to id
     */
    public static boolean startsWith(byte[] p, byte id) {
        // sanity check
        if (null == p || 0 == p.length) {
            return false;
        }

        int start = 0;
        for (start = 0; start < p.length && 0x00 == p[start]; start++)
            ;
        if (p.length <= start) {
            return false;
        }

        return (id == p[start]);
    }
}

Related

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