forked from geodmms/xdgnjobs

Dennis Kao
2014-01-15 94ae08701bbd7585a0b7e5a92d1975965a503c03
xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/util/BinConverter.java
@@ -10,15 +10,13 @@
 * BinConverter
 * User: Ulysses
 * Date: 2007/9/17
 * Time: ¤W¤È 01:13:13
 */
public class BinConverter
{
public class BinConverter {
    // our table for binhex conversion
    final static char[] HEXTAB =
            {
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
            };
        {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
        };
    /**
     * gets bytes from an array into a long
@@ -27,12 +25,11 @@
     * @param nStartIndex index from where to read the data
     * @return the 64bit integer
     */
    public static long byteArrayToLong(byte[] buffer, int nStartIndex)
    {
    public static long byteArrayToLong(byte[] buffer, int nStartIndex) {
        return (((long) buffer[nStartIndex]) << 56) | (((long) buffer[nStartIndex + 1] & 0x0ffL) << 48)
                | (((long) buffer[nStartIndex + 2] & 0x0ffL) << 40) | (((long) buffer[nStartIndex + 3] & 0x0ffL) << 32)
                | (((long) buffer[nStartIndex + 4] & 0x0ffL) << 24) | (((long) buffer[nStartIndex + 5] & 0x0ffL) << 16)
                | (((long) buffer[nStartIndex + 6] & 0x0ffL) << 8) | ((long) buffer[nStartIndex + 7] & 0x0ff);
            | (((long) buffer[nStartIndex + 2] & 0x0ffL) << 40) | (((long) buffer[nStartIndex + 3] & 0x0ffL) << 32)
            | (((long) buffer[nStartIndex + 4] & 0x0ffL) << 24) | (((long) buffer[nStartIndex + 5] & 0x0ffL) << 16)
            | (((long) buffer[nStartIndex + 6] & 0x0ffL) << 8) | ((long) buffer[nStartIndex + 7] & 0x0ff);
    }
    /**
@@ -42,8 +39,7 @@
     * @param buffer      the target buffer
     * @param nStartIndex where to place the bytes in the buffer
     */
    public static void longToByteArray(long lValue, byte[] buffer, int nStartIndex)
    {
    public static void longToByteArray(long lValue, byte[] buffer, int nStartIndex) {
        buffer[nStartIndex] = (byte) (lValue >>> 56);
        buffer[nStartIndex + 1] = (byte) ((lValue >>> 48) & 0x0ff);
        buffer[nStartIndex + 2] = (byte) ((lValue >>> 40) & 0x0ff);
@@ -61,8 +57,7 @@
     * @param nStartIndex index from where to read the data
     * @return the 64bit integer
     */
    public static long intArrayToLong(int[] buffer, int nStartIndex)
    {
    public static long intArrayToLong(int[] buffer, int nStartIndex) {
        return (((long) buffer[nStartIndex]) << 32) | (((long) buffer[nStartIndex + 1]) & 0x0ffffffffL);
    }
@@ -73,8 +68,7 @@
     * @param buffer      the target buffer
     * @param nStartIndex where to place the bytes in the buffer
     */
    public static void longToIntArray(long lValue, int[] buffer, int nStartIndex)
    {
    public static void longToIntArray(long lValue, int[] buffer, int nStartIndex) {
        buffer[nStartIndex] = (int) (lValue >>> 32);
        buffer[nStartIndex + 1] = (int) lValue;
    }
@@ -86,8 +80,7 @@
     * @param nHi higher 32bits
     * @return the built long
     */
    public static long makeLong(int nLo, int nHi)
    {
    public static long makeLong(int nLo, int nHi) {
        return (((long) nHi << 32) | ((long) nLo & 0x00000000ffffffffL));
    }
@@ -97,8 +90,7 @@
     * @param lVal the long integer
     * @return lower 32 bits
     */
    public static int longLo32(long lVal)
    {
    public static int longLo32(long lVal) {
        return (int) lVal;
    }
@@ -108,8 +100,7 @@
     * @param lVal the long integer
     * @return higher 32 bits
     */
    public static int longHi32(long lVal)
    {
    public static int longHi32(long lVal) {
        return (int) ((long) (lVal >>> 32));
    }
@@ -119,8 +110,7 @@
     * @param data the byte array
     * @return the binhex string
     */
    public static String bytesToBinHex(byte[] data)
    {
    public static String bytesToBinHex(byte[] data) {
        // just map the call
        return bytesToBinHex(data, 0, data.length);
    }
@@ -133,16 +123,14 @@
     * @param nNumOfBytes number of bytes to convert
     * @return the binhex string
     */
    public static String bytesToBinHex(byte[] data, int nStartPos, int nNumOfBytes)
    {
    public static String bytesToBinHex(byte[] data, int nStartPos, int nNumOfBytes) {
        StringBuffer sbuf = new StringBuffer();
        sbuf.setLength(nNumOfBytes << 1);
        int nPos = 0;
        for (int nI = 0; nI < nNumOfBytes; nI++)
        {
        for (int nI = 0; nI < nNumOfBytes; nI++) {
            sbuf.setCharAt(nPos++, HEXTAB[(data[nI + nStartPos] >> 4) & 0x0f]);
            sbuf.setCharAt(nPos++, HEXTAB[data[nI + nStartPos] & 0x0f]);
        }
@@ -161,52 +149,43 @@
     * @param nNumOfBytes number of bytes to extract
     * @return number of extracted bytes
     */
    public static int binHexToBytes(String sBinHex, byte[] data, int nSrcPos, int nDstPos, int nNumOfBytes)
    {
    public static int binHexToBytes(String sBinHex, byte[] data, int nSrcPos, int nDstPos, int nNumOfBytes) {
        // check for correct ranges
        int nStrLen = sBinHex.length();
        int nAvailBytes = (nStrLen - nSrcPos) >> 1;
        if (nAvailBytes < nNumOfBytes)
        {
        if (nAvailBytes < nNumOfBytes) {
            nNumOfBytes = nAvailBytes;
        }
        int nOutputCapacity = data.length - nDstPos;
        if (nNumOfBytes > nOutputCapacity)
        {
        if (nNumOfBytes > nOutputCapacity) {
            nNumOfBytes = nOutputCapacity;
        }
        // convert now
        int nResult = 0;
        for (int nI = 0; nI < nNumOfBytes; nI++)
        {
        for (int nI = 0; nI < nNumOfBytes; nI++) {
            byte bActByte = 0;
            boolean blConvertOK = true;
            for (int nJ = 0; nJ < 2; nJ++)
            {
            for (int nJ = 0; nJ < 2; nJ++) {
                bActByte <<= 4;
                char cActChar = sBinHex.charAt(nSrcPos++);
                if ((cActChar >= 'a') && (cActChar <= 'f'))
                {
                if ((cActChar >= 'a') && (cActChar <= 'f')) {
                    bActByte |= (byte) (cActChar - 'a') + 10;
                } else if ((cActChar >= '0') && (cActChar <= '9'))
                {
                } else if ((cActChar >= '0') && (cActChar <= '9')) {
                    bActByte |= (byte) (cActChar - '0');
                } else
                {
                } else {
                    blConvertOK = false;
                }
            }
            if (blConvertOK)
            {
            if (blConvertOK) {
                data[nDstPos++] = bActByte;
                nResult++;
            }
@@ -223,16 +202,14 @@
     * @param nNumOfBytes number of bytes to handle
     * @return the string
     */
    public static String byteArrayToUNCString(byte[] data, int nStartPos, int nNumOfBytes)
    {
    public static String byteArrayToUNCString(byte[] data, int nStartPos, int nNumOfBytes) {
        // we need two bytes for every character
        nNumOfBytes &= ~1;
        // enough bytes in the buffer?
        int nAvailCapacity = data.length - nStartPos;
        if (nAvailCapacity < nNumOfBytes)
        {
        if (nAvailCapacity < nNumOfBytes) {
            nNumOfBytes = nAvailCapacity;
        }
@@ -242,8 +219,7 @@
        int nSBufPos = 0;
        while (nNumOfBytes > 0)
        {
        while (nNumOfBytes > 0) {
            sbuf.setCharAt(nSBufPos++, (char) (((int) data[nStartPos] << 8) | ((int) data[nStartPos + 1] & 0x0ff)));
            nStartPos += 2;
            nNumOfBytes -= 2;
@@ -252,8 +228,7 @@
        return sbuf.toString();
    }
    public static long[] marshalByteArray(byte[] raws, boolean hasSignature)
    {
    public static long[] marshalByteArray(byte[] raws, boolean hasSignature) {
        int remainder = raws.length % 8;
        ByteBuffer rawData = ByteBuffer.wrap(raws);
@@ -262,36 +237,32 @@
        LongBuffer longBuffer = ((ByteBuffer) rawData.rewind()).asLongBuffer();
        int resultSize = longBuffer.limit() + ((remainder != 0)
                ? 1
                : 0) + (hasSignature
                ? 1
                : 0);
            ? 1
            : 0) + (hasSignature
            ? 1
            : 0);
        long[] result = new long[resultSize];
        int i = 0;
        if (hasSignature)
        {
        if (hasSignature) {
            result[i] = raws.length;
            i++;
        }
        while (longBuffer.hasRemaining())
        {
        while (longBuffer.hasRemaining()) {
            result[i] = longBuffer.get();
            i++;
        }
        if (remainder != 0)
        {
        if (remainder != 0) {
            int pos = (i - (hasSignature
                    ? 1
                    : 0)) * 8;
                ? 1
                : 0)) * 8;
            // int pos = rawData.position();
            byte[] temp = new byte[8];
            for (int j = 0; j < remainder; j++)
            {
            for (int j = 0; j < remainder; j++) {
                temp[7 - j] = raws[pos + j];
            }
@@ -302,16 +273,14 @@
        return result;
    }
    public static byte[] unmarshalByteArray(long[] raws, boolean hasSignature)
    {
    public static byte[] unmarshalByteArray(long[] raws, boolean hasSignature) {
        LongBuffer longBuffer = LongBuffer.wrap(raws);
        int resultBufferSize = (raws.length - (hasSignature
                ? 1
                : 0)) * 8;
            ? 1
            : 0)) * 8;
        int resultSize = resultBufferSize;
        if (hasSignature)
        {
        if (hasSignature) {
            resultSize = (int) longBuffer.get();
        }
@@ -319,13 +288,11 @@
        result.order(ByteOrder.LITTLE_ENDIAN);
        while (longBuffer.hasRemaining())
        {
        while (longBuffer.hasRemaining()) {
            result.putLong(longBuffer.get());
        }
        if (resultSize == resultBufferSize)
        {
        if (resultSize == resultBufferSize) {
            return result.array();
        }
@@ -337,8 +304,7 @@
        return resultData;
    }
    public static long[] marshalCompactByteArray(byte[] raws)
    {
    public static long[] marshalCompactByteArray(byte[] raws) {
        byte[] compactRaws = new byte[raws.length + 2];
        ByteBuffer bbCompact = ByteBuffer.wrap(compactRaws);
        bbCompact.order(ByteOrder.LITTLE_ENDIAN);
@@ -348,8 +314,7 @@
        return longData;
    }
    public static byte[] unmarshalCompactByteArray(long[] raws)
    {
    public static byte[] unmarshalCompactByteArray(long[] raws) {
        byte[] rawData = BinConverter.unmarshalByteArray(raws, false);
        ByteBuffer bbCompact = ByteBuffer.wrap(rawData);