forked from LupusNobilis/java-org.hwo
added float serialization and fixed some bugs
parent
e5210fa6a2
commit
e5832f7f2e
|
@ -53,6 +53,12 @@ public class BinaryParser {
|
|||
src.add((byte)c);
|
||||
}
|
||||
|
||||
static void writeBytes(List<Byte> src, float value) {
|
||||
byte[] buf = ByteBuffer.allocate(Float.BYTES).putFloat(value).array();
|
||||
for(byte b : buf)
|
||||
src.add(b);
|
||||
}
|
||||
|
||||
static byte readByte(List<Byte> src, int ptr) {
|
||||
if(ptr >= src.size() || ptr < 0)
|
||||
return 0;
|
||||
|
@ -165,5 +171,30 @@ public class BinaryParser {
|
|||
return new String(temp);
|
||||
}
|
||||
|
||||
static float readFloat(List<Byte> src, int ptr) {
|
||||
if(ptr >= src.size() || ptr < 0)
|
||||
return 0;
|
||||
|
||||
byte[] temp = new byte[Float.BYTES];
|
||||
for(int i = 0; i < Float.BYTES; i++)
|
||||
temp[i] = src.get(i + ptr);
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(temp);
|
||||
|
||||
return bb.getFloat();
|
||||
}
|
||||
|
||||
static float readFloat(byte[] src, int ptr) {
|
||||
if(ptr >= src.length || ptr < 0)
|
||||
return 0;
|
||||
|
||||
byte[] temp = new byte[Float.BYTES];
|
||||
for(int i = 0; i < Float.BYTES; i++)
|
||||
temp[i] = src[i + ptr];
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(temp);
|
||||
|
||||
return bb.getFloat();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ public class Database {
|
|||
ptr += obj.getSizeInBytes();
|
||||
result.addObject(obj);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
|
|
@ -17,9 +17,10 @@ public class DatabaseField {
|
|||
public static final byte SHORT = 0x01;
|
||||
public static final byte INT = 0x02;
|
||||
public static final byte LONG = 0x03;
|
||||
public static final byte FLOAT = 0x06;
|
||||
public static final byte STRING = 0x04;
|
||||
public static final byte OBJECT = 0x05;
|
||||
public static final byte ARRAY = 0x06;
|
||||
public static final byte ARRAY = 0x0F;
|
||||
|
||||
private static final String FIELD_ID = "DFLD";
|
||||
|
||||
|
@ -28,6 +29,7 @@ public class DatabaseField {
|
|||
SHORT,
|
||||
INT,
|
||||
LONG,
|
||||
FLOAT,
|
||||
STRING,
|
||||
OBJECT,
|
||||
ARRAY
|
||||
|
@ -93,6 +95,9 @@ public class DatabaseField {
|
|||
case LONG:
|
||||
setValue((long)value);
|
||||
break;
|
||||
case FLOAT:
|
||||
setValue((float)value);
|
||||
break;
|
||||
case STRING:
|
||||
setValue((String)value);
|
||||
break;
|
||||
|
@ -123,10 +128,12 @@ public class DatabaseField {
|
|||
return Type.STRING;
|
||||
case 0x05:
|
||||
return Type.OBJECT;
|
||||
case 0x06:
|
||||
return Type.FLOAT;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if(t >= 0x06)
|
||||
if(t >= 0x0F)
|
||||
return Type.ARRAY;
|
||||
return null;
|
||||
}
|
||||
|
@ -145,8 +152,10 @@ public class DatabaseField {
|
|||
return 0x04;
|
||||
case OBJECT:
|
||||
return 0x05;
|
||||
case ARRAY:
|
||||
case FLOAT:
|
||||
return 0x06;
|
||||
case ARRAY:
|
||||
return 0x0F;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
@ -164,27 +173,28 @@ public class DatabaseField {
|
|||
public void setValue(short value) { _setValue(value); }
|
||||
public void setValue(int value) { _setValue(value); }
|
||||
public void setValue(long value) { _setValue(value); }
|
||||
public void setValue(float value) { _setValue(value); }
|
||||
public void setValue(String value) { _setValue(value); }
|
||||
public void setValue(DatabaseObject value) { _setValue(value); }
|
||||
public void setValue(List<?> value) { _setValue(value); }
|
||||
|
||||
public void setValue(List<?> value) { this.arrayValue = value; }
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public<T> T getValue(){
|
||||
|
||||
if(this.type >= 0x06)
|
||||
if(this.type >= 0x0F)
|
||||
return (T)this.arrayValue;
|
||||
return (T)this.value.getValue();
|
||||
}
|
||||
|
||||
public int getSizeInBytes() {
|
||||
if(this.sizeInBytes <= 0)
|
||||
this.sizeInBytes = 11 + typeSize() + this.name.length();
|
||||
this.sizeInBytes = 11 + typeSize(_byteToType(this.type)) + this.name.length();
|
||||
|
||||
return this.sizeInBytes;
|
||||
}
|
||||
|
||||
private int typeSize() {
|
||||
switch(_byteToType(this.type)) {
|
||||
private int typeSize(Type t) {
|
||||
switch(t) {
|
||||
case BYTE:
|
||||
return Byte.BYTES;
|
||||
case SHORT:
|
||||
|
@ -193,6 +203,8 @@ public class DatabaseField {
|
|||
return Integer.BYTES;
|
||||
case LONG:
|
||||
return Long.BYTES;
|
||||
case FLOAT:
|
||||
return Float.BYTES;
|
||||
case STRING:
|
||||
return ((String)(this.value.getValue())).length();
|
||||
case OBJECT:
|
||||
|
@ -234,6 +246,8 @@ public class DatabaseField {
|
|||
case LONG:
|
||||
result += ((ArrayList<Long>)arrayValue).get(i) + " ";
|
||||
break;
|
||||
case FLOAT:
|
||||
result += ((ArrayList<Float>)arrayValue).get(i) + " ";
|
||||
case STRING:
|
||||
//result += (String)((List<Object>)getValue()).get(i) + " ";
|
||||
break;
|
||||
|
@ -292,6 +306,10 @@ public class DatabaseField {
|
|||
BinaryParser.writeBytes(bytes, (int)Long.BYTES);
|
||||
BinaryParser.writeBytes(bytes, (long)getValue());
|
||||
break;
|
||||
case FLOAT:
|
||||
BinaryParser.writeBytes(bytes, (int)Float.BYTES);
|
||||
BinaryParser.writeBytes(bytes, (float)getValue());
|
||||
break;
|
||||
case STRING:
|
||||
BinaryParser.writeBytes(bytes, (int)((String)getValue()).length());
|
||||
BinaryParser.writeBytes(bytes, (String)getValue());
|
||||
|
@ -306,7 +324,7 @@ public class DatabaseField {
|
|||
}
|
||||
|
||||
//serialize array
|
||||
if(this.type >= 0x06) {
|
||||
if(this.type >= 0x0F) {
|
||||
int arrLen = arrayValue.size();
|
||||
|
||||
switch(_byteToType((byte)(this.type >> 4))) {
|
||||
|
@ -330,6 +348,10 @@ public class DatabaseField {
|
|||
for(int i = 0; i < arrLen; i++)
|
||||
BinaryParser.writeBytes(bytes, ((List<Long>)arrayValue).get(i));
|
||||
break;
|
||||
case FLOAT:
|
||||
BinaryParser.writeBytes(bytes, (int)(Float.BYTES * arrLen));
|
||||
for(int i = 0; i < arrLen; i++)
|
||||
BinaryParser.writeBytes(bytes, ((List<Float>)arrayValue).get(i));
|
||||
case STRING:
|
||||
break;
|
||||
case OBJECT:
|
||||
|
@ -382,7 +404,7 @@ public class DatabaseField {
|
|||
tvalueLen = BinaryParser.readInt(src, ptr);
|
||||
ptr += 4;
|
||||
|
||||
//Length = 9 + NameLength + TypeLength
|
||||
//Length = 11 + NameLength + TypeLength
|
||||
switch(_byteToType(ttype)) {
|
||||
case BYTE:
|
||||
tvalue = BinaryParser.readByte(src, ptr);
|
||||
|
@ -400,6 +422,9 @@ public class DatabaseField {
|
|||
tvalue = BinaryParser.readLong(src, ptr);
|
||||
result = new DatabaseField(ttype, tname, (long)tvalue, false);
|
||||
break;
|
||||
case FLOAT:
|
||||
tvalue = BinaryParser.readFloat(src, ptr);
|
||||
result = new DatabaseField(ttype, tname, (float)tvalue, false);
|
||||
case STRING:
|
||||
tvalue = BinaryParser.readString(src, tvalueLen, ptr);
|
||||
result = new DatabaseField(ttype, tname, (String)tvalue, false);
|
||||
|
@ -421,7 +446,7 @@ public class DatabaseField {
|
|||
((ArrayList<Byte>)tvalue).add(BinaryParser.readByte(src, ptr));
|
||||
ptr += Byte.BYTES;
|
||||
}
|
||||
result = new DatabaseField(ttype, tname, (ArrayList<Byte>)tvalue, true);
|
||||
result = new DatabaseField((byte)(ttype >> 4), tname, (ArrayList<Byte>)tvalue, true);
|
||||
break;
|
||||
case SHORT:
|
||||
tvalue = new ArrayList<Short>();
|
||||
|
@ -429,7 +454,7 @@ public class DatabaseField {
|
|||
((ArrayList<Short>)tvalue).add(BinaryParser.readShort(src, ptr));
|
||||
ptr += Short.BYTES;
|
||||
}
|
||||
result = new DatabaseField(ttype, tname, (ArrayList<Short>)tvalue, true);
|
||||
result = new DatabaseField((byte)(ttype >> 4), tname, (ArrayList<Short>)tvalue, true);
|
||||
break;
|
||||
case INT:
|
||||
tvalue = new ArrayList<Integer>();
|
||||
|
@ -437,15 +462,23 @@ public class DatabaseField {
|
|||
((ArrayList<Integer>)tvalue).add(BinaryParser.readInt(src, ptr));
|
||||
ptr += Integer.BYTES;
|
||||
}
|
||||
result = new DatabaseField(ttype, tname, (ArrayList<Integer>)tvalue, true);
|
||||
result = new DatabaseField((byte)(ttype >> 4), tname, (ArrayList<Integer>)tvalue, true);
|
||||
break;
|
||||
case LONG:
|
||||
tvalue = new ArrayList<Long>();
|
||||
for(int i = 0; i < (tvalueLen / Long.BYTES); i++) {
|
||||
((ArrayList<Long>)tvalue).add(BinaryParser.readLong(src, ptr));
|
||||
ptr += Integer.BYTES;
|
||||
ptr += Long.BYTES;
|
||||
}
|
||||
result = new DatabaseField(ttype, tname, (ArrayList<Long>)tvalue, true);
|
||||
result = new DatabaseField((byte)(ttype >> 4), tname, (ArrayList<Long>)tvalue, true);
|
||||
break;
|
||||
case FLOAT:
|
||||
tvalue = new ArrayList<Float>();
|
||||
for(int i = 0; i < (tvalueLen / Float.BYTES); i++) {
|
||||
((ArrayList<Float>)tvalue).add(BinaryParser.readFloat(src, ptr));
|
||||
ptr += Float.BYTES;
|
||||
}
|
||||
result = new DatabaseField((byte)(ttype >> 4), tname, (ArrayList<Float>)tvalue, true);
|
||||
break;
|
||||
case STRING:
|
||||
break;
|
||||
|
|
|
@ -65,11 +65,17 @@ public class DatabaseObject {
|
|||
addField(temp);
|
||||
}
|
||||
|
||||
public void addField(String name, float value) {
|
||||
DatabaseField temp = new DatabaseField(DatabaseField.FLOAT, 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)
|
||||
|
@ -78,6 +84,8 @@ public class DatabaseObject {
|
|||
temp = new DatabaseField(DatabaseField.INT, name, value, true);
|
||||
else if(check instanceof Long)
|
||||
temp = new DatabaseField(DatabaseField.LONG, name, value, true);
|
||||
else if(check instanceof Float)
|
||||
temp = new DatabaseField(DatabaseField.FLOAT, name, value, true);
|
||||
else
|
||||
return;
|
||||
|
||||
|
|
Loading…
Reference in New Issue