-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtility.java
More file actions
67 lines (59 loc) · 1.59 KB
/
Utility.java
File metadata and controls
67 lines (59 loc) · 1.59 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
import java.io.*;
public class Utility {
public static byte[] concatArray(byte[] a, byte[] b)
{
byte[] res = new byte[a.length + b.length];
System.arraycopy(a, 0, res, 0, a.length);
System.arraycopy(b, 0, res, a.length, b.length);
return res;
}
public static void convertInt2Bytes(int v, byte[] res, int idx)
{
res[idx] = (byte)((v>>8)&0xFF);
res[idx + 1] = (byte)(v&0xFF);
}
public static int getInt2Bytes(byte[] arr, int idx)
{
int ret = arr[idx];
ret <<= 8;
ret |= getUnsignedByte(arr[idx + 1]);
return ret;
}
public static void writeBytes(String path, byte[] arr)
{
FileOutputStream out = null;
try {
out = new FileOutputStream(path, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(arr);
}catch(IOException e)
{
e.printStackTrace();
}
}
public static byte[] readBytes(String path)
{
File f = new File(path);
FileInputStream inp = null;
try{
inp = new FileInputStream(f);
}catch(IOException e)
{
e.printStackTrace();
}
byte[] res = new byte[(int)f.length()];
try {
inp.read(res);
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
public static int getUnsignedByte(byte b)
{
return (b&0xFF);
}
}