vincent
2020-06-04 101104e7d5df15be55be1a4984bf08fe87e73547
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package com.zy.gateway.core.transfer;
 
import com.zy.gateway.core.utils.Utilities;
 
 
/**
 * 字节转换类的基类,提供了一些基础的转换方法
 */
public class ByteTransformBase implements IByteTransform {
 
    /**
     * 实例化一个对象
     */
    public ByteTransformBase(){
        this.dataFormat = DataFormat.DCBA;
    }
 
    /**
     * 从缓存中提取出bool结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @return boolean值
     */
    public boolean TransBool(byte[] buffer, int index) {
        return buffer[index] != 0x00;
    }
 
    /**
     * 缓存中提取byte结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @return byte对象
     */
    public byte TransByte(byte[] buffer, int index) {
        return buffer[index];
    }
 
    /**
     * 从缓存中提取byte数组结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @param length 读取的数组长度
     * @return byte数组
     */
    public byte[] TransByte(byte[] buffer, int index, int length) {
        byte[] tmp = new byte[length];
        System.arraycopy(buffer, index, tmp, 0, length);
        return tmp;
    }
 
    /**
     * 从缓存中提取short结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @return short对象
     */
    public short TransInt16(byte[] buffer, int index) {
        return Utilities.getShort(buffer, index);
    }
 
    /**
     * 从缓存中提取short结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @param length 读取的数组长度
     * @return short数组对象
     */
    public short[] TransInt16(byte[] buffer, int index, int length) {
        short[] tmp = new short[length];
        for (int i = 0; i < length; i++) {
            tmp[i] = TransInt16(buffer, index + 2 * i);
        }
        return tmp;
    }
 
    /**
     * 从缓存中提取int结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @return int对象
     */
    public int TransInt32(byte[] buffer, int index) {
        return Utilities.getInt(ByteTransDataFormat4(buffer, index),0);
    }
 
 
    /**
     * 从缓存中提取int数组结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @param length 读取的数组长度
     * @return int数组对象
     */
    public int[] TransInt32(byte[] buffer, int index, int length) {
        int[] tmp = new int[length];
        for (int i = 0; i < length; i++) {
            tmp[i] = TransInt32(buffer, index + 4 * i);
        }
        return tmp;
    }
 
 
    /**
     * 从缓存中提取long结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @return long对象
     */
    public long TransInt64(byte[] buffer, int index) {
        return Utilities.getLong(ByteTransDataFormat8(buffer, index),0);
    }
 
 
    /**
     * 从缓存中提取long数组结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @param length 读取的数组长度
     * @return long数组对象
     */
    public long[] TransInt64(byte[] buffer, int index, int length) {
        long[] tmp = new long[length];
        for (int i = 0; i < length; i++) {
            tmp[i] = TransInt64(buffer, index + 8 * i);
        }
        return tmp;
    }
 
 
    /**
     * 从缓存中提取float结果
     *
     * @param buffer 缓存对象
     * @param index  索引位置
     * @return float对象
     */
    public float TransSingle(byte[] buffer, int index) {
        return Utilities.getFloat(ByteTransDataFormat4(buffer, index),0);
    }
 
 
    /**
     * 从缓存中提取float数组结果
     *
     * @param buffer 缓存数据
     * @param index  索引位置
     * @param length 读取的数组长度
     * @return float数组对象
     */
    public float[] TransSingle(byte[] buffer, int index, int length) {
        float[] tmp = new float[length];
        for (int i = 0; i < length; i++) {
            tmp[i] = TransSingle(buffer, index + 4 * i);
        }
        return tmp;
    }
 
 
    /**
     * 从缓存中提取double结果
     *
     * @param buffer 缓存对象
     * @param index  索引位置
     * @return double对象
     */
    public double TransDouble(byte[] buffer, int index) {
        return Utilities.getDouble(ByteTransDataFormat8(buffer, index),0);
    }
 
 
    /**
     * 从缓存中提取double数组结果
     *
     * @param buffer 缓存对象
     * @param index  索引位置
     * @param length 读取的数组长度
     * @return double数组
     */
    public double[] TransDouble(byte[] buffer, int index, int length) {
        double[] tmp = new double[length];
        for (int i = 0; i < length; i++) {
            tmp[i] = TransDouble(buffer, index + 8 * i);
        }
        return tmp;
    }
 
 
    /**
     * 从缓存中提取string结果,使用指定的编码
     *
     * @param buffer   缓存对象
     * @param index    索引位置
     * @param length   byte数组长度
     * @param encoding 字符串的编码
     * @return string对象
     */
    public String TransString(byte[] buffer, int index, int length, String encoding) {
        return Utilities.getString(TransByte(buffer, index, length), encoding);
    }
 
 
    /**
     * bool变量转化缓存数据
     *
     * @param value 等待转化的数据
     * @return buffer数据
     */
    public byte[] TransByte(boolean value) {
        return TransByte(new boolean[]{value});
    }
 
 
    /**
     * bool数组变量转化缓存数据
     *
     * @param values 等待转化的数组
     * @return buffer数据
     */
    public byte[] TransByte(boolean[] values) {
        if (values == null) return null;
 
        byte[] buffer = new byte[values.length];
        for (int i = 0; i < values.length; i++) {
            if (values[i]) buffer[i] = 0x01;
        }
 
        return buffer;
    }
 
 
    /**
     * byte变量转化缓存数据
     *
     * @param value 等待转化的数据
     * @return buffer数据
     */
    public byte[] TransByte(byte value) {
        return new byte[]{value};
    }
 
