Java Array Start With startWith(final byte[] aBuffer, final byte[] aPrefix)

Here you can find the source of startWith(final byte[] aBuffer, final byte[] aPrefix)

Description

start With

License

Open Source License

Parameter

Parameter Description
aBuffer a parameter
aPrefix a parameter

Declaration

public static boolean startWith(final byte[] aBuffer,
        final byte[] aPrefix) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 www.isandlatech.com (www.isandlatech.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  ww. j av a  2 s .c o m
 *    ogattaz (isandlaTech) - initial API and implementation
 *******************************************************************************/

public class Main {
    public static final String HEXA_BUFFER_DIGITS = ".x0123456789ABCDEF";
    private static final String HEXA_PREFIX = ".x";

    /**
     * @param aBuffer
     * @param aPrefix
     * @return
     */
    public static boolean startWith(final byte[] aBuffer,
            final byte[] aPrefix) {

        if (aBuffer == null || aPrefix == null
                || aBuffer.length < aPrefix.length) {
            return false;
        }

        int wMax = aPrefix.length;
        int wI = 0;
        while (wI < wMax) {
            if (aBuffer[wI] != aPrefix[wI]) {
                return false;
            }
            wI++;
        }

        return true;
    }

    /**
     * @param aBuffer
     * @param aHexaPrefix
     * @return
     */
    public static boolean startWith(final byte[] aBuffer,
            final String aHexaPrefix) {
        return startWith(aBuffer, hexaBufferToBytes(aHexaPrefix));
    }

    /**
     * @param aHexaBuffer
     * @return
     * @throws IllegalArgumentException
     */
    public static byte[] hexaBufferToBytes(final String aHexaBuffer)
            throws IllegalArgumentException {
        /*
         * test complet du buffer
         */
        testHexaBufferBytes(aHexaBuffer, true);

        int wStreamSize = aHexaBuffer.length() / 4;

        byte[] wBytes = new byte[wStreamSize];

        String wHexa;
        int wOffsetNextDot;
        int wOffsetDot = 0;
        int wI = 0;
        while (wI < wStreamSize) {
            wOffsetNextDot = aHexaBuffer.indexOf('.', wOffsetDot + 1);
            if (wOffsetNextDot == -1) {
                wOffsetNextDot = aHexaBuffer.length();
            }

            wHexa = aHexaBuffer.substring(wOffsetDot + 2, wOffsetNextDot)
                    .toLowerCase();

            try {
                wBytes[wI] = (byte) (Integer.parseInt(wHexa, 16) & 0x000000FF);
            } catch (Exception e) {
                throw new IllegalArgumentException(
                        "Can't parse hexa value [" + wHexa
                                + "] localized at offset ["
                                + (wOffsetDot + 2) + "] in hexaBuffer ["
                                + aHexaBuffer + "]");
            }
            wOffsetDot = wOffsetNextDot;
            wI++;
        }

        return wBytes;
    }

    /**
     * 
     * @param aHexaBuffer
     * @param aThrowingException
     * @return true si la chaine est de la forme ".x00.x01.xE5 etc..."
     * @throws IllegalArgumentException
     */
    private static boolean testHexaBufferBytes(final String aHexaBuffer,
            final boolean aThrowingException)
            throws IllegalArgumentException {
        if (aHexaBuffer == null) {
            if (aThrowingException) {
                throwBadHexaBufferBytes("The buffer is null.");
            } else {
                return false;
            }
        }
        if ((aHexaBuffer.length() % 4) != 0) {
            if (aThrowingException) {
                throwBadHexaBufferBytes("The buffer length is ["
                        + aHexaBuffer.length() + "]");
            } else {
                return false;
            }
        }
        if (!aHexaBuffer.startsWith(HEXA_PREFIX)) {
            if (aThrowingException) {
                throwBadHexaBufferBytes("The buffer des not start with ["
                        + HEXA_PREFIX + "].");
            } else {
                return false;
            }
        }

        return testCharsOfHexaBufferBytes(aHexaBuffer, aThrowingException);
    }

    /**
     * @param data
     * @param pattern
     * @return
     */
    public static int indexOf(final byte[] data, final byte[] pattern) {
        return indexOf(data, 0, data.length, pattern);
    }

    /**
     * @param data
     * @param aOffset
     * @param aMax
     * @param pattern
     * @return
     */
    public static int indexOf(final byte[] data, final int aOffset,
            final int aLen, final byte[] pattern) {
        if (aLen < 1 || aOffset < 0) {
            return -1;
        }
        int[] failure = computeFailure(pattern);
        int j = 0;
        int wMax = aOffset + aLen;
        for (int i = aOffset; i < wMax; i++) {
            while (j > 0 && pattern[j] != data[i]) {
                j = failure[j - 1];
            }
            if (pattern[j] == data[i]) {
                j++;
            }
            if (j == pattern.length) {
                return i - pattern.length + 1;
            }
        }
        return -1;
    }

    /**
     * @throws IllegalArgumentException
     */
    private static void throwBadHexaBufferBytes(final String aMess)
            throws IllegalArgumentException {
        String wMess = "Hexa string length should be at least a multiple of 4 characters like \".xFF.x0A\";";
        if (aMess != null && aMess.length() > 0) {
            wMess += aMess;
        }
        throw new IllegalArgumentException(wMess);
    }

    /**
     * 
     * @param aHexaBuffer
     * @param aThrowingException
     * @return
     * @throws IllegalArgumentException
     */
    private static boolean testCharsOfHexaBufferBytes(
            final String aHexaBuffer, final boolean aThrowingException)
            throws IllegalArgumentException {
        int wMax = aHexaBuffer.length();
        char wChar;
        int wI = 0;
        while (wI < wMax) {
            wChar = aHexaBuffer.charAt(wI);
            if (HEXA_BUFFER_DIGITS.indexOf(wChar) < 0) {
                if (aThrowingException) {
                    throwBadHexaBufferBytes("At the offset [" + wI
                            + "], the buffer contains the char [" + wChar
                            + "]  which is not a valid character ("
                            + HEXA_PREFIX + ").");
                } else {
                    return false;
                }
            }
            wI++;
        }
        return true;
    }

    /**
     * Computes the failure function using a boot-strapping process, where the
     * pattern is matched against itself.
     * 
     * @param pattern
     * @return
     */
    private static int[] computeFailure(final byte[] pattern) {
        int[] failure = new int[pattern.length];
        int j = 0;
        for (int i = 1; i < pattern.length; i++) {
            // System.out.println("i: " + i);
            while (j > 0 && pattern[j] != pattern[i]) {
                // System.out.println("j: " + j);
                j = failure[j - 1];
            }
            if (pattern[j] == pattern[i]) {
                j++;
            }
            failure[i] = j;
        }
        return failure;
    }
}

Related

  1. arrayStartsWith(final byte[] array, final byte[] str)
  2. arrayStartsWith(V[] pattern, V[] data)
  3. startWith(byte[] data, int start, byte[] find)
  4. startWith(byte[] startWith, byte[] bytes)
  5. startWith(char[] value, char c)
  6. startWith(String t, String[] prefix)