boolean and byte array support added
[lwes-java.git] / src / main / java / org / lwes / serializer / Serializer.java
blob3f0dd74c9c780e9cf9a8aba0a304d01dd1a4f2f0
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.InetAddress;
11 /**
12 * This contains low level type serialization used by the
13 * rest of the system.
15 * @author Anthony Molinaro
17 public class Serializer {
19 public static int serializeBYTE(byte aByte, byte[] bytes, int offset) {
20 bytes[offset] = aByte;
21 return (1);
24 public static int serializeBOOLEAN(boolean aBoolean, byte[] bytes, int offset) {
25 if (aBoolean) {
26 bytes[offset] = (byte) 0x01;
28 else {
29 bytes[offset] = (byte) 0x00;
31 return (1);
34 public static int serializeUINT16(int anUnsignedShortInt, byte[] bytes,
35 int offset) {
36 bytes[offset] = (byte) ((anUnsignedShortInt & (255 << 8)) >> 8);
37 bytes[offset + 1] = (byte) ((anUnsignedShortInt & (255 << 0)) >> 0);
38 return (2);
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);
44 return (2);
47 public static int serializeUINT32(long anUnsignedInt, byte[] bytes,
48 int offset) {
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);
53 return (4);
56 public static int serializeINT32(int anInt, byte[] bytes,
57 int offset) {
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);
62 return (4);
65 public static int serializeINT64(long anInt, byte[] bytes, int offset) {
66 NumberCodec.encodeLongUnchecked(anInt, bytes, offset);
67 return (8);
70 public static int serializeUINT64(long anInt, byte[] bytes, int offset) {
71 NumberCodec.encodeLongUnchecked(anInt, bytes, offset);
72 return (8);
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);
78 return (8);
81 /**
82 * @deprecated
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,
89 short encoding) {
90 byte[] stringBytes =
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);
96 return (length + 2);
98 return 0;
103 * String arrays are serialized as follows:
104 * <array_name_len><array_name_bytes><type>
105 * <array_length><serialized_type_1>...<serialized_type_n>
107 * @param value
108 * @param bytes
109 * @param offset
110 * @param encoding
111 * @return
113 public static int serializeStringArray(String[] value,
114 byte[] bytes,
115 int offset,
116 short encoding) {
118 int numbytes = 0;
119 int offsetStart = offset;
120 numbytes = serializeUINT16(value.length, bytes, offset);
121 offset += numbytes;
122 for (String s : value) {
123 numbytes = serializeSTRING(s, bytes, offset, encoding);
124 offset += numbytes;
126 return (offset - offsetStart);
129 public static int serializeInt16Array(short[] value,
130 byte[] bytes,
131 int offset) {
132 int numbytes = 0;
133 int offsetStart = offset;
134 numbytes = serializeUINT16(value.length, bytes, offset);
135 offset += numbytes;
136 for (short s : value) {
137 numbytes = serializeINT16(s, bytes, offset);
138 offset += numbytes;
140 return (offset - offsetStart);
143 public static int serializeInt32Array(int[] value,
144 byte[] bytes,
145 int offset) {
146 int numbytes = 0;
147 int offsetStart = offset;
148 numbytes = serializeUINT16(value.length, bytes, offset);
149 offset += numbytes;
150 for (int s : value) {
151 numbytes = serializeINT32(s, bytes, offset);
152 offset += numbytes;
154 return (offset - offsetStart);
157 public static int serializeInt64Array(long[] value,
158 byte[] bytes,
159 int offset) {
160 int numbytes = 0;
161 int offsetStart = offset;
162 numbytes = serializeUINT16(value.length, bytes, offset);
163 offset += numbytes;
164 for (long s : value) {
165 numbytes = serializeINT64(s, bytes, offset);
166 offset += numbytes;
168 return (offset - offsetStart);
171 public static int serializeUInt16Array(int[] value,
172 byte[] bytes,
173 int offset) {
174 int numbytes = 0;
175 int offsetStart = offset;
176 numbytes = serializeUINT16(value.length, bytes, offset);
177 offset += numbytes;
178 for (int s : value) {
179 numbytes = serializeUINT16(s, bytes, offset);
180 offset += numbytes;
182 return (offset - offsetStart);
185 public static int serializeUInt32Array(long[] value,
186 byte[] bytes,
187 int offset) {
188 int numbytes = 0;
189 int offsetStart = offset;
190 numbytes = serializeUINT16(value.length, bytes, offset);
191 offset += numbytes;
192 for (long s : value) {
193 numbytes = serializeUINT32(s, bytes, offset);
194 offset += numbytes;
196 return (offset - offsetStart);
199 public static int serializeUInt64Array(long[] value,
200 byte[] bytes,
201 int offset) {
202 int numbytes = 0;
203 int offsetStart = offset;
204 numbytes = serializeUINT16(value.length, bytes, offset);
205 offset += numbytes;
206 for (long s : value) {
207 numbytes = serializeUINT64(s, bytes, offset);
208 offset += numbytes;
210 return (offset - offsetStart);
213 public static int serializeBooleanArray(boolean[] value,
214 byte[] bytes,
215 int offset) {
216 int numbytes = 0;
217 int offsetStart = offset;
218 numbytes = serializeUINT16(value.length, bytes, offset);
219 offset += numbytes;
220 for (boolean s : value) {
221 numbytes = serializeBOOLEAN(s, bytes, offset);
222 offset += numbytes;
224 return (offset - offsetStart);
227 public static int serializeByteArray(byte[] value,
228 byte[] bytes,
229 int offset) {
230 int numbytes = 0;
231 int offsetStart = offset;
232 numbytes = serializeUINT16(value.length, bytes, offset);
233 offset += numbytes;
234 for (byte s : value) {
235 numbytes = serializeBYTE(s, bytes, offset);
236 offset += numbytes;
238 return (offset - offsetStart);
242 * @deprecated
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) {
250 byte[] stringBytes =
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);
256 return (length + 1);
258 return 0;
262 public static int serializeATTRIBUTEWORD(String aString, byte[] bytes,
263 int offset) {
264 return serializeEVENTWORD(aString, bytes, offset, Event.DEFAULT_ENCODING);
267 public static int serializeIPADDR(IPAddress anIPAddress, byte[] bytes,
268 int offset) {
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];
274 return (4);
277 public static int serializeIPADDR(InetAddress anIPAddress, byte[] bytes,
278 int offset) {
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];
284 return (4);