changing license to BSD, assigning Yahoo copyrights where appropriate
[lwes-java.git] / src / main / java / org / lwes / serializer / Serializer.java
blob2cfd2ef4b3f310eeadf49a37f2ec5a249ce98e27
1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
3 * *
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;
25 /**
26 * This contains low level type serialization used by the
27 * rest of the system.
29 * @author Anthony Molinaro
31 public class Serializer {
33 public static int serializeIPV4(Inet4Address value, byte[] bytes, int offset) {
34 byte[] bs = value.getAddress();
35 int i = offset;
36 for (byte b : bs) {
37 bytes[i++] = b;
39 return offset - i;
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);
52 return 8;
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);
61 return 4;
64 public static int serializeBYTE(byte value, byte[] bytes, int offset) {
65 bytes[offset] = value;
66 return 1;
69 public static int serializeBOOLEAN(boolean value, byte[] bytes, int offset) {
70 bytes[offset] = (byte) (value ? 1 : 0);
71 return 1;
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);
81 return 2;
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);
89 return (4);
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);
97 return 4;
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);
109 return 8;
112 public static int serializeUINT64(long anInt, byte[] bytes, int offset) {
113 NumberCodec.encodeLongUnchecked(anInt, bytes, offset);
114 return (8);
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);
120 return (8);
124 * @deprecated
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,
131 short encoding) {
132 byte[] stringBytes =
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);
138 return (length + 2);
140 return 0;
145 * String arrays are serialized as follows:
146 * <array_name_len><array_name_bytes><type>
147 * <array_length><serialized_type_1>...<serialized_type_n>
149 * @param value
150 * @param bytes
151 * @param offset
152 * @param encoding
153 * @return
155 public static int serializeStringArray(List<String> value,
156 byte[] bytes,
157 int offset,
158 short encoding) {
160 int numbytes = 0;
161 int offsetStart = offset;
162 numbytes = serializeUINT16(value.size(), bytes, offset);
163 offset += numbytes;
164 for (String s : value) {
165 numbytes = serializeSTRING(s, bytes, offset, encoding);
166 offset += numbytes;
168 return (offset - offsetStart);
171 public static int serializeInt16Array(List<Short> value,
172 byte[] bytes,
173 int offset) {
174 int numbytes = 0;
175 int offsetStart = offset;
176 numbytes = serializeUINT16(value.size(), bytes, offset);
177 offset += numbytes;
178 for (short s : value) {
179 numbytes = serializeINT16(s, bytes, offset);
180 offset += numbytes;
182 return (offset - offsetStart);
185 public static int serializeInt32Array(List<Integer> value,
186 byte[] bytes,
187 int offset) {
188 int numbytes = 0;
189 int offsetStart = offset;
190 numbytes = serializeUINT16(value.size(), bytes, offset);
191 offset += numbytes;
192 for (int s : value) {
193 numbytes = serializeINT32(s, bytes, offset);
194 offset += numbytes;
196 return (offset - offsetStart);
199 public static int serializeInt64Array(List<Long> value,
200 byte[] bytes,
201 int offset) {
202 int numbytes = 0;
203 int offsetStart = offset;
204 numbytes = serializeUINT16(value.size(), bytes, offset);
205 offset += numbytes;
206 for (long s : value) {
207 numbytes = serializeINT64(s, bytes, offset);
208 offset += numbytes;
210 return (offset - offsetStart);
213 public static int serializeUInt16Array(List<Integer> value,
214 byte[] bytes,
215 int offset) {
216 int numbytes = 0;
217 int offsetStart = offset;
218 numbytes = serializeUINT16(value.size(), bytes, offset);
219 offset += numbytes;
220 for (int s : value) {
221 numbytes = serializeUINT16(s, bytes, offset);
222 offset += numbytes;
224 return (offset - offsetStart);
227 public static int serializeUInt32Array(List<Long> value,
228 byte[] bytes,
229 int offset) {
230 int numbytes = 0;
231 int offsetStart = offset;
232 numbytes = serializeUINT16(value.size(), bytes, offset);
233 offset += numbytes;
234 for (long s : value) {
235 numbytes = serializeUINT32(s, bytes, offset);
236 offset += numbytes;
238 return (offset - offsetStart);
241 public static int serializeUInt64Array(List<Long> value,
242 byte[] bytes,
243 int offset) {
244 int numbytes = 0;
245 int offsetStart = offset;
246 numbytes = serializeUINT16(value.size(), bytes, offset);
247 offset += numbytes;
248 for (long s : value) {
249 numbytes = serializeUINT64(s, bytes, offset);
250 offset += numbytes;
252 return (offset - offsetStart);
255 public static int serializeBooleanArray(List<Boolean> value,
256 byte[] bytes,
257 int offset) {
258 int numbytes = 0;
259 int offsetStart = offset;
260 numbytes = serializeUINT16(value.size(), bytes, offset);
261 offset += numbytes;
262 for (boolean s : value) {
263 numbytes = serializeBOOLEAN(s, bytes, offset);
264 offset += numbytes;
266 return (offset - offsetStart);
269 public static int serializeByteArray(List<Byte> value,
270 byte[] bytes,
271 int offset) {
272 int numbytes = 0;
273 int offsetStart = offset;
274 numbytes = serializeUINT16(value.size(), bytes, offset);
275 offset += numbytes;
276 for (byte s : value) {
277 numbytes = serializeBYTE(s, bytes, offset);
278 offset += numbytes;
280 return (offset - offsetStart);
283 public static int serializeDoubleArray(List<Double> 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 (double s : value) {
289 numbytes = serializeDOUBLE(s, bytes, offset);
290 offset += numbytes;
292 return (offset - offsetStart);
295 public static int serializeFloatArray(List<Float> 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 (float s : value) {
301 numbytes = serializeFLOAT(s, bytes, offset);
302 offset += numbytes;
304 return (offset - offsetStart);
307 public static int serializeIPV4Array(List<InetAddress> value, byte[] bytes, int offset) {
308 int numbytes = 0;
309 int offsetStart = offset;
310 numbytes = serializeUINT16(value.size(), bytes, offset);
311 offset += numbytes;
312 for (InetAddress s : value) {
313 numbytes = serializeIPV4(s, bytes, offset);
314 offset += numbytes;
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,
324 byte[] bytes,
325 int offset,
326 short encoding) {
327 byte[] stringBytes =
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);
333 return (length + 1);
335 return 0;
339 public static int serializeATTRIBUTEWORD(String aString, byte[] bytes,
340 int offset) {
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
351 * @deprecated
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];
359 return (4);
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
369 * @deprecated
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];
377 return (4);
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];
394 return (4);