Compare commits

...

5 Commits

Author SHA1 Message Date
Niclas Thobaben 4425cc3670 added new CSV-interface 2018-02-12 10:17:54 +01:00
Niclas Thobaben 518fa54ea3 implemented new basic csv api 2018-02-09 10:57:24 +01:00
niclasthobaben e5832f7f2e added float serialization and fixed some bugs 2018-01-26 10:51:43 +01:00
niclasthobaben e5210fa6a2 fixed size calculation in serializer 2018-01-24 16:29:59 +01:00
Niclas Thobaben 7cb45827aa inserted org.hwo.serialize package for binary serialization 2018-01-23 08:45:20 +01:00
10 changed files with 1283 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import java.util.List;
import org.hwo.StringHelper;
import org.hwo.text.LineReader;
@Deprecated
public class CSV {
List<CSVRecord> records;

View File

@ -0,0 +1,44 @@
package org.hwo.csv;
import java.util.ArrayList;
public class CSVContainer {
private ArrayList<CSVTable> tables;
private CSVTable emptyTable;
public CSVContainer(){
init();
}
private void init() {
tables = new ArrayList<>();
}
public int getTableCount() {
return this.tables.size();
}
public CSVTable getTabel(int index) {
return this.tables.get(index);
}
public void addTable(CSVTable table) {
this.tables.add(table);
}
public void removeTable(CSVTable table) {
this.tables.remove(table);
}
public void removeTable(int index) {
this.tables.remove(index);
}
public String getCSV() {
String csv = new String();
for(CSVTable t : this.tables) {
csv += t.getCSV();
csv += CSVTableRow.NULL_ROW.getCSV();
}
return csv;
}
}

View File

@ -3,6 +3,7 @@ package org.hwo.csv;
import java.util.ArrayList;
import java.util.Arrays;
@Deprecated
public class CSVRecord {
ArrayList<Object> columns;

View File

@ -0,0 +1,87 @@
package org.hwo.csv;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CSVTable {
ArrayList<CSVTableRow> rows;
public CSVTable() {
this.rows = new ArrayList<>();
}
public void addRow(CSVTableRow row) {
this.rows.add(row);
}
public CSVTableRow getRow(int index) {
return this.rows.get(index);
}
public int getRowCount() {
return this.rows.size();
}
public Object[] getColumn(int column) {
ArrayList<Object> ret = new ArrayList<>();
for(CSVTableRow r : this.rows) {
ret.add(r.getValue(column));
}
return ret.toArray();
}
public int getColumnCount() {
int count = 0;
for(CSVTableRow r : this.rows) {
int rc = r.getColumnCount();
count = rc > count? rc : count;
}
return count;
}
public String getCSV() {
String csv = new String();
for(CSVTableRow r : this.rows) {
csv += r.getCSV();
}
return csv;
}
public static CSVTable ParseCSV(String path) {
CSVTable ret = new CSVTable();
File f = new File(path);
List<String> str = null;
try {
str = Files.readAllLines(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
if(str == null) {
return null;
}
for(String s : str) {
//parse line
CSVTableRow row = new CSVTableRow();
String[] tokens = s.split(",");
for(String t : tokens) {
if(t.contains("\"")) {
row.addValue(t.substring(1,t.length() - 1));
}
else if(t.contains(".")) {
row.addValue(Float.parseFloat(t));
}
else {
row.addValue(Integer.parseInt(t));
}
}
ret.addRow(row);
}
return ret;
}
}

View File

@ -0,0 +1,94 @@
package org.hwo.csv;
import java.util.ArrayList;
public class CSVTableRow {
public static CSVTableRow NULL_ROW = new CSVTableRow("");
private ArrayList<Object> values; //first value == rowTitle
public CSVTableRow() {
init();
}
public CSVTableRow(String title) {
init();
addValue(title);
}
private void init() {
this.values = new ArrayList<>();
}
//TODO add functionality for column detection (i.e. string column id, Object value)
public void setValue(int column, Object value) {
if(column < values.size()) {
this.values.set(column, value);
}
}
public Object getValue(int column) {
if(column >= this.values.size()) {
return "";
}
return this.values.get(column);
}
public Object[] getValues() {
return this.values.toArray();
}
public int getColumnCount() {
return this.values.size();
}
public void addValue(Object value) {
this.values.add(value);
}
public void addData(Object[] data) {
for(Object o : data) {
this.values.add(o);
}
}
public String getName() {
Object n = this.values.get(0);
if(n != null && n.getClass() == String.class) {
return (String)n;
}
return null;
}
public void clear() {
this.values.clear();
}
public String getCSV() {
String ret = new String();
for(int i = 0; i < values.size(); i++) {
Object obj = values.get(i);
if(obj != null) {
if(obj.getClass() == String.class) {
ret += formatString(obj.toString());
}
else {
ret += obj.toString();
}
}
else {
ret += formatString("null");
}
if(i < this.values.size() -1)
ret += ",";
}
ret += "\n";
return ret;
}
private String formatString(String str) {
return "\"" + str + "\"";
}
}

View File

@ -0,0 +1,200 @@
package org.hwo.serialize;
import java.nio.ByteBuffer;
import java.util.List;
public class BinaryParser {
private static final String _hex = "0123456789ABCDEF";
static void printBytes(List<Byte> src) {
for(Byte b : src) {
System.out.print(Integer.toHexString(b) + " ");
}
System.out.print("\n");
}
static void printBytes(byte[] src) {
for(byte b : src) {
System.out.print(Integer.toHexString(b) + " ");
}
System.out.print("\n");
}
static void writeBytes(List<Byte> src, byte value) {
src.add(value);
}
static void writeBytes(List<Byte> src, short value) {
byte[] buf = ByteBuffer.allocate(Short.BYTES).putShort(value).array();
for(byte b : buf)
src.add(b);
}
static void writeBytes(List<Byte> src, int value) {
byte[] buf = ByteBuffer.allocate(Integer.BYTES).putInt(value).array();
for(byte b : buf)
src.add(b);
}
static void writeBytes(List<Byte> src, long value) {
byte[] buf = ByteBuffer.allocate(Long.BYTES).putLong(value).array();
for(byte b : buf)
src.add(b);
}
static void writeBytes(List<Byte> src, String value) {
char[] arr = value.toCharArray();
for(char c : arr)
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;
return src.get(ptr);
}
static byte readByte(byte[] src, int ptr) {
if(ptr >= src.length || ptr < 0)
return 0;
return src[ptr];
}
static short readShort(List<Byte> src, int ptr) {
if(ptr >= src.size() || ptr < 0)
return 0;
byte[] temp = new byte[Short.BYTES];
for(int i = 0; i < Short.BYTES; i++)
temp[i] = src.get(i + ptr);
ByteBuffer bb = ByteBuffer.wrap(temp);
return bb.getShort();
}
static short readShort(byte[] src, int ptr) {
if(ptr >= src.length || ptr < 0)
return 0;
byte[] temp = new byte[Short.BYTES];
for(int i = 0; i < Short.BYTES; i++)
temp[i] = src[i + ptr];
ByteBuffer bb = ByteBuffer.wrap(temp);
return bb.getShort();
}
static int readInt(List<Byte> src, int ptr) {
if(ptr >= src.size() || ptr < 0)
return 0;
byte[] temp = new byte[Integer.BYTES];
for(int i = 0; i < Integer.BYTES; i++)
temp[i] = src.get(i + ptr);
ByteBuffer bb = ByteBuffer.wrap(temp);
return bb.getInt();
}
static int readInt(byte[] src, int ptr) {
if(ptr >= src.length || ptr < 0)
return 0;
byte[] temp = new byte[Integer.BYTES];
for(int i = 0; i < Integer.BYTES; i++)
temp[i] = src[i + ptr];
ByteBuffer bb = ByteBuffer.wrap(temp);
return bb.getInt();
}
static long readLong(List<Byte> src, int ptr) {
if(ptr >= src.size() || ptr < 0)
return 0;
byte[] temp = new byte[Long.BYTES];
for(int i = 0; i < Long.BYTES; i++)
temp[i] = src.get(i + ptr);
ByteBuffer bb = ByteBuffer.wrap(temp);
return bb.getLong();
}
static long readLong(byte[] src, int ptr) {
if(ptr >= src.length || ptr < 0)
return 0;
byte[] temp = new byte[Long.BYTES];
for(int i = 0; i < Long.BYTES; i++)
temp[i] = src[i + ptr];
ByteBuffer bb = ByteBuffer.wrap(temp);
return bb.getLong();
}
static String readString(List<Byte> src, int strLen, int ptr) {
if(ptr >= src.size() || ptr < 0)
return "";
char[] temp = new char[strLen];
for(int i = 0; i < strLen; i++)
temp[i] = (char)src.get(i + ptr).byteValue();
return new String(temp);
}
static String readString(byte[] src, int strLen, int ptr) {
if(ptr >= src.length || ptr < 0)
return "";
char[] temp = new char[strLen];
for(int i = 0; i < strLen; i++)
temp[i] = (char)src[i + ptr];
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();
}
}

View File

@ -0,0 +1,118 @@
package org.hwo.serialize;
import java.util.ArrayList;
import java.util.List;
public class Database {
//HEADER
//[4]String DatabaseID
//[4] Short ObjectCount
//[2] Short InfoSize
//[InfoSize] Info
//[4] String "DATA"
private static final String DATABASE_ID = "HF3B";
private List<DatabaseObject> objects;
private long sizeInBytes;
public Database() {
this.objects = new ArrayList<DatabaseObject>();
}
public void addObject(DatabaseObject obj) {
if(this.objects.contains(obj))
return;
this.objects.add(obj);
}
public void addObject(String name) {
DatabaseObject temp = new DatabaseObject(name);
if(temp != null)
this.objects.add(temp);
}
public DatabaseObject getObject(String name) {
for(DatabaseObject o : objects)
if(o.getName().equals(name))
return o;
return null;
}
public void removeObject(DatabaseObject obj) {
if(this.objects.contains(obj))
return;
int index = this.objects.indexOf(obj);
this.objects.remove(index);
}
public void removeObject(String obj) {
DatabaseObject temp = getObject(obj);
removeObject(temp);
}
public String toString() {
String result = new String();
result += "==========Database==========" + "\n";
for(DatabaseObject o : this.objects)
result += o.toString(5);
result += "\n";
return result;
}
public void print() {
System.out.print(this.toString());
}
public byte[] serialize() {
List<Byte> bytes = new ArrayList<Byte>();
BinaryParser.writeBytes(bytes, DATABASE_ID);
BinaryParser.writeBytes(bytes, (short)this.objects.size());
BinaryParser.writeBytes(bytes, "DATA");
for(DatabaseObject obj : this.objects) {
byte[] temp = obj.serialize();
for(byte b : temp)
bytes.add(b);
}
byte[] result = new byte[bytes.size()];
for(int i = 0; i < bytes.size(); i++)
result[i] = bytes.get(i);
this.sizeInBytes = result.length;
bytes = null;
return result;
}
public static Database deserialize(byte[] src) {
//HEADER
//[4]String DatabaseID
//[4] Short ObjectCount
//[4] String "DATA"
int ptr = 0;
String tDbId = BinaryParser.readString(src, 4, ptr);
ptr += 4;
if(!tDbId.equals(DATABASE_ID))
return null;
short tObjCount = BinaryParser.readShort(src, ptr);
ptr += 2;
String tDataId = BinaryParser.readString(src, 4, ptr);
ptr += 4;
Database result = new Database();
for(int i = 0; i < tObjCount; i++) {
DatabaseObject obj = DatabaseObject.deserialize(src, ptr);
ptr += obj.getSizeInBytes();
result.addObject(obj);
}
return result;
}
}

View File

@ -0,0 +1,498 @@
package org.hwo.serialize;
import java.util.ArrayList;
import java.util.List;
public class DatabaseField {
//FORMAT:
//[4] String FieldID (DFLD)
//[1] Byte FieldType
//[2] Short NameLength
//[NameLength] String Name
// if type==string [2] Short ValueLength
//[TypeLength] Value
//second nibble == type, first nibble == type if array
public static final byte BYTE = 0x00;
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 = 0x0F;
private static final String FIELD_ID = "DFLD";
public enum Type {
BYTE,
SHORT,
INT,
LONG,
FLOAT,
STRING,
OBJECT,
ARRAY
}
private class _valueType<T>{
private T value;
public _valueType(T value) {
this.value = value;
}
@SuppressWarnings("unused")
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
}
private byte type;
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, 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);
}
}
private void initialize(Object value) {
switch(_byteToType(this.type)) {
case BYTE:
setValue((byte)value);
break;
case SHORT:
setValue((short)value);
break;
case INT:
setValue((int)value);
break;
case LONG:
setValue((long)value);
break;
case FLOAT:
setValue((float)value);
break;
case STRING:
setValue((String)value);
break;
case OBJECT:
setValue((DatabaseObject)value);
break;
case ARRAY:
//setValue((List<?>)value);
break;
}
}
public Type getType() {
return _byteToType(this.type);
}
private static Type _byteToType(byte t) {
switch(t) {
case 0x00:
return Type.BYTE;
case 0x01:
return Type.SHORT;
case 0x02:
return Type.INT;
case 0x03:
return Type.LONG;
case 0x04:
return Type.STRING;
case 0x05:
return Type.OBJECT;
case 0x06:
return Type.FLOAT;
default:
break;
}
if(t >= 0x0F)
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 FLOAT:
return 0x06;
case ARRAY:
return 0x0F;
default:
return -1;
}
}
public String getName() {
return this.name;
}
private<T> void _setValue(T value) {
this.value = new _valueType<T>(value);
}
public void setValue(byte value) { _setValue(value); }
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) { this.arrayValue = value; }
@SuppressWarnings("unchecked")
public<T> T getValue(){
if(this.type >= 0x0F)
return (T)this.arrayValue;
return (T)this.value.getValue();
}
public int getSizeInBytes() {
if(this.sizeInBytes <= 0)
this.sizeInBytes = 11 + typeSize(_byteToType(this.type)) + this.name.length();
return this.sizeInBytes;
}
private int typeSize(Type t) {
switch(t) {
case BYTE:
return Byte.BYTES;
case SHORT:
return Short.BYTES;
case INT:
return Integer.BYTES;
case LONG:
return Long.BYTES;
case FLOAT:
return Float.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();
for(int i = 0; i < indendation; i++)
indend += " ";
result += indend + "Field " + this.name + "\n";
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<Byte>)arrayValue).get(i) + " ";
break;
case SHORT:
result += ((ArrayList<Short>)arrayValue).get(i) + " ";
break;
case INT:
result += ((ArrayList<Integer>)arrayValue).get(i) + " ";
break;
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;
case OBJECT:
//result += indend + ((DatabaseObject)((List<Object>)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";
return result;
}
public String toString() {
return toString(0);
}
public void print(int indendation) {
System.out.print(this.toString(indendation));
}
public void print() {
print(0);
}
@SuppressWarnings("unchecked")
public byte[] serialize() {
List<Byte> bytes = new ArrayList<Byte>();
BinaryParser.writeBytes(bytes, FIELD_ID);
BinaryParser.writeBytes(bytes, this.type);
BinaryParser.writeBytes(bytes, (short)this.name.length());
BinaryParser.writeBytes(bytes, this.name);
switch(getType()) {
case BYTE:
BinaryParser.writeBytes(bytes, (int)Byte.BYTES);
BinaryParser.writeBytes(bytes, (byte)getValue());
break;
case SHORT:
BinaryParser.writeBytes(bytes, (int)Short.BYTES);
BinaryParser.writeBytes(bytes, (short)getValue());
break;
case INT:
BinaryParser.writeBytes(bytes, (int)Integer.BYTES);
BinaryParser.writeBytes(bytes, (int)getValue());
break;
case LONG:
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());
break;
case OBJECT:
BinaryParser.writeBytes(bytes, (int)(((DatabaseObject)getValue()).getSizeInBytes()));
byte[] temp = ((DatabaseObject)getValue()).serialize();
for(byte b : temp)
bytes.add(b);
case ARRAY:
break;
}
//serialize array
if(this.type >= 0x0F) {
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<Byte>)arrayValue).get(i));
break;
case SHORT:
BinaryParser.writeBytes(bytes, (int)(Short.BYTES * arrLen));
for(int i = 0; i < arrLen; i++)
BinaryParser.writeBytes(bytes, ((List<Short>)arrayValue).get(i));
break;
case INT:
BinaryParser.writeBytes(bytes, (int)(Integer.BYTES * arrLen));
for(int i = 0; i < arrLen; i++)
BinaryParser.writeBytes(bytes,((List<Integer>)arrayValue).get(i));
break;
case LONG:
BinaryParser.writeBytes(bytes, (int)(Long.BYTES * arrLen));
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:
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);
this.sizeInBytes = result.length;
bytes = null;
return result;
}
@SuppressWarnings("unchecked")
public static DatabaseField deserialize(byte[] src, int ptr) {
String tid;
byte ttype;
short tnameLen;
String tname;
int tvalueLen = 0;
Object tvalue = null;
DatabaseField result = null;
tid = BinaryParser.readString(src, 4, ptr);
ptr += 4;
if(!tid.equals(FIELD_ID)) {
//throw new Exception();
System.out.println("Field to deserialize is not of type Field! \nID read: " + tid);
return null;
}
ttype = BinaryParser.readByte(src, ptr);
ptr += 1;
tnameLen = BinaryParser.readShort(src, ptr);
ptr += 2;
tname = BinaryParser.readString(src, tnameLen, ptr);
ptr += tnameLen;
tvalueLen = BinaryParser.readInt(src, ptr);
ptr += 4;
//Length = 11 + NameLength + TypeLength
switch(_byteToType(ttype)) {
case BYTE:
tvalue = BinaryParser.readByte(src, ptr);
result = new DatabaseField(ttype, tname, (byte)tvalue, false);
break;
case SHORT:
tvalue = BinaryParser.readShort(src, ptr);
result = new DatabaseField(ttype, tname, (short)tvalue, false);
break;
case INT:
tvalue = BinaryParser.readInt(src, ptr);
result = new DatabaseField(ttype, tname, (int)tvalue, false);
break;
case LONG:
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);
break;
case OBJECT:
tvalue = DatabaseObject.deserialize(src, ptr);
result = new DatabaseField(ttype, tname, (DatabaseObject)tvalue, false);
break;
case ARRAY:
break;
}
//Array Type
if(ttype >= 0x06) {
switch(_byteToType((byte)(ttype >> 4))) {
case BYTE:
tvalue = new ArrayList<Byte>();
for(int i = 0; i < (tvalueLen / Byte.BYTES); i++) {
((ArrayList<Byte>)tvalue).add(BinaryParser.readByte(src, ptr));
ptr += Byte.BYTES;
}
result = new DatabaseField((byte)(ttype >> 4), tname, (ArrayList<Byte>)tvalue, true);
break;
case SHORT:
tvalue = new ArrayList<Short>();
for(int i = 0; i < (tvalueLen / Short.BYTES); i++) {
((ArrayList<Short>)tvalue).add(BinaryParser.readShort(src, ptr));
ptr += Short.BYTES;
}
result = new DatabaseField((byte)(ttype >> 4), tname, (ArrayList<Short>)tvalue, true);
break;
case INT:
tvalue = new ArrayList<Integer>();
for(int i = 0; i < (tvalueLen / Integer.BYTES); i++) {
((ArrayList<Integer>)tvalue).add(BinaryParser.readInt(src, ptr));
ptr += Integer.BYTES;
}
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 += Long.BYTES;
}
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;
case OBJECT:
break;
case ARRAY:
break;
}
}
//result.sizeInBytes = 11 + tnameLen + tvalueLen;
return result;
}
}

