From a3d6fe7ec50084f05a6f9381f6f28bceeb107e15 Mon Sep 17 00:00:00 2001 From: Frank Maritato Date: Tue, 1 Dec 2009 21:10:05 +0000 Subject: [PATCH] boolean and byte array support added git-svn-id: https://lwes.svn.sourceforge.net/svnroot/lwes/lwes-java/branches/0.3.0@342 a2f82657-cdd2-4550-bd36-68a8e7111808 --- src/main/java/org/lwes/BaseType.java | 6 ++ src/main/java/org/lwes/Event.java | 84 ++++++++++++++++++---- src/main/java/org/lwes/db/EventTemplateDB.java | 12 ++++ .../java/org/lwes/serializer/Deserializer.java | 33 +++++++-- src/main/java/org/lwes/serializer/Serializer.java | 27 +++++++ src/test/java/org/lwes/db/ArrayTest.esf | 3 + src/test/java/org/lwes/db/ArrayTest.java | 40 +++++++++++ 7 files changed, 187 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/lwes/BaseType.java b/src/main/java/org/lwes/BaseType.java index b77e615..9f9ccea 100644 --- a/src/main/java/org/lwes/BaseType.java +++ b/src/main/java/org/lwes/BaseType.java @@ -166,6 +166,12 @@ public class BaseType { case TypeID.UINT64_ARRAY_TOKEN: size = ((long[]) typeObject).length*8+2; break; + case TypeID.BOOLEAN_ARRAY_TOKEN: + size = ((boolean[]) typeObject).length+2; + break; + case TypeID.BYTE_ARRAY_TOKEN: + size = ((byte[]) typeObject).length+2; + break; default: throw new NoSuchAttributeTypeException("Unknown size of BaseType " + typeName); } diff --git a/src/main/java/org/lwes/Event.java b/src/main/java/org/lwes/Event.java index b53959a..8b75798 100644 --- a/src/main/java/org/lwes/Event.java +++ b/src/main/java/org/lwes/Event.java @@ -292,6 +292,7 @@ public class Event { return null; } } + public int[] getInt32Array(String attributeName) throws NoSuchAttributeException { Object o = get(attributeName); @@ -302,6 +303,7 @@ public class Event { return null; } } + public long[] getInt64Array(String attributeName) throws NoSuchAttributeException { Object o = get(attributeName); @@ -312,6 +314,7 @@ public class Event { return null; } } + public int[] getUInt16Array(String attributeName) throws NoSuchAttributeException { Object o = get(attributeName); @@ -322,6 +325,7 @@ public class Event { return null; } } + public long[] getUInt32Array(String attributeName) throws NoSuchAttributeException { Object o = get(attributeName); @@ -332,6 +336,7 @@ public class Event { return null; } } + public long[] getUInt64Array(String attributeName) throws NoSuchAttributeException { Object o = get(attributeName); @@ -342,6 +347,7 @@ public class Event { return null; } } + public String[] getStringArray(String attributeName) throws NoSuchAttributeException { Object o = get(attributeName); @@ -353,6 +359,26 @@ public class Event { } } + public boolean[] getBooleanArray(String attributeName) throws NoSuchAttributeException { + Object o = get(attributeName); + if (o != null && o instanceof boolean[]) { + return (boolean[]) o; + } + else { + return null; + } + } + + public byte[] getByteArray(String attributeName) throws NoSuchAttributeException { + Object o = get(attributeName); + if (o != null && o instanceof byte[]) { + return (byte[]) o; + } + else { + return null; + } + } + /** * Method to check if an attribute is set in the event. This method does not throw * NoSuchAttributeException because it shouldn't really care. If it's not there, it's @@ -379,7 +405,7 @@ public class Event { */ public Boolean getBoolean(String attributeName) throws NoSuchAttributeException { return (Boolean) get(attributeName); - } + } /** * Accessor that returns an unsigned short, in the guise of an int, for attribute attributeName @@ -390,7 +416,7 @@ public class Event { */ public Integer getUInt16(String attributeName) throws NoSuchAttributeException { return (Integer) get(attributeName); - } + } /** * Accessor that returns an short, for attribute attributeName @@ -401,7 +427,7 @@ public class Event { */ public Short getInt16(String attributeName) throws NoSuchAttributeException { return (Short) get(attributeName); - } + } /** * Accessor that returns an unsigned int, in the guise of an long, for attribute attributeName @@ -412,7 +438,7 @@ public class Event { */ public Long getUInt32(String attributeName) throws NoSuchAttributeException { return (Long) get(attributeName); - } + } /** * Accessor that returns an int, for attribute attributeName @@ -423,7 +449,7 @@ public class Event { */ public Integer getInt32(String attributeName) throws NoSuchAttributeException { return (Integer) get(attributeName); - } + } /** * Accessor that returns an unsigned long, in the guise of an BigInteger, for attribute attributeName @@ -434,7 +460,7 @@ public class Event { */ public BigInteger getUInt64(String attributeName) throws NoSuchAttributeException { return (BigInteger) get(attributeName); - } + } /** @@ -446,7 +472,7 @@ public class Event { */ public Long getInt64(String attributeName) throws NoSuchAttributeException { return (Long) get(attributeName); - } + } /** * Accessor that returns an String, for attribute attributeName @@ -457,7 +483,7 @@ public class Event { */ public String getString(String attributeName) throws NoSuchAttributeException { return (String) get(attributeName); - } + } /** * Accessor that returns an InetAddress, for attribute attributeName @@ -485,7 +511,7 @@ public class Event { */ public byte[] getIPAddress(String attributeName) throws NoSuchAttributeException { return (byte[]) get(attributeName); - } + } /** @@ -554,31 +580,37 @@ public class Event { TypeID.INT16_ARRAY_TOKEN, value)); } + public void setInt32Array(String attributeName, int[] value) throws EventSystemException { set(attributeName, new BaseType(TypeID.INT32_ARRAY_STRING, TypeID.INT32_ARRAY_TOKEN, value)); } + public void setInt64Array(String attributeName, long[] value) throws EventSystemException { set(attributeName, new BaseType(TypeID.INT64_ARRAY_STRING, TypeID.INT64_ARRAY_TOKEN, value)); } + public void setUInt16Array(String attributeName, int[] value) throws EventSystemException { set(attributeName, new BaseType(TypeID.UINT16_ARRAY_STRING, TypeID.UINT16_ARRAY_TOKEN, value)); } + public void setUInt32Array(String attributeName, long[] value) throws EventSystemException { set(attributeName, new BaseType(TypeID.UINT32_ARRAY_STRING, TypeID.UINT32_ARRAY_TOKEN, value)); } + public void setUInt64Array(String attributeName, long[] value) throws EventSystemException { set(attributeName, new BaseType(TypeID.UINT64_ARRAY_STRING, TypeID.UINT64_ARRAY_TOKEN, value)); } + public void setStringArray(String attributeName, String[] value) throws EventSystemException { set(attributeName, new BaseType(TypeID.STRING_ARRAY_STRING, @@ -586,6 +618,20 @@ public class Event { value)); } + public void setBooleanArray(String attributeName, boolean[] value) + throws EventSystemException { + set(attributeName, new BaseType(TypeID.BOOLEAN_ARRAY_STRING, + TypeID.BOOLEAN_ARRAY_TOKEN, + value)); + } + + public void setByteArray(String attributeName, byte[] value) + throws EventSystemException { + set(attributeName, new BaseType(TypeID.BYTE_ARRAY_STRING, + TypeID.BYTE_ARRAY_TOKEN, + value)); + } + /** * Sets the given attribute with a boolean value given by aBool. * @@ -955,6 +1001,12 @@ public class Event { case TypeID.UINT64_ARRAY_TOKEN: offset += Serializer.serializeUInt64Array((long[]) data, bytes, offset); break; + case TypeID.BOOLEAN_ARRAY_TOKEN: + offset += Serializer.serializeBooleanArray((boolean[]) data, bytes, offset); + break; + case TypeID.BYTE_ARRAY_TOKEN: + offset += Serializer.serializeByteArray((byte[]) data, bytes, offset); + break; default: Log.warning("Unknown BaseType token: " + typeToken); break; @@ -1073,6 +1125,14 @@ public class Event { long[] ual = Deserializer.deserializeUInt64Array(state, bytes); setUInt64Array(attribute, ual); break; + case TypeID.BOOLEAN_ARRAY_TOKEN: + boolean[] ba = Deserializer.deserializeBooleanArray(state, bytes); + setBooleanArray(attribute, ba); + break; + case TypeID.BYTE_ARRAY_TOKEN: + byte[] bar = Deserializer.deserializeByteArray(state, bytes); + setByteArray(attribute, bar); + break; default: Log.warning("Unknown type " + type + " in deserialization"); } @@ -1197,11 +1257,11 @@ public class Event { * similar types the same way. */ if ((expected.getTypeToken() == TypeID.UINT16_TOKEN && - bt.getTypeToken() == TypeID.INT32_TOKEN) || + bt.getTypeToken() == TypeID.INT32_TOKEN) || (expected.getTypeToken() == TypeID.UINT32_TOKEN && - bt.getTypeToken() == TypeID.INT64_TOKEN) || + bt.getTypeToken() == TypeID.INT64_TOKEN) || (expected.getTypeToken() == TypeID.UINT64_TOKEN && - bt.getTypeToken() == TypeID.INT64_TOKEN) ) { + bt.getTypeToken() == TypeID.INT64_TOKEN)) { bt = expected; } if (!templ.checkTypeForAttribute(name, key, bt)) { diff --git a/src/main/java/org/lwes/db/EventTemplateDB.java b/src/main/java/org/lwes/db/EventTemplateDB.java index 8a28299..52cd87f 100644 --- a/src/main/java/org/lwes/db/EventTemplateDB.java +++ b/src/main/java/org/lwes/db/EventTemplateDB.java @@ -311,6 +311,12 @@ public class EventTemplateDB { else if (o instanceof long[]) { sizeToCheck = ((long[])o).length; } + else if (o instanceof boolean[]) { + sizeToCheck = ((boolean[])o).length; + } + else if (o instanceof byte[]) { + sizeToCheck = ((byte[])o).length; + } else { Object[] arr = (Object[]) attributeValue.getTypeObject(); sizeToCheck = arr.length; @@ -649,6 +655,12 @@ public class EventTemplateDB { knownTypes.put(TypeID.UINT64_ARRAY_STRING, new BaseType(TypeID.UINT64_ARRAY_STRING, TypeID.UINT64_ARRAY_TOKEN, null)); + knownTypes.put(TypeID.BOOLEAN_ARRAY_STRING, + new BaseType(TypeID.BOOLEAN_ARRAY_STRING, + TypeID.BOOLEAN_ARRAY_TOKEN, null)); + knownTypes.put(TypeID.BYTE_ARRAY_STRING, + new BaseType(TypeID.BYTE_ARRAY_STRING, + TypeID.BYTE_ARRAY_TOKEN, null)); } public Map getMetaFields() { diff --git a/src/main/java/org/lwes/serializer/Deserializer.java b/src/main/java/org/lwes/serializer/Deserializer.java index eb055b5..7b6cced 100644 --- a/src/main/java/org/lwes/serializer/Deserializer.java +++ b/src/main/java/org/lwes/serializer/Deserializer.java @@ -223,8 +223,9 @@ public class Deserializer { } return rtn; } - public static int[] deserializeUInt16Array(DeserializerState state, - byte[] bytes) { + + public static int[] deserializeUInt16Array(DeserializerState state, + byte[] bytes) { int length = deserializeUINT16(state, bytes); int[] rtn = new int[length]; for (int i = 0; i < length; i++) { @@ -234,7 +235,7 @@ public class Deserializer { } public static long[] deserializeUInt32Array(DeserializerState state, - byte[] bytes) { + byte[] bytes) { int length = deserializeUINT16(state, bytes); long[] rtn = new long[length]; for (int i = 0; i < length; i++) { @@ -244,7 +245,7 @@ public class Deserializer { } public static long[] deserializeUInt64Array(DeserializerState state, - byte[] bytes) { + byte[] bytes) { int length = deserializeUINT16(state, bytes); long[] rtn = new long[length]; for (int i = 0; i < length; i++) { @@ -252,7 +253,27 @@ public class Deserializer { } return rtn; } - + + public static boolean[] deserializeBooleanArray(DeserializerState state, + byte[] bytes) { + int length = deserializeUINT16(state, bytes); + boolean[] rtn = new boolean[length]; + for (int i = 0; i < length; i++) { + rtn[i] = deserializeBOOLEAN(state, bytes); + } + return rtn; + } + + public static byte[] deserializeByteArray(DeserializerState state, + byte[] bytes) { + int length = deserializeUINT16(state, bytes); + byte[] rtn = new byte[length]; + for (int i = 0; i < length; i++) { + rtn[i] = deserializeBYTE(state, bytes); + } + return rtn; + } + /** * Deserialize a String out of the byte array bytes * @@ -350,5 +371,5 @@ public class Deserializer { public static String deserializeATTRIBUTEWORD(DeserializerState myState, byte[] bytes) { return deserializeEVENTWORD(myState, bytes, Event.DEFAULT_ENCODING); - } + } } diff --git a/src/main/java/org/lwes/serializer/Serializer.java b/src/main/java/org/lwes/serializer/Serializer.java index 3f8ff32..3f0dd74 100644 --- a/src/main/java/org/lwes/serializer/Serializer.java +++ b/src/main/java/org/lwes/serializer/Serializer.java @@ -210,7 +210,34 @@ public class Serializer { return (offset - offsetStart); } + public static int serializeBooleanArray(boolean[] value, + byte[] bytes, + int offset) { + int numbytes = 0; + int offsetStart = offset; + numbytes = serializeUINT16(value.length, bytes, offset); + offset += numbytes; + for (boolean s : value) { + numbytes = serializeBOOLEAN(s, bytes, offset); + offset += numbytes; + } + return (offset - offsetStart); + } + public static int serializeByteArray(byte[] value, + byte[] bytes, + int offset) { + int numbytes = 0; + int offsetStart = offset; + numbytes = serializeUINT16(value.length, bytes, offset); + offset += numbytes; + for (byte s : value) { + numbytes = serializeBYTE(s, bytes, offset); + offset += numbytes; + } + return (offset - offsetStart); + } + /* * @deprecated */ diff --git a/src/test/java/org/lwes/db/ArrayTest.esf b/src/test/java/org/lwes/db/ArrayTest.esf index 926668c..ff9bc94 100644 --- a/src/test/java/org/lwes/db/ArrayTest.esf +++ b/src/test/java/org/lwes/db/ArrayTest.esf @@ -18,4 +18,7 @@ TestEvent uint16 field5[5]; uint32 field6[5]; uint64 field7[5]; + + boolean field8[5]; + byte field9[5]; } \ No newline at end of file diff --git a/src/test/java/org/lwes/db/ArrayTest.java b/src/test/java/org/lwes/db/ArrayTest.java index 10579f2..4d7696f 100644 --- a/src/test/java/org/lwes/db/ArrayTest.java +++ b/src/test/java/org/lwes/db/ArrayTest.java @@ -305,4 +305,44 @@ public class ArrayTest { } assertTrue("No exception was thrown for array length", exceptionThrown); } + + @Test + public void testBooleanArray() throws EventSystemException { + Event evt = new Event("TestEvent", true, template); + evt.setBooleanArray("field8", new boolean[]{true, false}); + boolean[] ar = evt.getBooleanArray("field8"); + assertNotNull("boolean array was null", ar); + assertEquals("length was incorrect", 2, ar.length); + + // Make sure we can serialize/deserialize it + byte[] serializedEvent = evt.serialize(); + Event evt2 = new Event(serializedEvent, true, template); + assertNotNull(evt2); + boolean[] a2 = evt2.getBooleanArray("field8"); + assertNotNull(a2); + assertEquals("deserialized array length was incorrect", 2, a2.length); + assertEquals("a2[0]", a2[0], true); + assertEquals("a2[1]", a2[1], false); + } + + @Test + public void testByteArray() throws EventSystemException { + Event evt = new Event("TestEvent", true, template); + evt.setByteArray("field9", new byte[]{(byte) 0x1, (byte) 0x2}); + byte[] ar = evt.getByteArray("field9"); + assertNotNull("boolean array was null", ar); + assertEquals("length was incorrect", 2, ar.length); + + // Make sure we can serialize/deserialize it + byte[] serializedEvent = evt.serialize(); + Event evt2 = new Event(serializedEvent, true, template); + assertNotNull(evt2); + byte[] a2 = evt2.getByteArray("field9"); + assertNotNull(a2); + assertEquals("deserialized array length was incorrect", 2, a2.length); + assertEquals("a2[0]", a2[0], (byte) 0x1); + assertEquals("a2[1]", a2[1], (byte) 0x2); + } + + } -- 2.11.4.GIT