Added the ByteBuffer type

This commit is contained in:
josua 2019-09-21 13:27:56 +10:00
parent 5a471d0f8c
commit 8ed682653e
3 changed files with 44 additions and 50 deletions

View File

@ -3,6 +3,8 @@ package bdf.types;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import com.sun.org.apache.bcel.internal.generic.Type;
import bdf.data.BdfDatabase;
import bdf.util.DataHelpers;
@ -26,7 +28,7 @@ public class BdfObject implements IBdfType
database = data.getAt(1, data.length());
// Set the object variable if there is an object specified
if(type == BdfTypes.STRING) object = new String(database.getBytes(), StandardCharsets.UTF_8);
if(type == BdfTypes.STRING) object = database.getBytes();
if(type == BdfTypes.ARRAY) object = new BdfArray(database);
if(type == BdfTypes.NAMED_LIST) object = new BdfNamedList(database);
}
@ -41,7 +43,7 @@ public class BdfObject implements IBdfType
@Override
public BdfDatabase serialize()
{
if(type == BdfTypes.STRING) database = new BdfDatabase(this.getString());
if(type == BdfTypes.STRING) database = new BdfDatabase(this.getByteBuffer().array());
if(type == BdfTypes.ARRAY) database = ((BdfArray)object).serialize();
if(type == BdfTypes.NAMED_LIST) database = ((BdfNamedList)object).serialize();
@ -138,7 +140,15 @@ public class BdfObject implements IBdfType
if(this.type != BdfTypes.STRING)
this.setString("");
return (String)object;
return new String((byte[])object, StandardCharsets.UTF_8);
}
public ByteBuffer getByteBuffer()
{
if(this.type != BdfTypes.STRING)
this.setString("");
return ByteBuffer.wrap((byte[])object);
}
public BdfArray getArray()
@ -212,8 +222,15 @@ public class BdfObject implements IBdfType
public BdfObject setString(String value) {
this.type = BdfTypes.STRING;
this.database = new BdfDatabase(value);
this.object = value;
this.database = new BdfDatabase(value.getBytes());
this.object = value.getBytes();
return this;
}
public BdfObject setByteBuffer(ByteBuffer value) {
this.type = BdfTypes.STRING;
this.database = new BdfDatabase(value.array());
this.object = value.array();
return this;
}
@ -269,6 +286,10 @@ public class BdfObject implements IBdfType
return (new BdfObject()).setString(v);
}
public static BdfObject withByteBuffer(ByteBuffer v) {
return (new BdfObject()).setByteBuffer(v);
}
public static BdfObject withArray(BdfArray v) {
return (new BdfObject()).setArray(v);
}

View File

@ -3,7 +3,6 @@ package tests;
import bdf.classes.IBdfClassManager;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import bdf.types.BdfTypes;
public class TestClass implements IBdfClassManager
{

View File

@ -1,8 +1,10 @@
package tests;
import java.nio.ByteBuffer;
import java.util.Iterator;
import bdf.classes.BdfClassManager;
import bdf.data.BdfDatabase;
import bdf.file.BdfFileManager;
import bdf.types.BdfArray;
import bdf.types.BdfNamedList;
@ -12,54 +14,26 @@ public class Tests {
public static void main(String[] args)
{
BdfFileManager bdf = new BdfFileManager("db.bdf");
BdfNamedList nl = bdf.getNamedList();
BdfObject bdf = new BdfObject();
BdfObject class1 = nl.get("class1");
BdfObject class2 = nl.get("class2");
TestClass t1 = new TestClass();
BdfClassManager m1 = new BdfClassManager(t1);
m1.setBdf(class1);
TestClass t2 = new TestClass();
BdfClassManager m2 = new BdfClassManager(t2);
m2.setBdf(class2);
m1.load();
m2.load();
t1.tick();
t2.tick();
m1.save();
m2.save();
bdf.saveDatabase();
System.out.println(bdf.serializeHumanReadable());
BdfArray a = new BdfArray();
a.add(BdfObject.withInteger(1));
a.add(BdfObject.withInteger(534));
a.add(BdfObject.withInteger(32));
a.add(BdfObject.withInteger(22));
a.add(BdfObject.withInteger(12));
Iterator<BdfObject> i = a.iterator();
while(i.hasNext())
{
System.out.println(i.next().getInteger());
i.remove();
ByteBuffer bb = ByteBuffer.allocate(Integer.BYTES*20);
for(int i=0;i<bb.capacity()/Integer.BYTES;i+=1) {
System.out.println("WRITE ("+i+"): "+i*10);
bb.putInt(i*Integer.BYTES, i*10);
}
Iterator<BdfObject> i2 = a.iterator();
bdf.setByteBuffer(bb);
BdfObject bdf2 = new BdfObject(new BdfDatabase(bdf.serialize().getBytes()));
while(i2.hasNext())
{
System.out.println(i2.next().getInteger());
ByteBuffer bb2 = bdf2.getByteBuffer();
/*for(int i=0;i<bb.capacity()/Integer.BYTES;i+=1) {
System.out.println("READ ("+i+"): "+bb.getInt(i*Integer.BYTES));
}*/
for(int i=0;i<bb2.capacity()/Integer.BYTES;i+=1) {
System.out.println("READ ("+i+"): "+bb2.getInt(i*Integer.BYTES));
}
}