Refactoring + implementation of some methods
parent
c3b8c2c5e0
commit
5eb7e0deab
@ -0,0 +1,66 @@
|
||||
package tools;
|
||||
import exception.SizeError;
|
||||
/** Helper to manipulate byte[].
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class BytesArrayTools {
|
||||
/** Write int in a bytearray
|
||||
* @param array the array to write
|
||||
* @param start where to begin writting
|
||||
* @param value int to write
|
||||
*/
|
||||
public static void write(byte[] array, int start, int value) {
|
||||
for(int i=0;i<4;i++) {
|
||||
array[start + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF);
|
||||
}
|
||||
}
|
||||
/** Write long in a bytearray
|
||||
* @param array the array to write
|
||||
* @param start where to begin writting
|
||||
* @param value long to write
|
||||
*/
|
||||
public static void write(byte[] array, int start, long value) {
|
||||
for(int i=0;i<4;i++) {
|
||||
array[start + i] = (byte) ((size >> (8 * (4 - i))) & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
/** Read int from a bytearray
|
||||
* @param array the array to read
|
||||
* @param start where to begin reading
|
||||
* @return value read as int
|
||||
*/
|
||||
public static int readInt(byte[] array, int start) throws SizeError {
|
||||
int size = 0;
|
||||
for(int i=0;i<4;i++) {
|
||||
size |= ((int)array[start + i]) << (8* i);
|
||||
}
|
||||
if (size < 0) {
|
||||
// Size in array is probably correct
|
||||
// but we cannot store it into a int (or this will be negative)
|
||||
throw new SizeError();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
/** Read long from a bytearray
|
||||
* @param array the array to read
|
||||
* @param start where to begin reading
|
||||
* @return value read as long
|
||||
*/
|
||||
public static long readLong(byte[] array, int start) throws SizeError {
|
||||
long size = 0;
|
||||
for(int i=0;i<8;i++) {
|
||||
size |= ((int)array[start + i]) << (8* i);
|
||||
}
|
||||
if (size < 0) {
|
||||
// Size in array is probably correct
|
||||
// but we cannot store it into a int (or this will be negative)
|
||||
throw new SizeError();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue