-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteUtils.java
More file actions
306 lines (259 loc) · 7.47 KB
/
ByteUtils.java
File metadata and controls
306 lines (259 loc) · 7.47 KB
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
package common.android.fiot.androidcommon;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
* Created by caoxuanphong on 4/28/16.
*/
public class ByteUtils {
private static final String TAG = "ByteUtils";
/**
* Create byte array from list of integer
*
* Ex: ByteUtils.createByteArray(1,2,3,4,5,100, 500);
* Result: [0x1, 0x2, 0x3, 0x4, 0x5, 0x64, 0xf4]
*
* @param numbers
* @return
*/
public static byte[] createByteArray(int ...numbers) {
byte [] array = new byte[numbers.length];
for (int i = 0; i < numbers.length; i++) {
array[i] = (byte) numbers[i];
}
return array;
}
/**
* Convert a String into byte array
* @param string
* @return
*/
public static byte[] stringToByteArray(String string) {
return string.getBytes();
}
/**
* Convert interger into byte array
* @param number
* @param order
* @return
*/
public static byte[] integerToByteArray(int number, ByteOrder order) {
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(order);
return byteBuffer.putInt(number).array();
}
/**
* Convert long into byte array
* @param number
* @param order
* @return
*/
public static byte[] longToByteArray(long number, ByteOrder order) {
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
byteBuffer.order(order);
return byteBuffer.putLong(number).array();
}
/**
* Convert short into byte array
* @param number
* @param order
* @return
*/
public static byte[] shortToByteArray(short number, ByteOrder order) {
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
byteBuffer.order(order);
return byteBuffer.putShort(number).array();
}
/**
* Convert float into byte array
* @param number
* @param order
* @return
*/
public static byte[] floatToByteArray(float number, ByteOrder order) {
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(order);
return byteBuffer.putFloat(number).array();
}
/**
* Convert double into byte array
* @param number
* @param order
* @return
*/
public static byte[] doubleToByteArray(double number, ByteOrder order) {
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
byteBuffer.order(order);
return byteBuffer.putDouble(number).array();
}
/**
* Convert byte array into float
* @param b
* @param order
* @return
*/
public static float toFloat(byte[] b, ByteOrder order) {
return ByteBuffer.wrap(b).order(order).getFloat();
}
/**
* Convert byte array into integer
* @param b
* @param order
* @return
*/
public static int toInteger(byte[] b, ByteOrder order) {
return ByteBuffer.wrap(b).order(order).getInt();
}
/**
* Convert byte array into long
* @param b
* @param order
* @return
*/
public static long toLong(byte[] b, ByteOrder order) {
return ByteBuffer.wrap(b).order(order).getLong();
}
public static double toDouble(byte[] b, ByteOrder order) {
return ByteBuffer.wrap(b).order(order).getDouble();
}
public static short toShort(byte[] b, ByteOrder order) {
return ByteBuffer.wrap(b).order(order).getShort();
}
public static String toString(byte[] b) {
return new String(b, StandardCharsets.UTF_8);
}
public static String toString(byte[] b, Charset charset) {
return new String(b, charset);
}
/**
* Concatenate 2 byte array into new byte array
* @param a
* @param b
* @return
*/
public static byte[] concatenate(byte[] a, byte[] b) {
if (a == null && b == null) return null;
if (a == null && b != null) return b;
if (a != null && b == null) return a;
byte[] c = new byte[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
/**
* Add 1 byte into the end of byte array
* @param a
* @param b
* @return
*/
public static byte[] addByte(byte[] a, byte b) {
byte[] c;
if (a == null) {
return new byte[] {b};
} else {
c = new byte[a.length + 1];
}
if (a != null) {
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(new byte[]{b}, 0, c, a.length, 1);
} else {
c[0] = b;
}
return c;
}
/**
* Merge 2 byte array
* @param src
* @param bytes
* @param startPos
* @return
*/
public static byte[] merge(byte[] src, byte[] bytes, int startPos) {
int j = 0;
for (int i = startPos; i < startPos + bytes.length; i++) {
src[i] = bytes[j++];
}
return src;
}
/**
* Get sub of array
* @param src
* @param startPos
* @param num
* @return
*/
public static byte[] subByteArray(byte[] src, int startPos, int num) {
int endPos = 0;
if (startPos + num > src.length) {
endPos = src.length;
} else {
endPos = startPos + num;
}
return Arrays.copyOfRange(src, startPos, endPos );
}
/**
* Convert byte array into string array of hex
* @param a
* @return
*/
public static String toHexString(byte[] a) {
if (a == null) return null;
String[] s = new String[a.length];
for (int i = 0; i < a.length; i++) {
s[i] = "0x" + Integer.toHexString((a[i] & 0xff));
}
return Arrays.toString(s);
}
public static String toIntegerString(byte[] a) {
if (a == null) return null;
return Arrays.toString(a);
}
/**
* Compare 2 byte array contain same data
* @param b1
* @param b2
* @return
*/
public static boolean compare2Array(byte[] b1, byte[] b2) {
if (b1 == null && b2 == null) return true;
if (b1 == null | b2 == null) return false;
if (b1 == b2) return true;
if (b1.length != b2.length) return false;
for (int i = 0; i < b1.length; i++) {
if (b1[i] != b2[i]) return false;
}
return true;
}
/**
* Check @child byte array is contain in @src byte array
* @param src
* @param child
* @return
*/
public static boolean isContain(byte[] src, byte[] child) {
if (src == null && child == null) return true;
if (src == null || child == null) return false;
if (src == child) return true;
if (child.length > src.length) {
return false;
} else if (child.length == src.length) {
for (int i = 0; i < src.length; i++) {
if (src[i] != child[i]) {
return false;
}
}
return true;
} else {
for (int i = 0; i < src.length; i++) {
if (i > child.length - 1) return false;
if (src[i] == child[i]) {
byte[] sub = subByteArray(src, i, child.length);
if (isContain(sub, child)) return true;
}
}
}
return false;
}
}