1 package org
.lwes
.serializer
;
3 import java
.math
.BigInteger
;
4 import java
.net
.InetAddress
;
7 import org
.lwes
.util
.EncodedString
;
8 import org
.lwes
.util
.IPAddress
;
9 import org
.lwes
.util
.NumberCodec
;
12 * This contains low level type serialization used by the
14 * @author Anthony Molinaro
16 public class Serializer
19 public static int serializeBYTE(byte aByte
, byte[] bytes
, int offset
)
21 bytes
[offset
] = aByte
;
25 public static int serializeBOOLEAN(boolean aBoolean
, byte[] bytes
, int offset
)
29 bytes
[offset
] = (byte)0x01;
33 bytes
[offset
] = (byte)0x00;
38 public static int serializeUINT16(int anUnsignedShortInt
, byte[] bytes
,
41 bytes
[offset
] = (byte)( (anUnsignedShortInt
& (255 << 8) ) >> 8);
42 bytes
[offset
+1]= (byte)( (anUnsignedShortInt
& (255 << 0) ) >> 0);
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);
53 public static int serializeUINT32(long anUnsignedInt
, byte[] bytes
,
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);
63 public static int serializeINT32(int anInt
, byte[] bytes
,
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);
73 public static int serializeINT64(long anInt
, byte[] bytes
, int offset
)
75 NumberCodec
.encodeLongUnchecked(anInt
,bytes
,offset
);
79 public static int serializeUINT64(long anInt
, byte[] bytes
, int offset
)
81 NumberCodec
.encodeLongUnchecked(anInt
,bytes
,offset
);
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
);
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
,
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
);
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
)
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
);
140 public static int serializeATTRIBUTEWORD(String aString
, byte[] bytes
,
143 return serializeEVENTWORD(aString
, bytes
, offset
, Event
.DEFAULT_ENCODING
);
146 public static int serializeIPADDR(IPAddress anIPAddress
, byte[] bytes
,
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];
157 public static int serializeIPADDR(InetAddress anIPAddress
, byte[] bytes
,
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];