    /**
     * short变量转化缓存数据
     *
     * @param value 等待转化的数据
     * @return buffer数据
     */
    public byte[] TransByte(short value) {
        return TransByte(new short[]{value});
    }
 
    /**
     * short数组变量转化缓存数据
     *
     * @param values 等待转化的数组
     * @return buffer数据
     */
    public byte[] TransByte(short[] values) {
        if (values == null) return null;
        byte[] buffer = new byte[values.length * 2];
        for (int i = 0; i < values.length; i++) {
            System.arraycopy(Utilities.getBytes(values[i]), 0, buffer, 2 * i, 2);
        }
        return buffer;
    }
 
 
    /**
     * int变量转化缓存数据
     *
     * @param value 等待转化的数据
     * @return buffer数据
     */
    public byte[] TransByte(int value) {
        return TransByte(new int[]{value});
    }
 
 
    /**
     * int数组变量转化缓存数据
     *
     * @param values 等待转化的数组
     * @return buffer数据
     */
    public byte[] TransByte(int[] values) {
        if (values == null) return null;
 
        byte[] buffer = new byte[values.length * 4];
        for (int i = 0; i < values.length; i++) {
            System.arraycopy(ByteTransDataFormat4(Utilities.getBytes(values[i])), 0, buffer, 4 * i, 4);
        }
 
        return buffer;
    }
 
    /**
     * long变量转化缓存数据
     *
     * @param value 等待转化的数据
     * @return buffer数据
     */
    public byte[] TransByte(long value) {
        return TransByte(new long[]{value});
    }
 
 
    /**
     * long数组变量转化缓存数据
     *
     * @param values 等待转化的数组
     * @return buffer数据
     */
    public byte[] TransByte(long[] values) {
        if (values == null) return null;
 
        byte[] buffer = new byte[values.length * 8];
        for (int i = 0; i < values.length; i++) {
            System.arraycopy(ByteTransDataFormat8(Utilities.getBytes(values[i])), 0, buffer, 8 * i, 8);
        }
 
        return buffer;
    }
 
 
    /**
     * float变量转化缓存数据
     *
     * @param value 等待转化的数据
     * @return buffer数据
     */
    public byte[] TransByte(float value) {
        return TransByte(new float[]{value});
    }
 
 
    /**
     * float数组变量转化缓存数据
     *
     * @param values 等待转化的数组
     * @return buffer数据
     */
    public byte[] TransByte(float[] values) {
        if (values == null) return null;
 
        byte[] buffer = new byte[values.length * 4];
        for (int i = 0; i < values.length; i++) {
            System.arraycopy(ByteTransDataFormat4(Utilities.getBytes(values[i])), 0, buffer, 4 * i, 4);
        }
 
        return buffer;
    }
 
 
    /**
     * double变量转化缓存数据
     *
     * @param value 等待转化的数据
     * @return buffer数据
     */
    public byte[] TransByte(double value) {
        return TransByte(new double[]{value});
    }
 
    /**
     * double数组变量转化缓存数据
     *
     * @param values 等待转化的数组
     * @return buffer数据
     */
    public byte[] TransByte(double[] values) {
        if (values == null) return null;
 
        byte[] buffer = new byte[values.length * 8];
        for (int i = 0; i < values.length; i++) {
            System.arraycopy(ByteTransDataFormat8(Utilities.getBytes(values[i])), 0, buffer, 8 * i, 8);
        }
 
        return buffer;
    }
 
