added ipv4 type
[lwes-java.git] / src / main / java / org / lwes / serializer / Serializer.java
blob262d8b5dbf085610b1cd651a284441cea3033fb8
1 package org.lwes.serializer;
3 import org.lwes.Event;
4 import org.lwes.util.EncodedString;
5 import org.lwes.util.IPAddress;
6 import org.lwes.util.NumberCodec;
8 import java.math.BigInteger;
9 import java.net.Inet4Address;
10 import java.net.InetAddress;
11 import java.util.List;
13 /**
14 * This contains low level type serialization used by the
15 * rest of the system.
17 * @author Anthony Molinaro
19 public class Serializer {
21 public static int serializeIPV4(Inet4Address value, byte[] bytes, int offset) {
22 byte[] bs = value.getAddress();
23 int i = offset;
24 for (byte b : bs) {
25 bytes[i++] = b;
27 return offset - i;
30 public static int serializeDOUBLE(Double value, byte[] bytes, int offset) {
31 long j = Double.doubleToLongBits(value);
32 bytes[offset + 7] = (byte) (j >>> 0);
33 bytes[offset + 6] = (byte) (j >>> 8);
34 bytes[offset + 5] = (byte) (j >>> 16);
35 bytes[offset + 4] = (byte) (j >>> 24);
36 bytes[offset + 3] = (byte) (j >>> 32);
37 bytes[offset + 2] = (byte) (j >>> 40);
38 bytes[offset + 1] = (byte) (j >>> 48);
39 bytes[offset + 0] = (byte) (j >>> 56);
40 return 8;
43 public static int serializeFLOAT(Float value, byte[] bytes, int offset) {
44 int i = Float.floatToIntBits(value);
45 bytes[offset + 3] = (byte) (i >>> 0);
46 bytes[offset + 2] = (byte) (i >>> 8);
47 bytes[offset + 1] = (byte) (i >>> 16);
48 bytes[offset + 0] = (byte) (i >>> 24);
49 return 4;
52 public static int serializeBYTE(byte value, byte[] bytes, int offset) {
53 bytes[offset] = value;
54 return 1;
57 public static int serializeBOOLEAN(boolean value, byte[] bytes, int offset) {
58 bytes[offset] = (byte) (value ? 1 : 0);
59 return 1;
62 public static int serializeUINT16(int value, byte[] bytes, int offset) {
63 return serializeINT16((short) value, bytes, offset);
66 public static int serializeINT16(short value, byte[] bytes, int offset) {
67 bytes[offset + 1] = (byte) (value >>> 0);
68 bytes[offset + 0] = (byte) (value >>> 8);
69 return 2;
72 public static int serializeUINT32(long anUnsignedInt, byte[] bytes, int offset) {
73 bytes[offset] = (byte) ((anUnsignedInt & 0xff000000) >> 24);
74 bytes[offset + 1] = (byte) ((anUnsignedInt & 0x00ff0000) >> 16);
75 bytes[offset + 2] = (byte) ((anUnsignedInt & 0x0000ff00) >> 8);
76 bytes[offset + 3] = (byte) ((anUnsignedInt & 0x000000ff) >> 0);
77 return (4);
80 public static int serializeINT32(int value, byte[] bytes, int offset) {
81 bytes[offset + 3] = (byte) (value >>> 0);
82 bytes[offset + 2] = (byte) (value >>> 8);
83 bytes[offset + 1] = (byte) (value >>> 16);
84 bytes[offset + 0] = (byte) (value >>> 24);
85 return 4;
88 public static int serializeINT64(long value, byte[] bytes, int offset) {
89 bytes[offset + 7] = (byte) (value >>> 0);
90 bytes[offset + 6] = (byte) (value >>> 8);
91 bytes[offset + 5] = (byte) (value >>> 16);
92 bytes[offset + 4] = (byte) (value >>> 24);
93 bytes[offset + 3] = (byte) (value >>> 32);
94 bytes[offset + 2] = (byte) (value >>> 40);
95 bytes[offset + 1] = (byte) (value >>> 48);
96 bytes[offset + 0] = (byte) (value >>> 56);
97 return 8;
100 public static int serializeUINT64(long anInt, byte[] bytes, int offset) {
101 NumberCodec.encodeLongUnchecked(anInt, bytes, offset);
102 return (8);
105 public static int serializeUINT64(BigInteger anInt, byte[] bytes, int offset) {
106 // TODO: write a BigInteger serialization method
107 NumberCodec.encodeLongUnchecked(anInt.longValue(), bytes, offset);
108 return (8);
112 * @deprecated
114 public static int serializeSTRING(String aString, byte[] bytes, int offset) {
115 return serializeSTRING(aString, bytes, offset, Event.DEFAULT_ENCODING);
118 public static int serializeSTRING(String aString, byte[] bytes, int offset,
119 short encoding) {
120 byte[] stringBytes =
121 EncodedString.getBytes(aString, Event.ENCODING_STRINGS[encoding]);
122 int length = stringBytes.length;
123 if (length < 65535 && length >= 0) {
124 offset += serializeUINT16(length, bytes, offset);
125 System.arraycopy(stringBytes, 0, bytes, offset, length);
126 return (length + 2);
128 return 0;
133 * String arrays are serialized as follows:
134 * <array_name_len><array_name_bytes><type>
135 * <array_length><serialized_type_1>...<serialized_type_n>
137 * @param value
138 * @param bytes
139 * @param offset
140 * @param encoding
141 * @return
143 public static int serializeStringArray(List<String> value,
144 byte[] bytes,
145 int offset,
146 short encoding) {
148 int numbytes = 0;
149 int offsetStart = offset;
150 numbytes = serializeUINT16(value.size(), bytes, offset);
151 offset += numbytes;
152 for (String s : value) {
153 numbytes = serializeSTRING(s, bytes, offset, encoding);
154 offset += numbytes;
156 return (offset - offsetStart);
159 public static int serializeInt16Array(List<Short> value,
160 byte[] bytes,
161 int offset) {
162 int numbytes = 0;
163 int offsetStart = offset;
164 numbytes = serializeUINT16(value.size(), bytes, offset);
165 offset += numbytes;
166 for (short s : value) {
167 numbytes = serializeINT16(s, bytes, offset);
168 offset += numbytes;
170 return (offset - offsetStart);
173 public static int serializeInt32Array(List<Integer> value,
174 byte[] bytes,
175 int offset) {
176 int numbytes = 0;
177 int offsetStart = offset;
178 numbytes = serializeUINT16(value.size(), bytes, offset);
179 offset += numbytes;
180 for (int s : value) {
181 numbytes = serializeINT32(s, bytes, offset);
182 offset += numbytes;
184 return (offset - offsetStart);
187 public static int serializeInt64Array(List<Long> value,
188 byte[] bytes,
189 int offset) {
190 int numbytes = 0;
191 int offsetStart = offset;
192 numbytes = serializeUINT16(value.size(), bytes, offset);
193 offset += numbytes;
194 for (long s : value) {
195 numbytes = serializeINT64(s, bytes, offset);
196 offset += numbytes;
198 return (offset - offsetStart);
201 public static int serializeUInt16Array(List<Integer> value,
202 byte[] bytes,
203 int offset) {
204 int numbytes = 0;
205 int offsetStart = offset;
206 numbytes = serializeUINT16(value.size(), bytes, offset);
207 offset += numbytes;
208 for (int s : value) {
209 numbytes = serializeUINT16(s, bytes, offset);
210 offset += numbytes;
212 return (offset - offsetStart);
215 public static int serializeUInt32Array(List<Long> value,
216 byte[] bytes,
217 int offset) {
218 int numbytes = 0;
219 int offsetStart = offset;
220 numbytes = serializeUINT16(value.size(), bytes, offset);
221 offset += numbytes;
222 for (long s : value) {
223 numbytes = serializeUINT32(s, bytes, offset);
224 offset += numbytes;
226 return (offset - offsetStart);
229 public static int serializeUInt64Array(List<Long> value,
230 byte[] bytes,
231 int offset) {
232 int numbytes = 0;
233 int offsetStart = offset;
234 numbytes = serializeUINT16(value.size(), bytes, offset);
235 offset += numbytes;
236 for (long s : value) {
237 numbytes = serializeUINT64(s, bytes, offset);
238 offset += numbytes;
240 return (offset - offsetStart);
243 public static int serializeBooleanArray(List<Boolean> value,
244 byte[] bytes,
245 int offset) {
246 int numbytes = 0;
247 int offsetStart = offset;
248 numbytes = serializeUINT16(value.size(), bytes, offset);
249 offset += numbytes;
250 for (boolean s : value) {
251 numbytes = serializeBOOLEAN(s, bytes, offset);
252 offset += numbytes;
254 return (offset - offsetStart);
257 public static int serializeByteArray(List<Byte> value,
258 byte[] bytes,
259 int offset) {
260 int numbytes = 0;
261 int offsetStart = offset;
262 numbytes = serializeUINT16(value.size(), bytes, offset);
263 offset += numbytes;
264 for (byte s : value) {
265 numbytes = serializeBYTE(s, bytes, offset);
266 offset += numbytes;
268 return (offset - offsetStart);
271 public static int serializeDoubleArray(List<Double> value, byte[] bytes, int offset) {
272 int numbytes = 0;
273 int offsetStart = offset;
274 numbytes = serializeUINT16(value.size(), bytes, offset);
275 offset += numbytes;
276 for (double s : value) {
277 numbytes = serializeDOUBLE(s, bytes, offset);
278 offset += numbytes;
280 return (offset - offsetStart);
283 public static int serializeFloatArray(List<Float> value, byte[] bytes, int offset) {
284 int numbytes = 0;
285 int offsetStart = offset;
286 numbytes = serializeUINT16(value.size(), bytes, offset);
287 offset += numbytes;
288 for (float s : value) {
289 numbytes = serializeFLOAT(s, bytes, offset);
290 offset += numbytes;
292 return (offset - offsetStart);
295 public static int serializeIPV4Array(List<InetAddress> value, byte[] bytes, int offset) {
296 int numbytes = 0;
297 int offsetStart = offset;
298 numbytes = serializeUINT16(value.size(), bytes, offset);
299 offset += numbytes;
300 for (InetAddress s : value) {
301 numbytes = serializeIPV4(s, bytes, offset);
302 offset += numbytes;
304 return (offset - offsetStart);
307 public static int serializeEVENTWORD(String aString, byte[] bytes, int offset) {
308 return serializeEVENTWORD(aString, bytes, offset, Event.DEFAULT_ENCODING);
311 private static int serializeEVENTWORD(String aString,
312 byte[] bytes,
313 int offset,
314 short encoding) {
315 byte[] stringBytes =
316 EncodedString.getBytes(aString, Event.ENCODING_STRINGS[encoding]);
317 int length = stringBytes.length;
318 if (length < 255 && length > 0) {
319 offset += serializeBYTE((byte) length, bytes, offset);
320 System.arraycopy(stringBytes, 0, bytes, offset, length);
321 return (length + 1);
323 return 0;
327 public static int serializeATTRIBUTEWORD(String aString, byte[] bytes,
328 int offset) {
329 return serializeEVENTWORD(aString, bytes, offset, Event.DEFAULT_ENCODING);
333 * Serialize IPAddress in *reverse* network order. Don't use this.
335 * @param anIPAddress the ip address to serialize
336 * @param bytes the byte array to modify
337 * @param offset what index in the array to start at
338 * @return how many bytes were set in the array
339 * @deprecated
341 public static int serializeIPADDR(IPAddress anIPAddress, byte[] bytes, int offset) {
342 byte[] inetaddr = anIPAddress.getInetAddressAsBytes();
343 bytes[offset + 3] = inetaddr[0];
344 bytes[offset + 2] = inetaddr[1];
345 bytes[offset + 1] = inetaddr[2];
346 bytes[offset] = inetaddr[3];
347 return (4);
351 * Serializes an ip address in *reverse* network order. Don't use this.
353 * @param anIPAddress the ip address to serialize
354 * @param bytes the byte array to modify
355 * @param offset what index in the array to start at
356 * @return how many bytes were set in the array
357 * @deprecated
359 public static int serializeIPADDR(InetAddress anIPAddress, byte[] bytes, int offset) {
360 byte[] inetaddr = anIPAddress.getAddress();
361 bytes[offset + 3] = inetaddr[0];
362 bytes[offset + 2] = inetaddr[1];
363 bytes[offset + 1] = inetaddr[2];
364 bytes[offset] = inetaddr[3];
365 return (4);
369 * Serialize InetAddress in network order.
371 * @param ip the ip address to serialize
372 * @param bytes the byte array to modify
373 * @param offset what index in the array to start at
374 * @return how many bytes were set in the array
376 public static int serializeIPV4(InetAddress ip, byte[] bytes, int offset) {
377 byte[] inetaddr = ip.getAddress();
378 bytes[offset] = inetaddr[0];
379 bytes[offset + 1] = inetaddr[1];
380 bytes[offset + 2] = inetaddr[2];
381 bytes[offset + 3] = inetaddr[3];
382 return (4);