| 93 |
Kevin |
1 |
import java.nio.ByteBuffer;
|
|
|
2 |
|
|
|
3 |
// Assisting class for MemClient to convert values to a byte array and vice versa
|
|
|
4 |
public class ByteStringConverter {
|
|
|
5 |
// Converts the passed string array into a byte array
|
|
|
6 |
public static byte[] convertToByteArray(int int1, int int2, String str) {
|
|
|
7 |
// Use ByteBuffer to serialize to bytes
|
|
|
8 |
byte[] byteArray = new byte[8 + str.length()];
|
|
|
9 |
byte[] bstr = str.getBytes();
|
|
|
10 |
|
|
|
11 |
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
|
|
|
12 |
|
|
|
13 |
// Insert passed values into the byte array
|
|
|
14 |
buffer.putInt(int1);
|
|
|
15 |
buffer.putInt(int2);
|
|
|
16 |
buffer.put(bstr);
|
|
|
17 |
|
|
|
18 |
return byteArray;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
// Converts the byte array into a string array
|
|
|
22 |
public static String[] convertToStringArray(byte[] byteArray) {
|
|
|
23 |
// Array of values to return from byte array
|
|
|
24 |
String[] strArray = new String[3];
|
|
|
25 |
|
|
|
26 |
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
|
|
|
27 |
|
|
|
28 |
int int1 = buffer.getInt();
|
|
|
29 |
int int2 = buffer.getInt();
|
|
|
30 |
byte[] bstr = new byte[byteArray.length - 8];
|
|
|
31 |
buffer.get(bstr);
|
|
|
32 |
|
|
|
33 |
// Pull values from byte array into string array
|
|
|
34 |
strArray[0] = Integer.toString(int1);
|
|
|
35 |
strArray[1] = Integer.toString(int2);
|
|
|
36 |
strArray[2] = new String(bstr);
|
|
|
37 |
|
|
|
38 |
return strArray;
|
|
|
39 |
}
|
|
|
40 |
}
|