diff --git a/src/org/hwo/serialize/DatabaseField.java b/src/org/hwo/serialize/DatabaseField.java index 0050dcc..e52abbc 100644 --- a/src/org/hwo/serialize/DatabaseField.java +++ b/src/org/hwo/serialize/DatabaseField.java @@ -54,39 +54,54 @@ public class DatabaseField { private String name; @SuppressWarnings("rawtypes") private _valueType value; + private List arrayValue; private int sizeInBytes; + +//+++++++++++CONSTRUCTORS+++++++++++++++// public DatabaseField(byte type, String name) { this.type = type; this.name = name; } - public DatabaseField(byte type, String name, Object value) { - this.type = type; - this.name = name; + public DatabaseField(byte type, String name, Object value, boolean array) { + if(array) { + this.type = ARRAY; + this.type |= type << 4; + this.name = name; + this.arrayValue = (ArrayList)value; + } + else { + this.type = type; + this.name = name; + initialize(value); + } + + } - switch(this.type) { - case 0x00: + private void initialize(Object value) { + switch(_byteToType(this.type)) { + case BYTE: setValue((byte)value); break; - case 0x01: + case SHORT: setValue((short)value); break; - case 0x02: + case INT: setValue((int)value); break; - case 0x03: + case LONG: setValue((long)value); break; - case 0x04: + case STRING: setValue((String)value); break; case OBJECT: setValue((DatabaseObject)value); break; case ARRAY: - - default: + //setValue((List)value); + break; } } @@ -108,10 +123,32 @@ public class DatabaseField { return Type.STRING; case 0x05: return Type.OBJECT; - case 0x06: - return Type.ARRAY; default: - return null; + break; + } + if(t >= 0x06) + return Type.ARRAY; + return null; + } + + private static byte _typeToByte(Type t) { + switch(t) { + case BYTE: + return 0x00; + case SHORT: + return 0x01; + case INT: + return 0x02; + case LONG: + return 0x03; + case STRING: + return 0x04; + case OBJECT: + return 0x05; + case ARRAY: + return 0x06; + default: + return -1; } } @@ -129,16 +166,45 @@ public class DatabaseField { public void setValue(long value) { _setValue(value); } public void setValue(String value) { _setValue(value); } public void setValue(DatabaseObject value) { _setValue(value); } + public void setValue(List value) { _setValue(value); } @SuppressWarnings("unchecked") public T getValue(){ + + if(this.type >= 0x06) + return (T)this.arrayValue; return (T)this.value.getValue(); } public int getSizeInBytes() { + if(this.sizeInBytes <= 0) + this.sizeInBytes = 11 + typeSize() + this.name.length(); + return this.sizeInBytes; } + private int typeSize() { + switch(_byteToType(this.type)) { + case BYTE: + return Byte.BYTES; + case SHORT: + return Short.BYTES; + case INT: + return Integer.BYTES; + case LONG: + return Long.BYTES; + case STRING: + return ((String)(this.value.getValue())).length(); + case OBJECT: + return ((DatabaseObject)(this.value.getValue())).getSizeInBytes(); + case ARRAY: + return 0; //TODO: + default: + return -1; + } + } + + @SuppressWarnings("unchecked") public String toString(int indendation) { String indend = new String(); String result = new String(); @@ -149,6 +215,38 @@ public class DatabaseField { result += indend + "Type: " + _byteToType(this.type) + "\n"; if(_byteToType(this.type) == Type.OBJECT) result += indend + "Value " + ((DatabaseObject)getValue()).toString(indendation + 5); + else if(_byteToType(this.type) == Type.ARRAY) { + result += indend + "Value: \n"; + result += indend; + for(int i = 0; i < arrayValue.size(); i++) { + result += "[" + i + "]" + " "; + //TODO: print values! + switch(_byteToType((byte)(this.type >> 4))) { + case BYTE: + result += ((ArrayList)arrayValue).get(i) + " "; + break; + case SHORT: + result += ((ArrayList)arrayValue).get(i) + " "; + break; + case INT: + result += ((ArrayList)arrayValue).get(i) + " "; + break; + case LONG: + result += ((ArrayList)arrayValue).get(i) + " "; + break; + case STRING: + //result += (String)((List)getValue()).get(i) + " "; + break; + case OBJECT: + //result += indend + ((DatabaseObject)((List)getValue()).get(i)).toString(indendation + 5); + break; + case ARRAY: + break; + } + if((i % 5) == 4) + result += "\n" + indend; + } + } else result += indend + "Value: " + getValue() + "\n"; result += "\n"; @@ -168,6 +266,7 @@ public class DatabaseField { print(0); } + @SuppressWarnings("unchecked") public byte[] serialize() { List bytes = new ArrayList(); @@ -203,10 +302,45 @@ public class DatabaseField { for(byte b : temp) bytes.add(b); case ARRAY: - //BinaryParser.writeBytes(bytes, getValue()); - default: + break; } + + //serialize array + if(this.type >= 0x06) { + int arrLen = arrayValue.size(); + switch(_byteToType((byte)(this.type >> 4))) { + case BYTE: + BinaryParser.writeBytes(bytes, (int)(Byte.BYTES * arrLen)); + for(int i = 0; i < arrLen; i++) + BinaryParser.writeBytes(bytes, ((List)arrayValue).get(i)); + break; + case SHORT: + BinaryParser.writeBytes(bytes, (int)(Short.BYTES * arrLen)); + for(int i = 0; i < arrLen; i++) + BinaryParser.writeBytes(bytes, ((List)arrayValue).get(i)); + break; + case INT: + BinaryParser.writeBytes(bytes, (int)(Integer.BYTES * arrLen)); + for(int i = 0; i < arrLen; i++) + BinaryParser.writeBytes(bytes,((List)arrayValue).get(i)); + break; + case LONG: + BinaryParser.writeBytes(bytes, (int)(Long.BYTES * arrLen)); + for(int i = 0; i < arrLen; i++) + BinaryParser.writeBytes(bytes, ((List)arrayValue).get(i)); + break; + case STRING: + break; + case OBJECT: + break; + case ARRAY: + break; + default: + break; + } + } + byte[] result = new byte[bytes.size()]; for(int i = 0; i < bytes.size(); i++) result[i] = bytes.get(i); @@ -217,6 +351,7 @@ public class DatabaseField { return result; } + @SuppressWarnings("unchecked") public static DatabaseField deserialize(byte[] src, int ptr) { String tid; byte ttype; @@ -231,7 +366,7 @@ public class DatabaseField { ptr += 4; if(!tid.equals(FIELD_ID)) { //throw new Exception(); - System.out.println("Field to deserialize is not of type Field!"); + System.out.println("Field to deserialize is not of type Field! \nID read: " + tid); return null; } @@ -251,33 +386,77 @@ public class DatabaseField { switch(_byteToType(ttype)) { case BYTE: tvalue = BinaryParser.readByte(src, ptr); - result = new DatabaseField(ttype, tname, (byte)tvalue); + result = new DatabaseField(ttype, tname, (byte)tvalue, false); break; case SHORT: tvalue = BinaryParser.readShort(src, ptr); - result = new DatabaseField(ttype, tname, (short)tvalue); + result = new DatabaseField(ttype, tname, (short)tvalue, false); break; case INT: tvalue = BinaryParser.readInt(src, ptr); - result = new DatabaseField(ttype, tname, (int)tvalue); + result = new DatabaseField(ttype, tname, (int)tvalue, false); break; case LONG: tvalue = BinaryParser.readLong(src, ptr); - result = new DatabaseField(ttype, tname, (long)tvalue); + result = new DatabaseField(ttype, tname, (long)tvalue, false); break; case STRING: tvalue = BinaryParser.readString(src, tvalueLen, ptr); - result = new DatabaseField(ttype, tname, (String)tvalue); + result = new DatabaseField(ttype, tname, (String)tvalue, false); break; case OBJECT: tvalue = DatabaseObject.deserialize(src, ptr); - result = new DatabaseField(ttype, tname, (DatabaseObject)tvalue); + result = new DatabaseField(ttype, tname, (DatabaseObject)tvalue, false); break; case ARRAY: - return null; + break; } - result.sizeInBytes = 11 + tnameLen + tvalueLen; + //Array Type + if(ttype >= 0x06) { + switch(_byteToType((byte)(ttype >> 4))) { + case BYTE: + tvalue = new ArrayList(); + for(int i = 0; i < (tvalueLen / Byte.BYTES); i++) { + ((ArrayList)tvalue).add(BinaryParser.readByte(src, ptr)); + ptr += Byte.BYTES; + } + result = new DatabaseField(ttype, tname, (ArrayList)tvalue, true); + break; + case SHORT: + tvalue = new ArrayList(); + for(int i = 0; i < (tvalueLen / Short.BYTES); i++) { + ((ArrayList)tvalue).add(BinaryParser.readShort(src, ptr)); + ptr += Short.BYTES; + } + result = new DatabaseField(ttype, tname, (ArrayList)tvalue, true); + break; + case INT: + tvalue = new ArrayList(); + for(int i = 0; i < (tvalueLen / Integer.BYTES); i++) { + ((ArrayList)tvalue).add(BinaryParser.readInt(src, ptr)); + ptr += Integer.BYTES; + } + result = new DatabaseField(ttype, tname, (ArrayList)tvalue, true); + break; + case LONG: + tvalue = new ArrayList(); + for(int i = 0; i < (tvalueLen / Long.BYTES); i++) { + ((ArrayList)tvalue).add(BinaryParser.readLong(src, ptr)); + ptr += Integer.BYTES; + } + result = new DatabaseField(ttype, tname, (ArrayList)tvalue, true); + break; + case STRING: + break; + case OBJECT: + break; + case ARRAY: + break; + } + } + + //result.sizeInBytes = 11 + tnameLen + tvalueLen; return result; } diff --git a/src/org/hwo/serialize/DatabaseObject.java b/src/org/hwo/serialize/DatabaseObject.java index da090c6..d33e2a7 100644 --- a/src/org/hwo/serialize/DatabaseObject.java +++ b/src/org/hwo/serialize/DatabaseObject.java @@ -1,5 +1,6 @@ package org.hwo.serialize; +import java.lang.reflect.*; import java.util.ArrayList; import java.util.List; @@ -29,41 +30,61 @@ public class DatabaseObject { } public void addField(String name, byte value) { - DatabaseField temp = new DatabaseField(DatabaseField.BYTE, name, value); + DatabaseField temp = new DatabaseField(DatabaseField.BYTE, name, value, false); if(temp != null) addField(temp); } public void addField(String name, short value) { - DatabaseField temp = new DatabaseField(DatabaseField.SHORT, name, value); + DatabaseField temp = new DatabaseField(DatabaseField.SHORT, name, value, false); if(temp != null) addField(temp); } public void addField(String name, int value) { - DatabaseField temp = new DatabaseField(DatabaseField.INT, name, value); + DatabaseField temp = new DatabaseField(DatabaseField.INT, name, value, false); if(temp != null) addField(temp); } public void addField(String name, long value) { - DatabaseField temp = new DatabaseField(DatabaseField.LONG, name, value); + DatabaseField temp = new DatabaseField(DatabaseField.LONG, name, value, false); if(temp != null) addField(temp); } public void addField(String name, String value) { - DatabaseField temp = new DatabaseField(DatabaseField.STRING, name, value); + DatabaseField temp = new DatabaseField(DatabaseField.STRING, name, value, false); if(temp != null) addField(temp); } public void addField(String name, DatabaseObject value) { - DatabaseField temp = new DatabaseField(DatabaseField.OBJECT, name, value); + DatabaseField temp = new DatabaseField(DatabaseField.OBJECT, name, value, false); if(temp != null) addField(temp); } + public void addField(String name, List value){ + + DatabaseField temp; + Object check = value.get(0); + + if(check instanceof Byte) + temp = new DatabaseField(DatabaseField.BYTE, name, value, true); + else if(check instanceof Short) + temp = new DatabaseField(DatabaseField.SHORT, name, value, true); + else if(check instanceof Integer) + temp = new DatabaseField(DatabaseField.INT, name, value, true); + else if(check instanceof Long) + temp = new DatabaseField(DatabaseField.LONG, name, value, true); + else + return; + + if(temp != null) + addField(temp); + } + public void removeField(DatabaseField field) { if(fields.contains(field)) return; @@ -90,9 +111,16 @@ public class DatabaseObject { } public int getSizeInBytes() { + if(this.sizeInBytes <= 0) { + this.sizeInBytes = 8 + this.name.length(); + for(int i = 0; i < this.fields.size(); i++) + this.sizeInBytes += this.fields.get(i).getSizeInBytes(); + } + return this.sizeInBytes; } + public String toString(int indendation) { String indend = new String(); String result = new String(); @@ -107,7 +135,6 @@ public class DatabaseObject { result += f.toString(indendation + 5); } - return result; } @@ -165,10 +192,7 @@ public class DatabaseObject { DatabaseObject result = new DatabaseObject(tName); DatabaseField field = null; int fieldSize = 0; - - if(result == null) - return null; - + for(short i = 0; i < tFieldCount; i++) { field = DatabaseField.deserialize(src, ptr); if(field != null) { diff --git a/src/org/hwo/serialize/DatabaseStructure b/src/org/hwo/serialize/DatabaseStructure index fa16d60..a349607 100644 --- a/src/org/hwo/serialize/DatabaseStructure +++ b/src/org/hwo/serialize/DatabaseStructure @@ -1,7 +1,7 @@ HEADER [4] String DatabaseID -[4] Short ObjectCount +[2] Short ObjectCount [4] String "DATA" @@ -19,5 +19,5 @@ HEADER [NameLength] String Name [4] Int ValueLength [TypeLength] Value - Length = 9 + NameLength + TypeLength + Length = 11 + NameLength + TypeLength \ No newline at end of file