forked from geodmms/xdgnjobs

Dennis Kao
2013-11-21 1f40660167b33075ad43984c9816727ea119ea64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.ximple.eofms.util;
 
//~--- JDK imports ------------------------------------------------------------
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.LongBuffer;
 
/**
 * BinConverter
 * User: Ulysses
 * Date: 2007/9/17
 */
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'
        };
 
    /**
     * gets bytes from an array into a long
     *
     * @param buffer      where to get the bytes
     * @param nStartIndex index from where to read the data
     * @return the 64bit integer
     */
    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);
    }
 
    /**
     * converts a long o bytes which are put into a given array
     *
     * @param lValue      the 64bit integer to convert
     * @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) {
        buffer[nStartIndex] = (byte) (lValue >>> 56);
        buffer[nStartIndex + 1] = (byte) ((lValue >>> 48) & 0x0ff);
        buffer[nStartIndex + 2] = (byte) ((lValue >>> 40) & 0x0ff);
        buffer[nStartIndex + 3] = (byte) ((lValue >>> 32) & 0x0ff);
        buffer[nStartIndex + 4] = (byte) ((lValue >>> 24) & 0x0ff);
        buffer[nStartIndex + 5] = (byte) ((lValue >>> 16) & 0x0ff);
        buffer[nStartIndex + 6] = (byte) ((lValue >>> 8) & 0x0ff);
        buffer[nStartIndex + 7] = (byte) lValue;
    }
 
    /**
     * converts values from an integer array to a long
     *
     * @param buffer      where to get the bytes
     * @param nStartIndex index from where to read the data
     * @return the 64bit integer
     */
    public static long intArrayToLong(int[] buffer, int nStartIndex) {
        return (((long) buffer[nStartIndex]) << 32) | (((long) buffer[nStartIndex + 1]) & 0x0ffffffffL);
    }
 
    /**
     * converts a long to integers which are put into a given array
     *
     * @param lValue      the 64bit integer to convert
     * @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) {
        buffer[nStartIndex] = (int) (lValue >>> 32);
        buffer[nStartIndex + 1] = (int) lValue;
    }
 
    /**
     * makes a long from two integers (treated unsigned)
     *
     * @param nLo lower 32bits
     * @param nHi higher 32bits
     * @return the built long
     */
    public static long makeLong(int nLo, int nHi) {
        return (((long) nHi << 32) | ((long) nLo & 0x00000000ffffffffL));
    }
 
    /**
     * gets the lower 32 bits of a long
     *
     * @param lVal the long integer
     * @return lower 32 bits
     */
    public static int longLo32(long lVal) {
        return (int) lVal;
    }
 
    /**
     * gets the higher 32 bits of a long
     *
     * @param lVal the long integer
     * @return higher 32 bits
     */
    public static int longHi32(long lVal) {
        return (int) ((long) (lVal >>> 32));
    }
 
    /**
     * converts a byte array to a binhex string
     *
     * @param data the byte array
     * @return the binhex string
     */
    public static String bytesToBinHex(byte[] data) {
        // just map the call
        return bytesToBinHex(data, 0, data.length);
    }
 
    /**
     * converts a byte array to a binhex string
     *
     * @param data        the byte array
     * @param nStartPos   start index where to get the bytes
     * @param nNumOfBytes number of bytes to convert
     * @return the binhex string
     */
    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++) {
            sbuf.setCharAt(nPos++, HEXTAB[(data[nI + nStartPos] >> 4) & 0x0f]);
            sbuf.setCharAt(nPos++, HEXTAB[data[nI + nStartPos] & 0x0f]);
        }
 
        return sbuf.toString();
    }
 
    /**
     * converts a binhex string back into a byte array (invalid codes will be skipped)
     *
     * @param sBinHex     binhex string
     * @param data        the target array
     * @param nSrcPos     from which character in the string the conversion should begin,
     *                    remember that (nSrcPos modulo 2) should equals 0 normally
     * @param nDstPos     to store the bytes from which position in the array
     * @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) {
        // check for correct ranges
        int nStrLen = sBinHex.length();
        int nAvailBytes = (nStrLen - nSrcPos) >> 1;
 
        if (nAvailBytes < nNumOfBytes) {
            nNumOfBytes = nAvailBytes;
        }
 
        int nOutputCapacity = data.length - nDstPos;
 
        if (nNumOfBytes > nOutputCapacity) {
            nNumOfBytes = nOutputCapacity;
        }
 
        // convert now
        int nResult = 0;
 
        for (int nI = 0; nI < nNumOfBytes; nI++) {
            byte bActByte = 0;
            boolean blConvertOK = true;
 
            for (int nJ = 0; nJ < 2; nJ++) {
                bActByte <<= 4;
 
                char cActChar = sBinHex.charAt(nSrcPos++);
 
                if ((cActChar >= 'a') && (cActChar <= 'f')) {
                    bActByte |= (byte) (cActChar - 'a') + 10;
                } else if ((cActChar >= '0') && (cActChar <= '9')) {
                    bActByte |= (byte) (cActChar - '0');
                } else {
                    blConvertOK = false;
                }
            }
 
            if (blConvertOK) {
                data[nDstPos++] = bActByte;
                nResult++;
            }
        }
 
        return nResult;
    }
 
    /**
     * converts a byte array into an UNICODE string
     *
     * @param data        the byte array
     * @param nStartPos   where to begin the conversion
     * @param nNumOfBytes number of bytes to handle
     * @return the string
     */
    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) {
            nNumOfBytes = nAvailCapacity;
        }
 
        StringBuffer sbuf = new StringBuffer();
 
        sbuf.setLength(nNumOfBytes >> 1);
 
        int nSBufPos = 0;
 
        while (nNumOfBytes > 0) {
            sbuf.setCharAt(nSBufPos++, (char) (((int) data[nStartPos] << 8) | ((int) data[nStartPos + 1] & 0x0ff)));
            nStartPos += 2;
            nNumOfBytes -= 2;
        }
 
        return sbuf.toString();
    }
 
    public static long[] marshalByteArray(byte[] raws, boolean hasSignature) {
        int remainder = raws.length % 8;
        ByteBuffer rawData = ByteBuffer.wrap(raws);
 
        rawData.rewind();
        rawData.order(ByteOrder.LITTLE_ENDIAN);
 
        LongBuffer longBuffer = ((ByteBuffer) rawData.rewind()).asLongBuffer();
        int resultSize = longBuffer.limit() + ((remainder != 0)
            ? 1
            : 0) + (hasSignature
            ? 1
            : 0);
        long[] result = new long[resultSize];
        int i = 0;
 
        if (hasSignature) {
            result[i] = raws.length;
            i++;
        }
 
        while (longBuffer.hasRemaining()) {
            result[i] = longBuffer.get();
            i++;
        }
 
        if (remainder != 0) {
            int pos = (i - (hasSignature
                ? 1
                : 0)) * 8;
 
            // int pos = rawData.position();
            byte[] temp = new byte[8];
 
            for (int j = 0; j < remainder; j++) {
                temp[7 - j] = raws[pos + j];
            }
 
            // System.arraycopy(raws, pos, temp, 0, remainder);
            result[i] = BinConverter.byteArrayToLong(temp, 0);
        }
 
        return result;
    }
 
    public static byte[] unmarshalByteArray(long[] raws, boolean hasSignature) {
        LongBuffer longBuffer = LongBuffer.wrap(raws);
        int resultBufferSize = (raws.length - (hasSignature
            ? 1
            : 0)) * 8;
        int resultSize = resultBufferSize;
 
        if (hasSignature) {
            resultSize = (int) longBuffer.get();
        }
 
        ByteBuffer result = ByteBuffer.allocate(resultBufferSize);
 
        result.order(ByteOrder.LITTLE_ENDIAN);
 
        while (longBuffer.hasRemaining()) {
            result.putLong(longBuffer.get());
        }
 
        if (resultSize == resultBufferSize) {
            return result.array();
        }
 
        byte[] resultData = new byte[resultSize];
 
        result.position(0);
        result.get(resultData, 0, resultSize);
 
        return resultData;
    }
 
    public static long[] marshalCompactByteArray(byte[] raws) {
        byte[] compactRaws = new byte[raws.length + 2];
        ByteBuffer bbCompact = ByteBuffer.wrap(compactRaws);
        bbCompact.order(ByteOrder.LITTLE_ENDIAN);
        bbCompact.putShort((short) raws.length);
        bbCompact.put(raws);
        long[] longData = BinConverter.marshalByteArray(compactRaws, false);
        return longData;
    }
 
    public static byte[] unmarshalCompactByteArray(long[] raws) {
        byte[] rawData = BinConverter.unmarshalByteArray(raws, false);
 
        ByteBuffer bbCompact = ByteBuffer.wrap(rawData);
        bbCompact.order(ByteOrder.LITTLE_ENDIAN);
        short originSize = bbCompact.getShort();
 
        byte[] rawOriginData = new byte[originSize];
        bbCompact.get(rawOriginData, 0, originSize);
        return rawOriginData;
    }
}