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
| /*
* File : $DataBufferizer.java$
* Version : $1.1$
* Date : $Wed Mar 18 10:37:54 CET 2009$
*/
public class DataBufferizer {
private static char[] digits = {'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
//------------------------------------------------------------
public static boolean readBoolean(byte[] buffer, int offset) {
return (buffer[offset] != 0);
}
//------------------------------------------------------------
public static void writeBoolean(boolean v, byte[] buffer, int offset) {
if (v) buffer[offset] = 1;
else buffer[offset] = 0;
}
//------------------------------------------------------------
public static byte readByte(byte[] buffer, int offset) {
return buffer[offset];
}
//------------------------------------------------------------
public static void writeByte(byte v, byte[] buffer, int offset) {
buffer[offset] = v;
}
//------------------------------------------------------------
public static short readUnsignedByte(byte[] buffer, int offset) {
short val = (short)(buffer[offset] & 0x00FF);
return val;
}
//------------------------------------------------------------
public static void writeUnsignedByte(short v, byte[] buffer, int offset) {
buffer[offset] = (byte)(0x00FF & v);
}
//------------------------------------------------------------
public static byte[] readByteArray(byte[] buffer, int offset, int length) {
byte[] val = new byte[length];
System.arraycopy(buffer,offset,val,0,length);
return val;
}
//------------------------------------------------------------
public static void writeByteArray(byte[] v, byte[] buffer, int offset, int length) {
System.arraycopy(v,0,buffer,offset,length);
}
//------------------------------------------------------------
public static char readChar(byte[] buffer, int offset) {
int val = buffer[offset] << 8;
val |= buffer[offset+1];
return ((char)val);
}
//------------------------------------------------------------
public static void writeChar(char v, byte[] buffer, int offset) {
buffer[offset] = (byte)((v & 0xFF00) >> 8);
buffer[offset+1] = (byte)((v & 0x00FF));
}
//------------------------------------------------------------
public static double readDouble(byte[] buffer, int offset) {
return Double.longBitsToDouble(readLong(buffer, offset));
}
//------------------------------------------------------------
public static void writeDouble(double v, byte[] buffer, int offset) {
writeLong(Double.doubleToLongBits(v), buffer, offset);
}
//------------------------------------------------------------
public static float readFloat(byte[] buffer, int offset) {
return Float.intBitsToFloat(readInt(buffer, offset));
}
//------------------------------------------------------------
public static void writeFloat(float v, byte[] buffer, int offset) {
writeInt(Float.floatToIntBits(v), buffer, offset);
}
//------------------------------------------------------------
public static int readInt(byte[] buffer, int offset) {
int value = ((int)buffer[offset] & 0xFF) << 24;
value |= ((int)buffer[offset+1] & 0xFF) << 16;
value |= ((int)buffer[offset+2] & 0xFF) << 8;
value |= ((int)buffer[offset+3] & 0xFF);
return value;
}
//------------------------------------------------------------
public static void writeInt(int v, byte[] buffer, int offset) {
buffer[offset] = (byte)(0xFF & (v >> 24));
buffer[offset+1] = (byte)(0xFF & (v >> 16));
buffer[offset+2] = (byte)(0xFF & (v >> 8));
buffer[offset+3] = (byte)(0xFF & v);
}
//------------------------------------------------------------
public static long readLong(byte[] buffer, int offset) {
return ((long)readInt(buffer, offset) << 32)
| ((long)readInt(buffer, offset+4) & 0xffffffffL);
}
//------------------------------------------------------------
public static void writeLong(long v, byte[] buffer, int offset) {
int hiInt=(int)(v >> 32);
int loInt=(int)(v & 0xFFFFFFFF);
writeInt(hiInt, buffer, offset);
writeInt(loInt, buffer, offset+4);
}
//------------------------------------------------------------
public static short readShort(byte[] buffer, int offset) {
short val = (short)((buffer[offset] & 0xFF) << 8);
val |= ((short)buffer[offset+1] & 0xFF);
return val;
}
//------------------------------------------------------------
public static void writeShort(short v, byte[] buffer, int offset) {
buffer[offset] = (byte)(0xFF & (v >> 8));
buffer[offset+1] = (byte)(0xFF & v);
}
//------------------------------------------------------------
public static short getBytesLenght(String str) {
return (short)str.getBytes().length;
}
//------------------------------------------------------------
public static String readString(byte[] buffer, int offset) {
short length = readUnsignedByte(buffer, offset);
return new String(buffer, offset+1, length);
}
//------------------------------------------------------------
public static void writeString(String str, byte[] buffer, int offset) {
byte[] strBuffer = str.getBytes();
int length = strBuffer.length;
if (length > Short.MAX_VALUE) length = Short.MAX_VALUE;
writeUnsignedByte((short)length, buffer, offset);
System.arraycopy(strBuffer, 0, buffer, offset+1, length);
}
//------------------------------------------------------------
public static String bufferToString(byte[] buffer) {
return bufferToString(buffer, 0, buffer.length);
}
//------------------------------------------------------------
public static String bufferToString(byte[] buffer,
int offset,
int length) {
StringBuffer result = new StringBuffer();
int max = (length+offset>buffer.length?buffer.length:length+offset);
for (int i=offset; i<max; i++)
result.append(hexa(buffer[i]) + " ");
return result.toString();
}
// ------------------------------------------------------------
public static String bufferToBinaryString(byte[] buffer) {
return bufferToBinaryString(buffer, 0, buffer.length);
}
//------------------------------------------------------------
public static String bufferToBinaryString(byte[] buffer,
int offset,
int length) {
StringBuffer result = new StringBuffer();
int max = (length+offset>buffer.length?buffer.length:length+offset);
for (int i=offset; i<max; i++)
result.append(bin(buffer[i]) + " ");
return result.toString();
}
//------------------------------------------------------------
public static String hexa(byte v) {
int low = v & 0x0F;
int high = (v & 0xF0) >> 4;
return String.valueOf(digits[high]) + String.valueOf(digits[low]);
}
//------------------------------------------------------------
public static String bin(byte v) {
String s = "";
for (int i=0;i<8;i++) {
if ((v & 0x01) == 1) s = "1" + s; else s = "0" + s;
v = (byte)(v >> 1);
}
return s;
}
//------------------------------------------------------------
public static boolean compareBuffer(byte[] sourceBuffer,
int sourceOffset,
byte[] toBuffer,
int toOffset,
int length) {
for (int i=0; i<length; i++)
if (toBuffer[toOffset + i] != sourceBuffer[sourceOffset + i])
return false;
return true;
}
} |
Partager