    /**
     * 使用指定的编码字符串转化缓存数据
     *
     * @param value    等待转化的数据
     * @param encoding 字符串的编码方式
     * @return buffer数据
     */
    public byte[] TransByte(String value, String encoding) {
        return Utilities.getBytes(value, encoding);
    }
 
 
    private DataFormat dataFormat = DataFormat.ABCD;
 
    /**
     * 设置数据解析的格式,ABCD,BADC,CDAB,DCBA格式
     *
     * @param dataFormat
     */
    public void setDataFormat(DataFormat dataFormat) {
        this.dataFormat = dataFormat;
    }
 
    /**
     * 获取数据解析的格式,默认ABCD,可选BADC,CDAB,DCBA格式
     *
     * @return
     */
    public DataFormat getDataFormat() {
        return this.dataFormat;
    }
 
 
    /**
     * 反转多字节的数据信息
     *
     * @param value 数据字节
     * @param Index 起始索引,默认值为0
     * @return 实际字节信息
     */
    protected byte[] ByteTransDataFormat4(byte[] value, int Index) {
        byte[] buffer = new byte[4];
        switch (dataFormat) {
            case ABCD: {
                buffer[0] = value[Index + 3];
                buffer[1] = value[Index + 2];
                buffer[2] = value[Index + 1];
                buffer[3] = value[Index + 0];
                break;
            }
            case BADC: {
                buffer[0] = value[Index + 2];
                buffer[1] = value[Index + 3];
                buffer[2] = value[Index + 0];
                buffer[3] = value[Index + 1];
                break;
            }
            case CDAB: {
                buffer[0] = value[Index + 1];
                buffer[1] = value[Index + 0];
                buffer[2] = value[Index + 3];
                buffer[3] = value[Index + 2];
                break;
            }
            case DCBA: {
                buffer[0] = value[Index + 0];
                buffer[1] = value[Index + 1];
                buffer[2] = value[Index + 2];
                buffer[3] = value[Index + 3];
                break;
            }
        }
        return buffer;
    }
 
 
    /**
     * 反转多字节的数据信息
     *
     * @param value 数据字节
     * @return 实际字节信息
     */
    protected byte[] ByteTransDataFormat4(byte[] value) {
        return ByteTransDataFormat4(value, 0);
    }
 
 
    /**
     * 反转多字节的数据信息
     *
     * @param value 数据字节
     * @param Index 起始索引,默认值为0
     * @return 实际字节信息
     */
    protected byte[] ByteTransDataFormat8(byte[] value, int Index) {
        byte[] buffer = new byte[8];
        switch (dataFormat) {
            case ABCD: {
                buffer[0] = value[Index + 7];
                buffer[1] = value[Index + 6];
                buffer[2] = value[Index + 5];
                buffer[3] = value[Index + 4];
                buffer[4] = value[Index + 3];
                buffer[5] = value[Index + 2];
                buffer[6] = value[Index + 1];
                buffer[7] = value[Index + 0];
                break;
            }
            case BADC: {
                buffer[0] = value[Index + 6];
                buffer[1] = value[Index + 7];
                buffer[2] = value[Index + 4];
                buffer[3] = value[Index + 5];
                buffer[4] = value[Index + 2];
                buffer[5] = value[Index + 3];
                buffer[6] = value[Index + 0];
                buffer[7] = value[Index + 1];
                break;
            }
 
            case CDAB: {
                buffer[0] = value[Index + 1];
                buffer[1] = value[Index + 0];
                buffer[2] = value[Index + 3];
                buffer[3] = value[Index + 2];
                buffer[4] = value[Index + 5];
                buffer[5] = value[Index + 4];
                buffer[6] = value[Index + 7];
                buffer[7] = value[Index + 6];
                break;
            }
            case DCBA: {
                buffer[0] = value[Index + 0];
                buffer[1] = value[Index + 1];
                buffer[2] = value[Index + 2];
                buffer[3] = value[Index + 3];
                buffer[4] = value[Index + 4];
                buffer[5] = value[Index + 5];
                buffer[6] = value[Index + 6];
                buffer[7] = value[Index + 7];
                break;
            }
        }
        return buffer;
    }
 
 
    /**
     * 反转多字节的数据信息
     *
     * @param value 数据字节
     * @return 实际字节信息
     */
    protected byte[] ByteTransDataFormat8(byte[] value) {
        return ByteTransDataFormat8(value, 0);
    }
}