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