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
.InetAddress
;
12 * This contains low level type serialization used by the
15 * @author Anthony Molinaro
17 public class Serializer
{
19 public static int serializeBYTE(byte aByte
, byte[] bytes
, int offset
) {
20 bytes
[offset
] = aByte
;
24 public static int serializeBOOLEAN(boolean aBoolean
, byte[] bytes
, int offset
) {
26 bytes
[offset
] = (byte) 0x01;
29 bytes
[offset
] = (byte) 0x00;
34 public static int serializeUINT16(int anUnsignedShortInt
, byte[] bytes
,
36 bytes
[offset
] = (byte) ((anUnsignedShortInt
& (255 << 8)) >> 8);
37 bytes
[offset
+ 1] = (byte) ((anUnsignedShortInt
& (255 << 0)) >> 0);
41 public static int serializeINT16(short aShortInt
, byte[] bytes
, int offset
) {
42 bytes
[offset
] = (byte) ((aShortInt
& (255 << 8)) >> 8);
43 bytes
[offset
+ 1] = (byte) ((aShortInt
& (255 << 0)) >> 0);
47 public static int serializeUINT32(long anUnsignedInt
, byte[] bytes
,
49 bytes
[offset
] = (byte) ((anUnsignedInt
& 0xff000000) >> 24);
50 bytes
[offset
+ 1] = (byte) ((anUnsignedInt
& 0x00ff0000) >> 16);
51 bytes
[offset
+ 2] = (byte) ((anUnsignedInt
& 0x0000ff00) >> 8);
52 bytes
[offset
+ 3] = (byte) ((anUnsignedInt
& 0x000000ff) >> 0);
56 public static int serializeINT32(int anInt
, byte[] bytes
,
58 bytes
[offset
] = (byte) ((anInt
& (255 << 24)) >> 24);
59 bytes
[offset
+ 1] = (byte) ((anInt
& (255 << 16)) >> 16);
60 bytes
[offset
+ 2] = (byte) ((anInt
& (255 << 8)) >> 8);
61 bytes
[offset
+ 3] = (byte) ((anInt
& (255 << 0)) >> 0);
65 public static int serializeINT64(long anInt
, byte[] bytes
, int offset
) {
66 NumberCodec
.encodeLongUnchecked(anInt
, bytes
, offset
);
70 public static int serializeUINT64(long anInt
, byte[] bytes
, int offset
) {
71 NumberCodec
.encodeLongUnchecked(anInt
, bytes
, offset
);
75 public static int serializeUINT64(BigInteger anInt
, byte[] bytes
, int offset
) {
76 // TODO: write a BigInteger serialization method
77 NumberCodec
.encodeLongUnchecked(anInt
.longValue(), bytes
, offset
);
84 public static int serializeSTRING(String aString
, byte[] bytes
, int offset
) {
85 return serializeSTRING(aString
, bytes
, offset
, Event
.DEFAULT_ENCODING
);
88 public static int serializeSTRING(String aString
, byte[] bytes
, int offset
,
91 EncodedString
.getBytes(aString
, Event
.ENCODING_STRINGS
[encoding
]);
92 int length
= stringBytes
.length
;
93 if (length
< 65535 && length
>= 0) {
94 offset
+= serializeUINT16(length
, bytes
, offset
);
95 System
.arraycopy(stringBytes
, 0, bytes
, offset
, length
);
103 * String arrays are serialized as follows:
104 * <array_name_len><array_name_bytes><type>
105 * <array_length><serialized_type_1>...<serialized_type_n>
113 public static int serializeStringArray(String
[] value
,
119 int offsetStart
= offset
;
120 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
122 for (String s
: value
) {
123 numbytes
= serializeSTRING(s
, bytes
, offset
, encoding
);
126 return (offset
- offsetStart
);
129 public static int serializeInt16Array(short[] value
,
133 int offsetStart
= offset
;
134 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
136 for (short s
: value
) {
137 numbytes
= serializeINT16(s
, bytes
, offset
);
140 return (offset
- offsetStart
);
143 public static int serializeInt32Array(int[] value
,
147 int offsetStart
= offset
;
148 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
150 for (int s
: value
) {
151 numbytes
= serializeINT32(s
, bytes
, offset
);
154 return (offset
- offsetStart
);
157 public static int serializeInt64Array(long[] value
,
161 int offsetStart
= offset
;
162 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
164 for (long s
: value
) {
165 numbytes
= serializeINT64(s
, bytes
, offset
);
168 return (offset
- offsetStart
);
171 public static int serializeUInt16Array(int[] value
,
175 int offsetStart
= offset
;
176 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
178 for (int s
: value
) {
179 numbytes
= serializeUINT16(s
, bytes
, offset
);
182 return (offset
- offsetStart
);
185 public static int serializeUInt32Array(long[] value
,
189 int offsetStart
= offset
;
190 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
192 for (long s
: value
) {
193 numbytes
= serializeUINT32(s
, bytes
, offset
);
196 return (offset
- offsetStart
);
199 public static int serializeUInt64Array(long[] value
,
203 int offsetStart
= offset
;
204 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
206 for (long s
: value
) {
207 numbytes
= serializeUINT64(s
, bytes
, offset
);
210 return (offset
- offsetStart
);
213 public static int serializeBooleanArray(boolean[] value
,
217 int offsetStart
= offset
;
218 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
220 for (boolean s
: value
) {
221 numbytes
= serializeBOOLEAN(s
, bytes
, offset
);
224 return (offset
- offsetStart
);
227 public static int serializeByteArray(byte[] value
,
231 int offsetStart
= offset
;
232 numbytes
= serializeUINT16(value
.length
, bytes
, offset
);
234 for (byte s
: value
) {
235 numbytes
= serializeBYTE(s
, bytes
, offset
);
238 return (offset
- offsetStart
);
244 public static int serializeEVENTWORD(String aString
, byte[] bytes
, int offset
) {
245 return serializeEVENTWORD(aString
, bytes
, offset
, Event
.DEFAULT_ENCODING
);
248 private static int serializeEVENTWORD(String aString
, byte[] bytes
,
249 int offset
, short encoding
) {
251 EncodedString
.getBytes(aString
, Event
.ENCODING_STRINGS
[encoding
]);
252 int length
= stringBytes
.length
;
253 if (length
< 255 && length
> 0) {
254 offset
+= serializeBYTE((byte) length
, bytes
, offset
);
255 System
.arraycopy(stringBytes
, 0, bytes
, offset
, length
);
262 public static int serializeATTRIBUTEWORD(String aString
, byte[] bytes
,
264 return serializeEVENTWORD(aString
, bytes
, offset
, Event
.DEFAULT_ENCODING
);
267 public static int serializeIPADDR(IPAddress anIPAddress
, byte[] bytes
,
269 byte[] inetaddr
= anIPAddress
.getInetAddressAsBytes();
270 bytes
[offset
+ 3] = inetaddr
[0];
271 bytes
[offset
+ 2] = inetaddr
[1];
272 bytes
[offset
+ 1] = inetaddr
[2];
273 bytes
[offset
] = inetaddr
[3];
277 public static int serializeIPADDR(InetAddress anIPAddress
, byte[] bytes
,
279 byte[] inetaddr
= anIPAddress
.getAddress();
280 bytes
[offset
+ 3] = inetaddr
[0];
281 bytes
[offset
+ 2] = inetaddr
[1];
282 bytes
[offset
+ 1] = inetaddr
[2];
283 bytes
[offset
] = inetaddr
[3];