1 package org
.lwes
.serializer
;
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
;
14 * This contains low level type serialization used by the
17 * @author Anthony Molinaro
19 public class Serializer
{
21 public static int serializeIPV4(Inet4Address value
, byte[] bytes
, int offset
) {
22 byte[] bs
= value
.getAddress();
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);
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);
52 public static int serializeBYTE(byte value
, byte[] bytes
, int offset
) {
53 bytes
[offset
] = value
;
57 public static int serializeBOOLEAN(boolean value
, byte[] bytes
, int offset
) {
58 bytes
[offset
] = (byte) (value ?
1 : 0);
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);
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);
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);
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);
100 public static int serializeUINT64(long anInt
, byte[] bytes
, int offset
) {
101 NumberCodec
.encodeLongUnchecked(anInt
, bytes
, offset
);
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
);
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
,
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
);
133 * String arrays are serialized as follows:
134 * <array_name_len><array_name_bytes><type>
135 * <array_length><serialized_type_1>...<serialized_type_n>
143 public static int serializeStringArray(List
<String
> value
,
149 int offsetStart
= offset
;
150 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
152 for (String s
: value
) {
153 numbytes
= serializeSTRING(s
, bytes
, offset
, encoding
);
156 return (offset
- offsetStart
);
159 public static int serializeInt16Array(List
<Short
> value
,
163 int offsetStart
= offset
;
164 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
166 for (short s
: value
) {
167 numbytes
= serializeINT16(s
, bytes
, offset
);
170 return (offset
- offsetStart
);
173 public static int serializeInt32Array(List
<Integer
> value
,
177 int offsetStart
= offset
;
178 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
180 for (int s
: value
) {
181 numbytes
= serializeINT32(s
, bytes
, offset
);
184 return (offset
- offsetStart
);
187 public static int serializeInt64Array(List
<Long
> value
,
191 int offsetStart
= offset
;
192 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
194 for (long s
: value
) {
195 numbytes
= serializeINT64(s
, bytes
, offset
);
198 return (offset
- offsetStart
);
201 public static int serializeUInt16Array(List
<Integer
> value
,
205 int offsetStart
= offset
;
206 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
208 for (int s
: value
) {
209 numbytes
= serializeUINT16(s
, bytes
, offset
);
212 return (offset
- offsetStart
);
215 public static int serializeUInt32Array(List
<Long
> value
,
219 int offsetStart
= offset
;
220 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
222 for (long s
: value
) {
223 numbytes
= serializeUINT32(s
, bytes
, offset
);
226 return (offset
- offsetStart
);
229 public static int serializeUInt64Array(List
<Long
> value
,
233 int offsetStart
= offset
;
234 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
236 for (long s
: value
) {
237 numbytes
= serializeUINT64(s
, bytes
, offset
);
240 return (offset
- offsetStart
);
243 public static int serializeBooleanArray(List
<Boolean
> value
,
247 int offsetStart
= offset
;
248 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
250 for (boolean s
: value
) {
251 numbytes
= serializeBOOLEAN(s
, bytes
, offset
);
254 return (offset
- offsetStart
);
257 public static int serializeByteArray(List
<Byte
> value
,
261 int offsetStart
= offset
;
262 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
264 for (byte s
: value
) {
265 numbytes
= serializeBYTE(s
, bytes
, offset
);
268 return (offset
- offsetStart
);
271 public static int serializeDoubleArray(List
<Double
> value
, byte[] bytes
, int offset
) {
273 int offsetStart
= offset
;
274 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
276 for (double s
: value
) {
277 numbytes
= serializeDOUBLE(s
, bytes
, offset
);
280 return (offset
- offsetStart
);
283 public static int serializeFloatArray(List
<Float
> value
, byte[] bytes
, int offset
) {
285 int offsetStart
= offset
;
286 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
288 for (float s
: value
) {
289 numbytes
= serializeFLOAT(s
, bytes
, offset
);
292 return (offset
- offsetStart
);
295 public static int serializeIPV4Array(List
<InetAddress
> value
, byte[] bytes
, int offset
) {
297 int offsetStart
= offset
;
298 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
300 for (InetAddress s
: value
) {
301 numbytes
= serializeIPV4(s
, bytes
, offset
);
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
,
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
);
327 public static int serializeATTRIBUTEWORD(String aString
, byte[] bytes
,
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
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];
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
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];
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];