1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
4 * Licensed under the New BSD License (the "License"); you may not use *
5 * this file except in compliance with the License. Unless required *
6 * by applicable law or agreed to in writing, software distributed *
7 * under the License is distributed on an "AS IS" BASIS, WITHOUT *
8 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
9 * See the License for the specific language governing permissions and *
10 * limitations under the License. See accompanying LICENSE file. *
11 *======================================================================*/
13 package org
.lwes
.serializer
;
15 import org
.lwes
.Event
;
16 import org
.lwes
.util
.EncodedString
;
17 import org
.lwes
.util
.IPAddress
;
18 import org
.lwes
.util
.NumberCodec
;
20 import java
.math
.BigInteger
;
21 import java
.net
.Inet4Address
;
22 import java
.net
.InetAddress
;
23 import java
.util
.List
;
26 * This contains low level type serialization used by the
29 * @author Anthony Molinaro
31 public class Serializer
{
33 public static int serializeIPV4(Inet4Address value
, byte[] bytes
, int offset
) {
34 byte[] bs
= value
.getAddress();
42 public static int serializeDOUBLE(Double value
, byte[] bytes
, int offset
) {
43 long j
= Double
.doubleToLongBits(value
);
44 bytes
[offset
+ 7] = (byte) (j
>>> 0);
45 bytes
[offset
+ 6] = (byte) (j
>>> 8);
46 bytes
[offset
+ 5] = (byte) (j
>>> 16);
47 bytes
[offset
+ 4] = (byte) (j
>>> 24);
48 bytes
[offset
+ 3] = (byte) (j
>>> 32);
49 bytes
[offset
+ 2] = (byte) (j
>>> 40);
50 bytes
[offset
+ 1] = (byte) (j
>>> 48);
51 bytes
[offset
+ 0] = (byte) (j
>>> 56);
55 public static int serializeFLOAT(Float value
, byte[] bytes
, int offset
) {
56 int i
= Float
.floatToIntBits(value
);
57 bytes
[offset
+ 3] = (byte) (i
>>> 0);
58 bytes
[offset
+ 2] = (byte) (i
>>> 8);
59 bytes
[offset
+ 1] = (byte) (i
>>> 16);
60 bytes
[offset
+ 0] = (byte) (i
>>> 24);
64 public static int serializeBYTE(byte value
, byte[] bytes
, int offset
) {
65 bytes
[offset
] = value
;
69 public static int serializeBOOLEAN(boolean value
, byte[] bytes
, int offset
) {
70 bytes
[offset
] = (byte) (value ?
1 : 0);
74 public static int serializeUINT16(int value
, byte[] bytes
, int offset
) {
75 return serializeINT16((short) value
, bytes
, offset
);
78 public static int serializeINT16(short value
, byte[] bytes
, int offset
) {
79 bytes
[offset
+ 1] = (byte) (value
>>> 0);
80 bytes
[offset
+ 0] = (byte) (value
>>> 8);
84 public static int serializeUINT32(long anUnsignedInt
, byte[] bytes
, int offset
) {
85 bytes
[offset
] = (byte) ((anUnsignedInt
& 0xff000000) >> 24);
86 bytes
[offset
+ 1] = (byte) ((anUnsignedInt
& 0x00ff0000) >> 16);
87 bytes
[offset
+ 2] = (byte) ((anUnsignedInt
& 0x0000ff00) >> 8);
88 bytes
[offset
+ 3] = (byte) ((anUnsignedInt
& 0x000000ff) >> 0);
92 public static int serializeINT32(int value
, byte[] bytes
, int offset
) {
93 bytes
[offset
+ 3] = (byte) (value
>>> 0);
94 bytes
[offset
+ 2] = (byte) (value
>>> 8);
95 bytes
[offset
+ 1] = (byte) (value
>>> 16);
96 bytes
[offset
+ 0] = (byte) (value
>>> 24);
100 public static int serializeINT64(long value
, byte[] bytes
, int offset
) {
101 bytes
[offset
+ 7] = (byte) (value
>>> 0);
102 bytes
[offset
+ 6] = (byte) (value
>>> 8);
103 bytes
[offset
+ 5] = (byte) (value
>>> 16);
104 bytes
[offset
+ 4] = (byte) (value
>>> 24);
105 bytes
[offset
+ 3] = (byte) (value
>>> 32);
106 bytes
[offset
+ 2] = (byte) (value
>>> 40);
107 bytes
[offset
+ 1] = (byte) (value
>>> 48);
108 bytes
[offset
+ 0] = (byte) (value
>>> 56);
112 public static int serializeUINT64(long anInt
, byte[] bytes
, int offset
) {
113 NumberCodec
.encodeLongUnchecked(anInt
, bytes
, offset
);
117 public static int serializeUINT64(BigInteger anInt
, byte[] bytes
, int offset
) {
118 // TODO: write a BigInteger serialization method
119 NumberCodec
.encodeLongUnchecked(anInt
.longValue(), bytes
, offset
);
126 public static int serializeSTRING(String aString
, byte[] bytes
, int offset
) {
127 return serializeSTRING(aString
, bytes
, offset
, Event
.DEFAULT_ENCODING
);
130 public static int serializeSTRING(String aString
, byte[] bytes
, int offset
,
133 EncodedString
.getBytes(aString
, Event
.ENCODING_STRINGS
[encoding
]);
134 int length
= stringBytes
.length
;
135 if (length
< 65535 && length
>= 0) {
136 offset
+= serializeUINT16(length
, bytes
, offset
);
137 System
.arraycopy(stringBytes
, 0, bytes
, offset
, length
);
145 * String arrays are serialized as follows:
146 * <array_name_len><array_name_bytes><type>
147 * <array_length><serialized_type_1>...<serialized_type_n>
155 public static int serializeStringArray(List
<String
> value
,
161 int offsetStart
= offset
;
162 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
164 for (String s
: value
) {
165 numbytes
= serializeSTRING(s
, bytes
, offset
, encoding
);
168 return (offset
- offsetStart
);
171 public static int serializeInt16Array(List
<Short
> value
,
175 int offsetStart
= offset
;
176 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
178 for (short s
: value
) {
179 numbytes
= serializeINT16(s
, bytes
, offset
);
182 return (offset
- offsetStart
);
185 public static int serializeInt32Array(List
<Integer
> value
,
189 int offsetStart
= offset
;
190 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
192 for (int s
: value
) {
193 numbytes
= serializeINT32(s
, bytes
, offset
);
196 return (offset
- offsetStart
);
199 public static int serializeInt64Array(List
<Long
> value
,
203 int offsetStart
= offset
;
204 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
206 for (long s
: value
) {
207 numbytes
= serializeINT64(s
, bytes
, offset
);
210 return (offset
- offsetStart
);
213 public static int serializeUInt16Array(List
<Integer
> value
,
217 int offsetStart
= offset
;
218 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
220 for (int s
: value
) {
221 numbytes
= serializeUINT16(s
, bytes
, offset
);
224 return (offset
- offsetStart
);
227 public static int serializeUInt32Array(List
<Long
> value
,
231 int offsetStart
= offset
;
232 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
234 for (long s
: value
) {
235 numbytes
= serializeUINT32(s
, bytes
, offset
);
238 return (offset
- offsetStart
);
241 public static int serializeUInt64Array(List
<Long
> value
,
245 int offsetStart
= offset
;
246 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
248 for (long s
: value
) {
249 numbytes
= serializeUINT64(s
, bytes
, offset
);
252 return (offset
- offsetStart
);
255 public static int serializeBooleanArray(List
<Boolean
> value
,
259 int offsetStart
= offset
;
260 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
262 for (boolean s
: value
) {
263 numbytes
= serializeBOOLEAN(s
, bytes
, offset
);
266 return (offset
- offsetStart
);
269 public static int serializeByteArray(List
<Byte
> value
,
273 int offsetStart
= offset
;
274 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
276 for (byte s
: value
) {
277 numbytes
= serializeBYTE(s
, bytes
, offset
);
280 return (offset
- offsetStart
);
283 public static int serializeDoubleArray(List
<Double
> value
, byte[] bytes
, int offset
) {
285 int offsetStart
= offset
;
286 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
288 for (double s
: value
) {
289 numbytes
= serializeDOUBLE(s
, bytes
, offset
);
292 return (offset
- offsetStart
);
295 public static int serializeFloatArray(List
<Float
> value
, byte[] bytes
, int offset
) {
297 int offsetStart
= offset
;
298 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
300 for (float s
: value
) {
301 numbytes
= serializeFLOAT(s
, bytes
, offset
);
304 return (offset
- offsetStart
);
307 public static int serializeIPV4Array(List
<InetAddress
> value
, byte[] bytes
, int offset
) {
309 int offsetStart
= offset
;
310 numbytes
= serializeUINT16(value
.size(), bytes
, offset
);
312 for (InetAddress s
: value
) {
313 numbytes
= serializeIPV4(s
, bytes
, offset
);
316 return (offset
- offsetStart
);
319 public static int serializeEVENTWORD(String aString
, byte[] bytes
, int offset
) {
320 return serializeEVENTWORD(aString
, bytes
, offset
, Event
.DEFAULT_ENCODING
);
323 private static int serializeEVENTWORD(String aString
,
328 EncodedString
.getBytes(aString
, Event
.ENCODING_STRINGS
[encoding
]);
329 int length
= stringBytes
.length
;
330 if (length
< 255 && length
> 0) {
331 offset
+= serializeBYTE((byte) length
, bytes
, offset
);
332 System
.arraycopy(stringBytes
, 0, bytes
, offset
, length
);
339 public static int serializeATTRIBUTEWORD(String aString
, byte[] bytes
,
341 return serializeEVENTWORD(aString
, bytes
, offset
, Event
.DEFAULT_ENCODING
);
345 * Serialize IPAddress in *reverse* network order. Don't use this.
347 * @param anIPAddress the ip address to serialize
348 * @param bytes the byte array to modify
349 * @param offset what index in the array to start at
350 * @return how many bytes were set in the array
353 public static int serializeIPADDR(IPAddress anIPAddress
, byte[] bytes
, int offset
) {
354 byte[] inetaddr
= anIPAddress
.getInetAddressAsBytes();
355 bytes
[offset
+ 3] = inetaddr
[0];
356 bytes
[offset
+ 2] = inetaddr
[1];
357 bytes
[offset
+ 1] = inetaddr
[2];
358 bytes
[offset
] = inetaddr
[3];
363 * Serializes an ip address in *reverse* network order. Don't use this.
365 * @param anIPAddress the ip address to serialize
366 * @param bytes the byte array to modify
367 * @param offset what index in the array to start at
368 * @return how many bytes were set in the array
371 public static int serializeIPADDR(InetAddress anIPAddress
, byte[] bytes
, int offset
) {
372 byte[] inetaddr
= anIPAddress
.getAddress();
373 bytes
[offset
+ 3] = inetaddr
[0];
374 bytes
[offset
+ 2] = inetaddr
[1];
375 bytes
[offset
+ 1] = inetaddr
[2];
376 bytes
[offset
] = inetaddr
[3];
381 * Serialize InetAddress in network order.
383 * @param ip the ip address to serialize
384 * @param bytes the byte array to modify
385 * @param offset what index in the array to start at
386 * @return how many bytes were set in the array
388 public static int serializeIPV4(InetAddress ip
, byte[] bytes
, int offset
) {
389 byte[] inetaddr
= ip
.getAddress();
390 bytes
[offset
] = inetaddr
[0];
391 bytes
[offset
+ 1] = inetaddr
[1];
392 bytes
[offset
+ 2] = inetaddr
[2];
393 bytes
[offset
+ 3] = inetaddr
[3];