restructuring
[lwes-java.git] / src / main / java / org / lwes / serializer / Serializer.java
blobdaaf3152eb02f2c4e3b68b2f403fa60c2e67dc39
1 package org.lwes.serializer;
3 import java.math.BigInteger;
4 import java.net.InetAddress;
6 import org.lwes.Event;
7 import org.lwes.util.EncodedString;
8 import org.lwes.util.IPAddress;
9 import org.lwes.util.NumberCodec;
11 /**
12 * This contains low level type serialization used by the
13 * rest of the system.
14 * @author Anthony Molinaro
16 public class Serializer
19 public static int serializeBYTE(byte aByte, byte[] bytes, int offset)
21 bytes[offset] = aByte;
22 return (1);
25 public static int serializeBOOLEAN(boolean aBoolean, byte[] bytes, int offset)
27 if ( aBoolean )
29 bytes[offset] = (byte)0x01;
31 else
33 bytes[offset] = (byte)0x00;
35 return (1);
38 public static int serializeUINT16(int anUnsignedShortInt, byte[] bytes,
39 int offset)
41 bytes[offset] = (byte)( (anUnsignedShortInt & (255 << 8) ) >> 8);
42 bytes[offset+1]= (byte)( (anUnsignedShortInt & (255 << 0) ) >> 0);
43 return (2);
46 public static int serializeINT16(short aShortInt, byte[] bytes, int offset)
48 bytes[offset] = (byte)( (aShortInt & (255 << 8) ) >> 8);
49 bytes[offset+1]= (byte)( (aShortInt & (255 << 0) ) >> 0);
50 return (2);
53 public static int serializeUINT32(long anUnsignedInt, byte[] bytes,
54 int offset)
56 bytes[offset] = (byte) ( (anUnsignedInt & 0xff000000 ) >> 24);
57 bytes[offset+1] = (byte) ( (anUnsignedInt & 0x00ff0000 ) >> 16);
58 bytes[offset+2] = (byte) ( (anUnsignedInt & 0x0000ff00 ) >> 8);
59 bytes[offset+3] = (byte) ( (anUnsignedInt & 0x000000ff ) >> 0);
60 return (4);
63 public static int serializeINT32(int anInt, byte[] bytes,
64 int offset)
66 bytes[offset] = (byte) ( (anInt & (255 << 24)) >> 24);
67 bytes[offset+1] = (byte) ( (anInt & (255 << 16)) >> 16);
68 bytes[offset+2] = (byte) ( (anInt & (255 << 8)) >> 8);
69 bytes[offset+3] = (byte) ( (anInt & (255 << 0)) >> 0);
70 return (4);
73 public static int serializeINT64(long anInt, byte[] bytes, int offset)
75 NumberCodec.encodeLongUnchecked(anInt,bytes,offset);
76 return (8);
79 public static int serializeUINT64(long anInt, byte[] bytes, int offset)
81 NumberCodec.encodeLongUnchecked(anInt,bytes,offset);
82 return (8);
85 public static int serializeUINT64(BigInteger anInt, byte[] bytes, int offset)
87 // TODO: write a BigInteger serialization method
88 NumberCodec.encodeLongUnchecked(anInt.longValue(),bytes,offset);
89 return (8);
92 /**
93 * @deprecated
95 public static int serializeSTRING(String aString, byte[] bytes, int offset)
97 return serializeSTRING(aString, bytes, offset, Event.DEFAULT_ENCODING);
100 public static int serializeSTRING(String aString, byte[] bytes, int offset,
101 short encoding)
103 byte[] stringBytes =
104 EncodedString.getBytes(aString, Event.ENCODING_STRINGS[encoding]);
105 int length = stringBytes.length;
106 if ( length < 65535 && length >= 0 )
108 offset += serializeUINT16(length,bytes,offset);
109 System.arraycopy(stringBytes,0,bytes,offset,length);
110 return (length+2);
112 return 0;
117 * @deprecated
119 public static int serializeEVENTWORD(String aString, byte[] bytes, int offset)
121 return serializeEVENTWORD(aString, bytes, offset, Event.DEFAULT_ENCODING);
124 private static int serializeEVENTWORD(String aString, byte[] bytes,
125 int offset, short encoding)
127 byte[] stringBytes =
128 EncodedString.getBytes(aString, Event.ENCODING_STRINGS[encoding]);
129 int length = stringBytes.length;
130 if ( length < 255 && length > 0 )
132 offset += serializeBYTE((byte)length,bytes,offset);
133 System.arraycopy(stringBytes,0,bytes,offset,length);
134 return (length+1);
136 return 0;
140 public static int serializeATTRIBUTEWORD(String aString, byte[] bytes,
141 int offset)
143 return serializeEVENTWORD(aString, bytes, offset, Event.DEFAULT_ENCODING);
146 public static int serializeIPADDR(IPAddress anIPAddress, byte[] bytes,
147 int offset)
149 byte[] inetaddr = anIPAddress.getInetAddressAsBytes();
150 bytes[offset+3] = inetaddr[0];
151 bytes[offset+2] = inetaddr[1];
152 bytes[offset+1] = inetaddr[2];
153 bytes[offset ] = inetaddr[3];
154 return (4);
157 public static int serializeIPADDR(InetAddress anIPAddress, byte[] bytes,
158 int offset)
160 byte[] inetaddr = anIPAddress.getAddress();
161 bytes[offset+3] = inetaddr[0];
162 bytes[offset+2] = inetaddr[1];
163 bytes[offset+1] = inetaddr[2];
164 bytes[offset ] = inetaddr[3];
165 return (4);