View File

@ -0,0 +1,217 @@
package org.hwo.serialize;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;
public class DatabaseObject {
//OBJECT
//[4] String ObjectID ("OBJT")
//[2] Short NameLen
//[NameLen] String Name
//[2] Short NumFields
private static final String OBJECT_ID = "OBJT";
private List<DatabaseField> fields;
private String name;
private int sizeInBytes;
public DatabaseObject(String name) {
this.name = name;
this.fields = new ArrayList<DatabaseField>();
}
public void addField(DatabaseField field) {
if(fields.contains(field))
return;
fields.add(field);
}
public void addField(String name, byte 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, false);
if(temp != null)
addField(temp);
}
public void addField(String name, int 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, false);
if(temp != null)
addField(temp);
}
public void addField(String name, String 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, false);
if(temp != null)
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)
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 if(check instanceof Float)
temp = new DatabaseField(DatabaseField.FLOAT, name, value, true);
else
return;
if(temp != null)
addField(temp);
}
public void removeField(DatabaseField field) {
if(fields.contains(field))
return;
int index = fields.indexOf(field);
fields.remove(index);
}
public DatabaseField getField(String name) {
for(DatabaseField f : fields)
if(f.getName().equals(name))
return f;
return null;
}
public <T>T getValue(String name){
DatabaseField f = getField(name);
if(f != null)
return f.getValue();
return null;
}
public String getName() {
return this.name;
}
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();
for(int i = 0; i < indendation; i++)
indend += " ";
result += indend + "Object: " + this.name + "\n";
result += indend + "Fields: " + "\n";
for(DatabaseField f : this.fields) {
result += f.toString(indendation + 5);
}
return result;
}
public String toString() {
return toString(0);
}
public void print(int indendation) {
System.out.print(this.toString(indendation));
}
public void print() {
print(0);
}
public byte[] serialize() {
List<Byte> bytes = new ArrayList<Byte>();
BinaryParser.writeBytes(bytes, OBJECT_ID); //Object ID
BinaryParser.writeBytes(bytes, (short)this.name.length()); //Name Length
BinaryParser.writeBytes(bytes, this.name); // Name
BinaryParser.writeBytes(bytes, (short)this.fields.size()); //Field Count
for(DatabaseField f : this.fields) {
byte[] temp = f.serialize();
for(byte b : temp)
bytes.add(b);
}
byte[] result = new byte[bytes.size()];
for(int i = 0; i < bytes.size(); i++)
result[i] = bytes.get(i);
this.sizeInBytes = result.length;
bytes = null;
return result;
}
//@SuppressWarnings("unused")
public static DatabaseObject deserialize(byte[] src, int ptr) {
String tObjId = BinaryParser.readString(src, 4, ptr);
ptr += 4;
if(!tObjId.equals(OBJECT_ID))
return null;
short tNameLen = BinaryParser.readShort(src, ptr);
ptr += 2;
String tName = BinaryParser.readString(src, tNameLen, ptr);
ptr += tNameLen;
short tFieldCount = BinaryParser.readShort(src, ptr);
ptr += 2;
DatabaseObject result = new DatabaseObject(tName);
DatabaseField field = null;
int fieldSize = 0;
for(short i = 0; i < tFieldCount; i++) {
field = DatabaseField.deserialize(src, ptr);
if(field != null) {
ptr += field.getSizeInBytes();
fieldSize += field.getSizeInBytes();
result.addField(field);
}
}
result.sizeInBytes = 8 + tNameLen + fieldSize;
return result;
}
}

View File

@ -0,0 +1,23 @@
HEADER
[4] String DatabaseID
[2] Short ObjectCount
[4] String "DATA"
OBJECT
[4] String ObjectID ("OBJT")
[2] Short NameLen
[NameLen] String Name
[2] Short NumFields
Length = 8 + NameLen + Fields.Length
FIELD
[4] String FieldID
[1] Byte FieldType
[2] Short NameLength
[NameLength] String Name
[4] Int ValueLength
[TypeLength] Value
Length = 11 + NameLength + TypeLength