Package org.chaidb.db.helper

Source Code of org.chaidb.db.helper.ByteTool

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/

package org.chaidb.db.helper;

import java.sql.Time;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Vector;

/**
* This is an utility class to operate byte array.
*/
public class ByteTool {

    /* MSB encoding */
    public static boolean DEFAULT_MSB_FIRST = true;

    /**
     * @param a    a byte array that contains data to be compared
     * @param aOff offset of a
     * @param b    anpther byte array that contains data to be compared
     * @param bOff offset of b
     * @param len  the length to compare
     * @return >0 if a > b, 0 for a == b, <0 for a < b
     */
    public final static int compare(byte[] a, int aOff, byte[] b, int bOff, int len) {
        int l = len;
        if (l > a.length - aOff) l = a.length - aOff;
        if (l > b.length - bOff) l = b.length - bOff;
        for (int i = 0; i < l; i++) {
            if (a[i + aOff] > b[i + bOff]) return 1;
            if (a[i + aOff] < b[i + bOff]) return -1;
        }

        if ((a.length - aOff) > (b.length - bOff)) return 1;
        if ((a.length - aOff) < (b.length - bOff)) return -1;

        return 0;
    }

    /**
     * Compare two ids
     */
    public final static boolean sameId(byte[] id1, byte[] id2) {
        if (id1 == null && id2 == null) return true;
        if (id1 == null || id2 == null) return false;
        if (id1.length != id2.length) return false;
        for (int i = id1.length - 1; i >= 0; i--)
            if (id1[i] != id2[i]) return false;
        return true;
    }

    /**
     * @param ignoreCases the comparison is case sensitive or not
     * @param a           a byte array that contains data to be compared
     * @param b           anpther byte array that contains data to be compared
     * @return <code>true</code> if two byte arrays are the same
     */

