|
|
|
@ -1,5 +1,10 @@
|
|
|
|
|
package tools;
|
|
|
|
|
import exception.SizeError;
|
|
|
|
|
import exception.ProtocolError;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Helper to manipulate byte[].
|
|
|
|
|
* @author Louis Royer
|
|
|
|
|
* @author Flavien Haas
|
|
|
|
@ -116,4 +121,129 @@ public class BytesArrayTools {
|
|
|
|
|
}
|
|
|
|
|
return new String(bArray, 0, cpt + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Write a list of string to a byte array. String are separated by a separator.
|
|
|
|
|
* @param byteArray array to write
|
|
|
|
|
* @param strArray string array to read
|
|
|
|
|
* @param start start position to write in byteArray
|
|
|
|
|
* @param separator separator to use
|
|
|
|
|
* @throws InternalError
|
|
|
|
|
*/
|
|
|
|
|
public static void write(byte[] byteArray, String[] strArray, int start, String separator) throws InternalError {
|
|
|
|
|
int bCount = start;
|
|
|
|
|
try {
|
|
|
|
|
byte[] sepb = separator.getBytes("UTF-8");
|
|
|
|
|
for(String s : strArray) {
|
|
|
|
|
// write separator
|
|
|
|
|
if (bCount != start) { // not on first iteration
|
|
|
|
|
for (byte b : sepb) {
|
|
|
|
|
byteArray[bCount] = b;
|
|
|
|
|
bCount += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Copy string
|
|
|
|
|
byte[] sb = s.getBytes("UTF-8");
|
|
|
|
|
for(byte b : sb) {
|
|
|
|
|
byteArray[bCount] = b;
|
|
|
|
|
bCount += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Read string array from byte array starting at start with size parameter.
|
|
|
|
|
* @param byteArray Array to read
|
|
|
|
|
* @param start Start position
|
|
|
|
|
* @param size Size to read
|
|
|
|
|
* @param separator Separator used
|
|
|
|
|
* @return String array output
|
|
|
|
|
* @throws InternalError
|
|
|
|
|
*/
|
|
|
|
|
public static String[] readStringArray(byte[] byteArray, int start, int size, String separator) throws InternalError {
|
|
|
|
|
try {
|
|
|
|
|
return (new String(byteArray, start, size, "UTF-8")).split(separator);
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Compute size of string array once converted to byte array with separator
|
|
|
|
|
* @param strArray String Array
|
|
|
|
|
* @param separator Separator used
|
|
|
|
|
* @return size
|
|
|
|
|
*/
|
|
|
|
|
public static int computeStringArraySize(String[] strArray, String separator) {
|
|
|
|
|
int size = 0;
|
|
|
|
|
for (String s: strArray) {
|
|
|
|
|
if (size != 0) {
|
|
|
|
|
size += separator.length();
|
|
|
|
|
}
|
|
|
|
|
size += s.length();
|
|
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Write a string to a byte array.
|
|
|
|
|
* @param array Byte Array to write
|
|
|
|
|
* @param str String to read
|
|
|
|
|
* @param start Start position in byte array to write
|
|
|
|
|
* @throws InternalError
|
|
|
|
|
*/
|
|
|
|
|
public static void write(byte[] array, String str, int start) throws InternalError {
|
|
|
|
|
int bCount = start;
|
|
|
|
|
try {
|
|
|
|
|
byte[] sb = str.getBytes("UTF-8");
|
|
|
|
|
for(byte b : sb) {
|
|
|
|
|
array[bCount] = b;
|
|
|
|
|
bCount += 1;
|
|
|
|
|
}
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Read string from byte array
|
|
|
|
|
* @param array Byte array to read
|
|
|
|
|
* @param start start position in byte array
|
|
|
|
|
* @param size size to read
|
|
|
|
|
* @return String read
|
|
|
|
|
* @throws InternalError
|
|
|
|
|
*/
|
|
|
|
|
public static String readString(byte[] array, int start, int size) throws InternalError {
|
|
|
|
|
try {
|
|
|
|
|
return new String(array, start, size, "UTF-8");
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Write byte Array to byte Array.
|
|
|
|
|
* @param dst Destination byte Array
|
|
|
|
|
* @param src Source byte Array
|
|
|
|
|
* @param start Start in dest array
|
|
|
|
|
*/
|
|
|
|
|
public static void write(byte[] dst, byte[] src, int start) {
|
|
|
|
|
int i = start;
|
|
|
|
|
for (byte b: src) {
|
|
|
|
|
dst[i] = b;
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Read byte Array to byte Array
|
|
|
|
|
* @param src Source byte Array
|
|
|
|
|
* @param start Start position in source
|
|
|
|
|
* @param end End position in source
|
|
|
|
|
* @return Resulting byte array
|
|
|
|
|
* @throws ProtocolError
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] readByteArray(byte[] src, int start, int end) throws ProtocolError {
|
|
|
|
|
try {
|
|
|
|
|
return Arrays.copyOfRange(src, start, end);
|
|
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
|
throw new ProtocolError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|