    public final static boolean same(boolean ignoreCases, byte[] a, byte[] b) {
        int alen = a.length;
        int blen = b.length;

        int tolower = 32;
        if (alen != blen) {
            return false;
        }

        for (int i = 0; i < alen; i++) {
            if (a[i] == b[i]) continue;
            else {
                if (!ignoreCases) return false;
                else {
                    if (a[i] >= 97) {
                        if (a[i] > 122) {
                            return false;
                        }
                        if (a[i] - tolower != b[i]) return false;
                    } else {
                        if (b[i] >= 97) {
                            if (b[i] > 122) {
                                return false;
                            }
                            if (b[i] - tolower != a[i]) return false;
                        } else return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * Check whether the given item exists in the list.
     */
    public static boolean contains(AbstractList list, byte[] item) {
        if (list == null || item == null) return false;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            byte[] listItem = (byte[]) list.get(i);
            if (ByteTool.same(false, listItem, item)) return true;
        }
        return false;
    }

    /**
     * @return true if the given item is in the list; false otherwise
     */
    public static boolean remove(AbstractList list, byte[] item) {
        if (list == null || item == null) return false;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            byte[] listItem = (byte[]) list.get(i);
            if (ByteTool.same(false, listItem, item)) {
                list.remove(i);
                return true;
            }
        }
        return false;
    }


    /**
     * are this two bytes same - ignoring the case
     *
     * @param b1
     * @param b2
     */
    private static boolean equalIgnoreCase(byte b1, byte b2) {
        int toLower = 32;
        if (b1 == b2) return true;

        if (b1 >= 97) {
            if (b1 > 122) return false;

            if (b1 - toLower != b2) return false;
        } else {
            if (b2 >= 97) {
                if (b2 > 122) return false;

                if (b2 - toLower != b1) return false;
            }
        }
        return true;
    }


    /**
     * @param a    a byte array that contains data to be compared
     * @param aOff offset of a
     * @param b    anpther byte array that contains data to be compared
     * @param bOff offset of b
     * @param len  the length to compare
     * @return >0 if a > b, 0 for a == b, <0 for a < b
     */
    public final static int compareSubArray(byte[] a, int aOff, byte[] b, int bOff, int len) {
        int l = len;
        if (l > a.length - aOff) return 2; // error
        if (l > b.length - bOff) return 2; // error
        for (int i = 0; i < l; i++) {
            if (a[i + aOff] > b[i + bOff]) return 1;
            if (a[i + aOff] < b[i + bOff]) return -1;
        }

        return 0;
    }


    /**
     * Convert four bytes from a byte array into an integer, using
     * LSB or MSB data order, as appropriate.
     *
     * @param ba     byte array
     * @param offset Offset in barray to read integer
     * @return integer formed from indicated bytes
     */
    public static final int bytesToInt(byte[] ba, int offset, boolean msbFirst) {
        if (msbFirst)
            return (((ba[offset] & 0xff) << 24) | ((ba[offset + 1] & 0xff) << 16) | ((ba[offset + 2] & 0xff) << 8) | (ba[offset + 3] & 0xff));
        else
            return ((ba[offset] & 0xff) | ((ba[offset + 1] & 0xff) << 8) | ((ba[offset + 2] & 0xff) << 16) | ((ba[offset + 3] & 0xff) << 24));
    }

    /**
     * Convert 8 bytes to a long using
     * LSB or MSB data order, as appropriate.
     *
     * @param ba     byte array
     * @param offset Offset in barray to read integer
     * @return integer formed from indicated bytes
     */
    public static final long bytesToLong(byte[] ba, int offset, boolean msbFirst) {
        long i1, i2, i3, i4, i5, i6, i7, i8, i9;
        i9 = 0xff;
        if (msbFirst)
/*    return ( ((ba[offset] & 0xff)<<56) |
                ((ba[offset+1]&0xff)<<48)  |
      ((ba[offset+2]& 0xff)<<40) |
            ((long)(ba[offset+3]&0xff) << 32)  |
      ((ba[offset+4]& 0xff)<<24)  |
            ((ba[offset+5]&0xff)<<16)  |
      ((ba[offset+6]& 0xff)<< 8)  |
            (ba[offset+7]&0xff) );
*/ {
            /* modified by marriane 2001-10-24 */
            i1 = (ba[offset] & i9) << 56;
            i2 = (ba[offset + 1] & i9) << 48;
            i3 = (ba[offset + 2] & i9) << 40;
            i4 = (ba[offset + 3] & i9) << 32;
            i5 = (ba[offset + 4] & i9) << 24;
            i6 = (ba[offset + 5] & i9) << 16;
            i7 = (ba[offset + 6] & i9) << 8;
            i8 = ba[offset + 7] & i9;

            return i1 | i2 | i3 | i4 | i5 | i6 | i7 | i8;
        } else {
/*    return ((ba[offset]&0xff) | ((ba[offset+1]&0xff)<<8) |
            ((ba[offset+2]&0xff)<<16) | ((ba[offset+3]&0xff)<<24) |
      ((ba[offset+4]&0xff)<<32) | ((ba[offset+5]&0xff)<<40) |
      ((ba[offset+6]&0xff)<<48) | ((ba[offset+7]&0xff)<<56) );
*/
            i1 = ba[offset] & i9;
            i2 = (ba[offset + 1] & i9) << 8;
            i3 = (ba[offset + 2] & i9) << 16;
            i4 = (ba[offset + 3] & i9) << 24;
            i5 = (ba[offset + 4] & i9) << 32;
            i6 = (ba[offset + 5] & i9) << 40;
            i7 = (ba[offset + 6] & i9) << 48;
            i8 = (ba[offset + 7] & i9) << 56;

            return i1 | i2 | i3 | i4 | i5 | i6 | i7 | i8;
        }
    }

    public static final byte[] intToBytes(int v) {
        return intToBytes(v, DEFAULT_MSB_FIRST);
    }

    public static final byte[] intToBytes(int v, boolean msbFirst) {
        byte[] ret = new byte[4];
        if (msbFirst) {
            ret[0] = (byte) ((v >>> 24) & 0xFF);
            ret[1] = (byte) ((v >>> 16) & 0xFF);
            ret[2] = (byte) ((v >>> 8) & 0xFF);
            ret[3] = (byte) ((v >>> 0) & 0xFF);
        } else {
            ret[3] = (byte) ((v >>> 24) & 0xFF);
            ret[2] = (byte) ((v >>> 16) & 0xFF);
            ret[1] = (byte) ((v >>> 8) & 0xFF);
            ret[0] = (byte) ((v >>> 0) & 0xFF);
        }
        return ret;
    }

    public static final byte[] shortToBytes(short v) {
        byte[] ret = new byte[2];

        ret[0] = (byte) ((v >>> 8) & 0xFF);
        ret[1] = (byte) ((v >>> 0) & 0xFF);

        return ret;
    }

    public static final byte[] longToBytes(long v) {
        byte[] ret = new byte[8];

        ret[0] = (byte) ((v >>> 56) & 0xFF);
        ret[1] = (byte) ((v >>> 48) & 0xFF);
        ret[2] = (byte) ((v >>> 40) & 0xFF);
        ret[3] = (byte) ((v >>> 32) & 0xFF);
        ret[4] = (byte) ((v >>> 24) & 0xFF);
        ret[5] = (byte) ((v >>> 16) & 0xFF);
        ret[6] = (byte) ((v >>> 8) & 0xFF);
        ret[7] = (byte) ((v >>> 0) & 0xFF);

        return ret;
    }

    public static final void intToBytes(byte[] ret, int start, int v) {
        intToBytes(ret, start, v, DEFAULT_MSB_FIRST);
    }

    public static final void intToBytes(byte[] ret, int start, int v, boolean msbFirst) {
        if (msbFirst) {
            ret[start] = (byte) ((v >>> 24) & 0xFF);
            ret[start + 1] = (byte) ((v >>> 16) & 0xFF);
            ret[start + 2] = (byte) ((v >>> 8) & 0xFF);
            ret[start + 3] = (byte) ((v >>> 0) & 0xFF);
        } else {
            ret[start + 3] = (byte) ((v >>> 24) & 0xFF);
            ret[start + 2] = (byte) ((v >>> 16) & 0xFF);
            ret[start + 1] = (byte) ((v >>> 8) & 0xFF);
            ret[start] = (byte) ((v >>> 0) & 0xFF);
        }
    }

    public static final void shortToBytes(byte[] ret, int start, short v) {

        ret[start] = (byte) ((v >>> 8) & 0xFF);
        ret[start + 1] = (byte) ((v >>> 0) & 0xFF);
    }

    public static final void longToBytes(byte[] ret, int start, long v) {

        ret[start] = (byte) ((v >>> 56) & 0xFF);
        ret[start + 1] = (byte) ((v >>> 48) & 0xFF);
        ret[start + 2] = (byte) ((v >>> 40) & 0xFF);
        ret[start + 3] = (byte) ((v >>> 32) & 0xFF);
        ret[start + 4] = (byte) ((v >>> 24) & 0xFF);
        ret[start + 5] = (byte) ((v >>> 16) & 0xFF);
        ret[start + 6] = (byte) ((v >>> 8) & 0xFF);
        ret[start + 7] = (byte) ((v >>> 0) & 0xFF);
    }


    /**
     * Convert two bytes from a byte array into a short.
     *
     * @param ba     byte array
     * @param offset Offset in barray to read short
     * @return short formed from indicated bytes
     */
    public static short bytesToShort(byte[] ba, int offset, boolean msbFirst) {
        if (msbFirst) return (short) (((ba[offset] & 0xff) << 8) | (short) (ba[offset + 1] & 0xff));
        else return (short) ((ba[offset] & 0xff) | ((ba[offset + 1] & 0xff) << 8));
    }


    /**
     * Convert two bytes from a bytes array into a short
     *
     * @param ba     byte array
     * @param offset Offset in barray to read short
     * @return short formed from indicated bytes
     */
    public static short bytesToShort(byte[] ba, int offset) {
        return bytesToShort(ba, offset, DEFAULT_MSB_FIRST);
    }

    /**
     * Convert an unsiged byte(128~255) to a short.
     *
     * @param ubyte an unsiged byte
     * @return short formed from indeicated bytes
     */
    public static short unsignedByteToShort(byte ubyte) {
        return (short) ((ubyte & 0x7f) + (ubyte & 0x80));
    }


    /**
     * Return a hexadecimal representation of the bytes in
     * the given byte array.
     *
     * @param level the indentation.
     * @param bytes the byte array that needs to be converted to
     *              a Hex String.
     * @return String containing the hexadecimal representation of
     *         the given byte array.
     */
    public static String toHexString(int level, byte[] bytes, int off, int len) {
        StringBuffer inds = new StringBuffer();
        for (int i = 0; i < level; i++)
            inds.append("    ");
        String hexString = new String("0123456789ABCDEF");
        StringBuffer res = new StringBuffer();

        for (int i = 0; i < len; i++) {
            if (i % 16 == 0) res.append("\r\n").append(inds.toString());
            res.append(hexString.charAt((bytes[i + off] >> 4) & 0xf));
            res.append(hexString.charAt(bytes[i + off] & 0xf));
            res.append(" ");
        }
        res.append("\r\n");
        return res.toString();
    }

    /**
     * Return a hexadecimal representation of the bytes without formatting
     *
     * @param bytes the byte array that needs to be converted to
     *              a Hex String.
     * @return String containing the hexadecimal representation of
     *         the given byte array.
     */
    public static String hexString(byte[] bytes, int off, int len) {
        String hexString = new String("0123456789ABCDEF");
        StringBuffer res = new StringBuffer();

        for (int i = 0; i < len; i++) {
            res.append(hexString.charAt((bytes[i + off] >> 4) & 0xf));
            res.append(hexString.charAt(bytes[i + off] & 0xf));
        }
        return res.toString();
    }


    public static String toString(byte[] src) {
        if (src == null) return null;
        int length = src.length;
        StringBuffer dst = new StringBuffer();
        for (int i = 0; i < length; i++) {
            dst.append((char) src[i]);
        }
        return dst.toString();
    }

    /**
     * returns a list of candidate values in byte[]
     *
     * @param value The string to be operated on it. It is a byte[].
     */
    public static AbstractList buildSubstrings(byte[] value, short subStringLength) {
        ArrayList result = null;
        if (value.length < subStringLength) {
            // just add value itself
            result = new ArrayList(1);
            result.add(value);
            return result;
        }
        int size = value.length - subStringLength + 1 + 2 + 1;
        result = new ArrayList(size);
        byte[] first = new byte[subStringLength];
        byte[] last = new byte[subStringLength];
        first[0] = 0; // ###
        System.arraycopy(value, 0, first, 1, subStringLength - 1);
        System.arraycopy(value, value.length - subStringLength + 1, last, 0, subStringLength - 1);
        last[subStringLength - 1] = 0; // ###
        result.add(value);
        result.add(first);
        result.add(last);

        for (int i = 0; i <= (value.length - subStringLength); i++) {
            byte[] internal = new byte[subStringLength];
            System.arraycopy(value, i, internal, 0, subStringLength);
            result.add(internal);
        }
        return result;
    }

    /**
     * returns a list of candidate values in byte[]
     *
     * @param value The string to be operated on it.
     */
    public static AbstractList buildSubstrings(String value, short subStringLength) {
        ArrayList result = null;
        if (value.length() < subStringLength) {
            // just add value itself
            result = new ArrayList(1);
            result.add(value.getBytes());
            return result;
        }

        int size = value.length() - subStringLength + 1 + 2 + 1;
        result = new ArrayList(size);
        // special char for the beginning
        StringBuffer first = new StringBuffer("^");
        first.append(value.substring(0, subStringLength - 1));
        StringBuffer last = new StringBuffer();
        last.append(value.substring(value.length() - subStringLength + 1));
        last.append("$"); // special char for the ending
        result.add(value.getBytes());
        result.add(first.toString().getBytes());
        result.add(last.toString().getBytes());

        for (int i = 0; i <= (value.length() - subStringLength); i++) {
            String internal = value.substring(i, i + subStringLength);
            result.add(internal.getBytes());
        }
        return result;
    }

    /**
     * returns a list of candidate values in byte[]
     *
     * @param value The string to be operated on it. It is a byte[].
     */
    public static AbstractList buildWordlists(String value) {
        AbstractList words = Util.splits(value, ' ');
        int word_size = words.size();

        ArrayList result = new ArrayList(word_size);

        for (int i = 0; i < word_size; i++)
            result.add(((String) words.get(i)).getBytes());

        return result;
    }

    /**
     * returns a list of candidate values in byte[]
     *
     * @param value The string to be operated on it. It is a byte[].
     */
    public static AbstractList buildInternalSubstrings(byte[] value, short subStringLength) {
        if (value.length < subStringLength) return null; // should the caller check?
        int size = value.length - subStringLength;
        ArrayList result = new ArrayList(size);

        for (int i = 0; i <= (value.length - subStringLength); i++) {
            byte[] internal = new byte[subStringLength];
            System.arraycopy(value, i, internal, 0, subStringLength);
            result.add(internal);
        }
        return result;
    }

    /**
     * returns a list of candidate values in byte[]
     *
     * @param value The string to be operated on it. It is a byte[].
     */
    public static AbstractList buildInternalSubstrings(String value, short subStringLength) {
        if (value.length() < subStringLength) return null; // should the caller check?
        int size = value.length() - subStringLength;
        ArrayList result = new ArrayList(size);

        for (int i = 0; i <= (value.length() - subStringLength); i++) {
            String internal = value.substring(i, i + subStringLength);
            result.add(internal.getBytes());
        }
        return result;
    }

    /**
     * Returns the index within this string of the first occurrence of the
     * specified substring, starting at the specified index.
     *
     * @param src       the source string to search for
     * @param str       the substring to search for.
     * @param fromIndex the index to start the search from.
     * @return If the str argument occurs as a substring within src
     *         at a starting index no smaller than <code>fromIndex</code>,
     *         then the index of the first character
     *         of the first such substring is returned; otherwise,
     *         <code>-1</code> is returned.
     */
    public static final int indexOf(byte[] src, int fromIndex, byte[] str) {
        byte v1[] = src;
        byte v2[] = str;
        int max = (src.length - str.length);
        if (fromIndex < 0) {
            fromIndex = 0;
        } else if (fromIndex >= src.length) {
            // Note: fromIndex might be near -1>>>1.
            return -1;
        }
        test:
        for (int i = ((fromIndex < 0) ? 0 : fromIndex); i <= max; i++) {
            int n = str.length;
            int j = i;
            int k = 0;
            while (n-- != 0) {
                if (v1[j++] != v2[k++]) {
                    continue test;
                }
            }
            return i;
        }
        return -1;
    }

    public static final int indexOf(byte[] src, byte[] str) {
        return indexOf(src, 0, str);
    }

    public static final int indexOf(byte[] src, int fromIndex, byte b) {
        int max = src.length - 1;
        if (fromIndex < 0) {
            fromIndex = 0;
        } else if (fromIndex >= src.length) {
            // Note: fromIndex might be near -1>>>1.
            return -1;
        }

        for (int i = ((fromIndex < 0) ? 0 : fromIndex); i <= max; i++) {
            if (src[i] != b) {
                continue;
            }
            return i;
        }
        return -1;
    }

    /**
     * Tests if the byte array starts with the specified prefix.
     *
     * @param src       the original byte array
     * @param prefix    the prefix.
     * @param srcOffset where to begin looking in the original.
     * @return <code>true</code> if the prefix byte array  is a prefix of src
     *         starting at index <code>srcOffset</code>;<code>false</code> otherwise.
     */
    public static final boolean startsWith(byte[] src, int srcOffset, byte[] prefix) {
        int to = srcOffset;
        int po = 0;
        int pc = prefix.length;

        // Note: toffset might be near -1>>>1.
        if ((srcOffset < 0) || (srcOffset > src.length - pc)) {
            return false;
        }


        while (--pc >= 0) {
            if (src[to++] != prefix[po++]) {
                return false;
            }
        }


        return true;
    }


    /**
     * Tests if the byte array starts with the specified prefix. This method
     * will ignore the case for comparision purpose
     *
     * @param src       the original byte array
     * @param prefix    the prefix.
     * @param srcOffset where to begin looking in the original.
     * @return <code>true</code> if the prefix byte array  is a prefix of src
     *         starting at index <code>srcOffset</code>;<code>false</code> otherwise.
     */
    public static final boolean startsWithIgnoreCase(byte[] src, int srcOffset, byte[] prefix) {
        int to = srcOffset;
        int po = 0;
        int pc = prefix.length;

        // Note: toffset might be near -1>>>1.
        if ((srcOffset < 0) || (srcOffset > src.length - pc)) {
            return false;
        }

        while (--pc >= 0) {
            return equalIgnoreCase(src[to++], prefix[po++]);
        }
        return true;
    }


    public static final boolean startsWith(byte[] src, byte[] prefix) {
        return startsWith(src, 0, prefix);
    }

    /**
     * public static final boolean
     * endsWith(byte[] src, int srcOffset, byte[] suffix) {
     * return startsWith(src, srcOffset - suffix.length, suffix );
     * }
     */

    public static final boolean endsWith(byte[] src, byte[] suffix) {
        if (src.length < suffix.length) return false;
        return startsWith(src, src.length - suffix.length, suffix);
    }

    /*
     *   This method will ignore the case while comparing
     *
     *
    */
    public static final boolean endsWithIgnoreCase(byte[] src, byte[] suffix) {
        if (src.length < suffix.length) return false;
        return startsWithIgnoreCase(src, src.length - suffix.length, suffix);
    }


    /**
     * Tests if the received byte is a digit.
     *
     * @param src the original byte
     * @return <code>true</code> if the received byte is a digit in ASCII
     */

    public static final boolean isDigit(byte src) {
        if ((src >= 48) && (src <= 57)) return true;
        else return false;
    }

    /**
     * Test if the received byte array is a number.
     *
     * @param src the original byte array
     * @return <code>true</code> if the received byte array is a number in ASCII
     */
    public final static boolean isNumber(byte[] src) {
        if (src.length == 0) {
            return false;
        }
        for (int i = 0; i < src.length; i++) {
            if ((src[i] < 48) || (src[i] > 57)) {
                return false;
            }
        }
        return true;
    }


    /**
     * Tests if two byte[] have the same content
     *
     * @param ignoreCase comapre Case or not
     * @param bOff       offset in byte[] b
     * @param aOff       offset in byte[] a
     * @param len        length of comparison
     */

    public final static boolean regionMatches(boolean ignoreCase, byte[] a, int aOff, byte[] b, int bOff, int len) {
        int tolower = 32;

        int alen = a.length;
        int blen = b.length;
        if ((aOff < 0) || (bOff < 0) || (aOff > alen - len) || (bOff > blen - len)) return false;

        for (int i = 0; i < len; i++) {
            int tempaoff = i + aOff;
            int tempboff = i + bOff;
            if (a[tempaoff] == b[tempboff]) continue;
            else {
                if (!ignoreCase) return false;
                else {
                    if (a[tempaoff] >= 97) {
                        if (a[tempaoff] > 122) {
                            return false;
                        }
                        if (a[tempaoff] - tolower != b[tempboff]) return false;
                    } else {
                        if ((b[tempboff] >= 97) || (b[tempboff] <= 122)) {
                            if (b[tempboff] - tolower != a[tempaoff]) return false;
                        } else return false;
                    }
                }
            }
        }
        return true;
    }


    /**
     * Convert the input byte array into lower cases.
     *
     * @param src the original byte
     */


    public final static void toLowerCase(byte[] src) {
        int len = src.length;

        for (int i = 0; i < len; i++) {
            if ((src[i] >= 65) && (src[i] <= 90)) {
                src[i] += 32;
            }
        }
        return;
    }

    public final static byte[] toLowerCase2(byte[] src) {
        int len = src.length;
        byte[] dst = src;

        for (int i = 0; i < len; i++) {
            if ((src[i] >= 65) && (src[i] <= 90)) {
                dst[i] = (byte) (src[i] + 32);
            }
        }
        return dst;

    }


    public final static void toLowerCase(byte[] src, byte[] dest) {
        int len = src.length;

        for (int i = 0; i < len; i++) {
            dest[i] = src[i];
            if ((dest[i] >= 65) && (dest[i] <= 90)) {
                dest[i] += 32;
            }
        }
        return;
    }

    /**
     * Tests if the input vector contains a certain byte array or not
     *
     * @param vec vector
     * @param ba  byte array
     * @return <code> true </code> if the vector contains the byte array
     */
    public final static boolean contains(Vector vec, byte[] ba) {
        int vecsize = vec.size();

        for (int i = 0; i < vecsize; i++) {
            int j = 0;
            byte[] temp = (byte[]) vec.elementAt(i);

            int alen = temp.length;
            int blen = ba.length;

            if (alen != blen) j = alen + 1;
            for (; j < alen; j++) {
                if (temp[j] != ba[j]) break;
            }
            if (j == alen) return true;
        }

        return false;
    }

    /**
     * Tests if an byte[][] contains a byte[]
     */
    public final static boolean contains(byte[][] values, byte[] val) {
        for (int i = 0; i < values.length; i++)
            if (compare(values[i], 0, val, 0, val.length) == 0) return true;
        return false;
    }

    /**
     * Append another byte array to the end of the original byte array
     *
     * @param src    the original byte array
     * @param values the appending byte array
     * @return <code> byte[] </code> with the added values
     */
    public final static byte[] append(byte[] src, byte[] values) {
        int lengthSrc = src.length;
        int lengthValues = values.length;
        byte dst[] = new byte[lengthSrc + lengthValues];

        /* begin: modified by marriane 2002-1-11*/
        /*
        for (int i = 0; i < lengthSrc; i ++) {
            dst[i] = src[i];
        }
        for (int i = 0; i < lengthValues; i ++) {
            dst[i + lengthSrc] = values[i];
        }*/
        System.arraycopy(src, 0, dst, 0, lengthSrc);
        System.arraycopy(values, 0, dst, lengthSrc, lengthValues);
        /* end: modified by marriane 2002-1-11*/

        return dst;
    }

    /**
     * Trim the white space from the front and end
     *
     * @param src the original byte array
     * @return <code> byte[] </code> with the trimmed byte array
     */

    public final static byte[] trim(byte[] src) {
        int len = src.length;
        int start = 0;
        int end = len - 1;

        while ((start < len) && (src[start] >= 0 && src[start] <= 32)) {
            start++;
        }
        while ((end > start) && (src[end] >= 0 && src[end] <= 32)) {
            end--;
        }
        return (subByteArray(src, start, end - start + 1));
    }

    public final static byte[] subByteArray(byte[] src, int length) {
        int maxLength = src.length;
        if (length > maxLength) return null;
        byte[] dst = new byte[length];
        /*begin: modified by marriane 2002-1-11 */
        /*for (int i = 0; i < length; i++) {
            dst [i] = src[i];
        }*/
        System.arraycopy(src, 0, dst, 0, length);
        /*end: modified by marriane 2002-1-11 */
        return dst;
    }

    public final static byte[] subByteArray2(byte[] src, int start) {
        int maxLength = src.length;
        if (start >= maxLength) return null;
        int length = maxLength - start;
        byte[] dst = new byte[length];
        /* begin: modified by marriane 2001-1-11*/
        /*for (int i = 0; i < length; i++) {
            dst [i] = src[i+start];
        }*/
        System.arraycopy(src, start, dst, 0, length);
        /* end: modified by marriane 2001-1-11*/
        return dst;
    }


    public final static byte[] copyByteArray(byte[] str, int start, int length) {
        int maxLength = str.length;
        if ((start > maxLength) || (length > maxLength) || (start + length > maxLength)) {
            System.out.println("copyByteArray exception, start/(start+length) > str.length");
            return null;
        }

        byte temp[] = new byte[length];
        /* begin: modified by marriane 2001-1-11*/
        /*
        for (int i = 0; i < length; i++) {
            temp[i] = str[i + start];
        }*/

        System.arraycopy(str, start, temp, 0, length);
        /* end: modified by marriane 2001-1-11*/
        return temp;
    }

    public final static byte[] subByteArray(byte[] str, int start, int length) {
        int maxLength = str.length;
        if ((start > maxLength) || (length > maxLength) || (start + length > maxLength)) {
            System.out.println("copyByteArray exception, start/(start+length) > str.length");
            return null;
        }

        byte temp[] = new byte[length];
        /*begin: modified by marriane 2001-1-11*/
        /*for (int i = 0; i < length; i++) {
            temp[i] = str[i + start];
        }*/
        System.arraycopy(str, start, temp, 0, length);
        /*end: modified by marriane 2001-1-11*/
        return temp;
    }

    public final static byte[] subByteArray2(byte[] str, int start, int end) {

        int maxLength = str.length;
        if ((start >= maxLength) || (end >= maxLength) || (start > end)) {
            System.out.println("copyByteArray exception, start/(start+length) > str.length   or  start > end");
            return null;
        }

        int length = end - start + 1;
        byte temp[] = new byte[length];
        /* begin: modified by marriane 2001-1-11 */
        /*for (int i = start; i <= end; i++) {
               temp[i-start] = str[i];
          }*/
        System.arraycopy(str, start, temp, 0, length);

        /* end: modified by marriane 2001-1-11 */
        return temp;
    }

    public final static void displayCurrentTime() {
        long temp = System.currentTimeMillis();
        Time tempTime = new Time(temp);
        System.out.println("Current Time: " + tempTime.toString());
    }

    public static String UTF8String(byte[] str) {
        try {
            return new String(str, "UTF8");
        } catch (Exception e) {
            return null;
        }
    }
}
TOP

Related Classes of org.chaidb.db.helper.ByteTool

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.