First Commit

This commit is contained in:
jsrobson10 2019-07-07 18:14:47 +10:00
commit 3d1b08ae41
9 changed files with 634 additions and 0 deletions

28
bdf/Main.java Executable file
View File

@ -0,0 +1,28 @@
package bdf;
import bdf.data.BdfDatabase;
import bdf.types.BdfArray;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
public class Main {
public static void main(String[] args)
{
BdfObject object = BdfObject.getNew(new BdfNamedList());
for(int i=0;i<1000;i++)
{
object.getNamedList().set(Integer.toString(i), BdfObject.getNew("value + 1 = "+Integer.toString(i+1)));
}
byte[] database = object.serialize().getBytes();
BdfObject object2 = new BdfObject(new BdfDatabase(database));
System.out.println("value = \""+ object2.getNamedList().get("20").getString()+"\"");
System.out.println(object2.getNamedList().contains("54"));
}
}

62
bdf/data/BdfDatabase.java Executable file
View File

@ -0,0 +1,62 @@
package bdf.data;
import java.nio.charset.StandardCharsets;
public class BdfDatabase
{
protected byte[] database = null;
public BdfDatabase() {
this.database = new byte[0];
}
public BdfDatabase(String database) {
this.database = database.getBytes();
}
public BdfDatabase(byte ...database) {
this.database = database;
}
public BdfDatabase getAt(int start, int end)
{
byte[] split = new byte[end - start];
for(int i=start;i<end;i++) {
split[i-start] = this.database[i];
}
return new BdfDatabase(split);
}
public int length() {
return this.database.length;
}
public byte[] getBytes() {
return this.database;
}
public byte getByte(int i) {
return this.database[i];
}
public String getString() {
return new String(this.database, StandardCharsets.UTF_8);
}
public static BdfDatabase add(BdfDatabase d1, BdfDatabase d2)
{
byte[] added = new byte[d1.length() + d2.length()];
for(int i=0;i<d1.length();i++) {
added[i] = d1.getByte(i);
}
for(int i=0;i<d2.length();i++) {
added[d1.length()+i] = d2.getByte(i);
}
return new BdfDatabase(added);
}
}

View File

@ -0,0 +1,11 @@
package bdf.exception;
public class UndefinedKeyException extends RuntimeException
{
private static final long serialVersionUID = 1L;
public UndefinedKeyException(String key)
{
super("The key \""+key+"\" has not been defined.");
}
}

88
bdf/types/BdfArray.java Executable file
View File

@ -0,0 +1,88 @@
package bdf.types;
import java.util.ArrayList;
import bdf.data.BdfDatabase;
import bdf.util.DataHelpers;
public class BdfArray implements IBdfType
{
protected ArrayList<BdfObject> elements = new ArrayList<BdfObject>();
public BdfArray() {
}
public BdfArray(BdfDatabase data)
{
// Create an iterator value to loop over the data
int i = 0;
// Loop over the data
while(i < data.length())
{
// Get the size of the object
int size = DataHelpers.getByteBuffer(data.getAt(i, (i+(Integer.SIZE/8)))).getInt();
// Get the object
BdfObject object = new BdfObject(data.getAt((i+(Integer.SIZE/8)), (i+(Integer.SIZE/8)+size)));
// Add the object to the elements list
elements.add(object);
// Increase the iterator by the amount of bytes
i += (Integer.SIZE/8)+size;
}
}
@Override
public BdfDatabase serialize()
{
// Create the serialized data string
BdfDatabase serialized = new BdfDatabase();
// Loop over the elements
for(BdfObject o : elements)
{
// Convert the object to a string
BdfDatabase db = o.serialize();
// Add the serialized object to the serialized data
serialized = BdfDatabase.add(serialized, DataHelpers.serializeInt(db.length()));
serialized = BdfDatabase.add(serialized, db);
}
// Send back the serialized data
return serialized;
}
public BdfArray add(BdfObject o)
{
// Add an element
elements.add(o);
return this;
}
public BdfArray clear()
{
// Clear the elements
elements.clear();
return this;
}
public BdfArray remove(int index)
{
elements.remove(index);
return this;
}
public BdfObject get(int index) {
return elements.get(index);
}
public int size() {
return elements.size();
}
}

178
bdf/types/BdfNamedList.java Executable file
View File

@ -0,0 +1,178 @@
package bdf.types;
import java.util.ArrayList;
import bdf.data.BdfDatabase;
import bdf.exception.UndefinedKeyException;
import bdf.util.DataHelpers;
public class BdfNamedList implements IBdfType
{
protected class Element
{
public byte[] key;
public BdfObject object;
}
ArrayList<Element> elements = new ArrayList<Element>();
public BdfNamedList() {
}
public BdfNamedList(BdfDatabase data)
{
// Create an iterator value to loop over the data
int i = 0;
// Loop over the data
while(i < data.length())
{
// Get the key
int key_size = DataHelpers.getByteBuffer(data.getAt(i, i+(Integer.SIZE/8))).getInt();
i += (Integer.SIZE/8);
byte[] key = data.getAt(i, i+key_size).getBytes();
// Get the object
i += key_size;
int object_size = DataHelpers.getByteBuffer(data.getAt(i, i+(Integer.SIZE/8))).getInt();
i += (Integer.SIZE/8);
BdfObject object = new BdfObject(data.getAt(i, i+object_size));
// Create a new element and save some data to it
Element element = new Element();
element.object = object;
element.key = key;
// Add the object to the elements list
elements.add(element);
// Increase the iterator by the amount of bytes
i += object_size;
}
}
@Override
public BdfDatabase serialize()
{
// Create the serialized data string
BdfDatabase serialized = new BdfDatabase();
// Loop over the elements
for(Element o : elements)
{
// Add the serialized data to the data string
BdfDatabase data = o.object.serialize();
serialized = BdfDatabase.add(serialized, DataHelpers.serializeInt(o.key.length));
serialized = BdfDatabase.add(serialized, new BdfDatabase(o.key));
serialized = BdfDatabase.add(serialized, DataHelpers.serializeInt(data.length()));
serialized = BdfDatabase.add(serialized, data);
}
// Send back the serialized data
return serialized;
}
public BdfObject get(String key)
{
// Get the object to send back
BdfObject object = null;
// Convert the key to bytes
byte[] key_bytes = key.getBytes();
// Loop over the elements
for(Element e : elements)
{
// Is this the element key
if(DataHelpers.bytesAreEqual(e.key, key_bytes))
{
// Set the object
object = e.object;
// Return the object
return object;
}
}
// Raise an error
throw new UndefinedKeyException(key);
}
public void remove(String key)
{
// Convert the key to bytes
byte[] key_bytes = key.getBytes();
// Loop over the elements
for(int i=0;i<elements.size();i++)
{
// Get the element
Element e = elements.get(i);
// Is the specified key the same as the elements key
if(DataHelpers.bytesAreEqual(e.key, key_bytes))
{
// Delete this element
elements.remove(i);
// Exit out of the function, prevent NullPointException
return;
}
}
// Raise an error
throw new UndefinedKeyException(key);
}
public void set(String key, BdfObject object)
{
// Convert the key to bytes
byte[] key_bytes = key.getBytes();
// Loop over the elements, does it already exist
for(Element e : elements)
{
// Is the key here the same as the specified key
if(DataHelpers.bytesAreEqual(e.key, key_bytes))
{
// Set the new object
e.object = object;
// Exit out of the function, don't add another object
}
}
// Create a new element object
Element e = new Element();
e.key = key_bytes;
e.object = object;
// Add the new element object to the elements list
elements.add(e);
}
public boolean contains(String key)
{
// Convert the key to bytes
byte[] key_bytes = key.getBytes();
// Loop over the elements
for(Element e : elements)
{
// Is the elements key the same as the specified key
if(DataHelpers.bytesAreEqual(e.key, key_bytes))
{
// Send back true to say the element was found
return true;
}
}
// Send back false if nothing was found
return false;
}
public int size() {
return elements.size();
}
}

204
bdf/types/BdfObject.java Executable file
View File

@ -0,0 +1,204 @@
package bdf.types;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import bdf.data.BdfDatabase;
import bdf.util.DataHelpers;
public class BdfObject implements IBdfType
{
protected BdfDatabase database = null;
protected Object object = null;
protected byte type = BdfTypes.EMPTY;
public static BdfObject getNew() {
return new BdfObject();
}
public BdfObject(BdfDatabase data)
{
this();
// Get the type and database values
type = data.getAt(0, 1).getByte(0);
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.ARRAY) object = new BdfArray(database);
if(type == BdfTypes.NAMED_LIST) object = new BdfNamedList(database);
}
public BdfObject() {
database = new BdfDatabase();
}
public static BdfObject getNew(int v) {
return (new BdfObject()).setInteger(v);
}
public static BdfObject getNew(byte v) {
return (new BdfObject()).setByte(v);
}
public static BdfObject getNew(boolean v) {
return (new BdfObject()).setBoolean(v);
}
public static BdfObject getNew(float v) {
return (new BdfObject()).setFloat(v);
}
public static BdfObject getNew(double v) {
return (new BdfObject()).setDouble(v);
}
public static BdfObject getNew(long v) {
return (new BdfObject()).setLong(v);
}
public static BdfObject getNew(short v) {
return (new BdfObject()).setShort(v);
}
public static BdfObject getNew(String v) {
return (new BdfObject()).setString(v);
}
public static BdfObject getNew(BdfArray v) {
return (new BdfObject()).setArray(v);
}
public static BdfObject getNew(BdfNamedList v) {
return (new BdfObject()).setNamedList(v);
}
@Override
public BdfDatabase serialize()
{
if(type == BdfTypes.STRING) database = new BdfDatabase(this.getString());
if(type == BdfTypes.ARRAY) database = ((BdfArray)object).serialize();
if(type == BdfTypes.NAMED_LIST) database = ((BdfNamedList)object).serialize();
return BdfDatabase.add(new BdfDatabase(type), database);
}
public byte getType() {
return this.type;
}
public int getInteger() {
return DataHelpers.getByteBuffer(database).getInt(0);
}
public byte getByte() {
return database.getByte(0);
}
public boolean getBoolean() {
return database.getByte(0) == 0x00;
}
public double getDouble() {
return DataHelpers.getByteBuffer(database).getDouble(0);
}
public float getFloat() {
return DataHelpers.getByteBuffer(database).getFloat(0);
}
public long getLong() {
return DataHelpers.getByteBuffer(database).getLong(0);
}
public short getShort() {
return DataHelpers.getByteBuffer(database).getShort(0);
}
public String getString() {
return (String)object;
}
public BdfArray getArray() {
return (BdfArray)object;
}
public BdfNamedList getNamedList() {
return (BdfNamedList)object;
}
public BdfObject setInteger(int value) {
this.type = BdfTypes.INTEGER;
ByteBuffer b = ByteBuffer.allocate(Integer.SIZE/8);
b.putInt(0, value);
database = DataHelpers.getDatabase(b);
return this;
}
public BdfObject setByte(byte value) {
this.type = BdfTypes.BYTE;
database = new BdfDatabase(value);
return this;
}
public BdfObject setBoolean(boolean value) {
this.type = BdfTypes.BOOLEAN;
if(value) database = new BdfDatabase((byte)0x01);
else database = new BdfDatabase((byte)0x00);
return this;
}
public BdfObject setDouble(double value) {
this.type = BdfTypes.DOUBLE;
ByteBuffer b = ByteBuffer.allocate(Double.SIZE/8);
b.putDouble(0, value);
database = DataHelpers.getDatabase(b);
return this;
}
public BdfObject setFloat(float value) {
this.type = BdfTypes.FLOAT;
ByteBuffer b = ByteBuffer.allocate(Float.SIZE/8);
b.putFloat(0, value);
database = DataHelpers.getDatabase(b);
return this;
}
public BdfObject setLong(long value) {
this.type = BdfTypes.LONG;
ByteBuffer b = ByteBuffer.allocate(Long.SIZE/8);
b.putLong(0, value);
database = DataHelpers.getDatabase(b);
return this;
}
public BdfObject setShort(short value) {
this.type = BdfTypes.SHORT;
ByteBuffer b = ByteBuffer.allocate(Short.SIZE/8);
b.putShort(0, value);
database = DataHelpers.getDatabase(b);
return this;
}
public BdfObject setString(String value) {
this.type = BdfTypes.STRING;
this.database = new BdfDatabase(value);
this.object = value;
return this;
}
public BdfObject setArray(BdfArray value) {
this.type = BdfTypes.ARRAY;
this.database = value.serialize();
this.object = value;
return this;
}
public BdfObject setNamedList(BdfNamedList value) {
this.type = BdfTypes.NAMED_LIST;
this.database = value.serialize();
this.object = value;
return this;
}
}

16
bdf/types/BdfTypes.java Executable file
View File

@ -0,0 +1,16 @@
package bdf.types;
public class BdfTypes
{
public static final byte BOOLEAN = 0;
public static final byte INTEGER = 1;
public static final byte LONG = 2;
public static final byte SHORT = 3;
public static final byte BYTE = 4;
public static final byte DOUBLE = 5;
public static final byte FLOAT = 6;
public static final byte STRING = 7;
public static final byte ARRAY = 8;
public static final byte NAMED_LIST = 9;
public static final byte EMPTY = 10;
}

8
bdf/types/IBdfType.java Executable file
View File

@ -0,0 +1,8 @@
package bdf.types;
import bdf.data.BdfDatabase;
public interface IBdfType
{
public BdfDatabase serialize();
}

39
bdf/util/DataHelpers.java Executable file
View File

@ -0,0 +1,39 @@
package bdf.util;
import java.nio.ByteBuffer;
import bdf.data.BdfDatabase;
public class DataHelpers
{
public static ByteBuffer getByteBuffer(BdfDatabase db) {
return ByteBuffer.wrap(db.getBytes());
}
public static BdfDatabase getDatabase(ByteBuffer buffer) {
return new BdfDatabase(buffer.array());
}
public static BdfDatabase serializeInt(int value)
{
ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE/8);
buffer.putInt(value);
return getDatabase(buffer);
}
public static boolean bytesAreEqual(byte[] b1, byte[] b2)
{
// Send back false if the lengths are different
if(b1.length != b2.length) return false;
// Loop over the bytes
for(int i=0;i<b1.length;i++)
{
// Send back false if the bytes are different
if(b1[i] != b2[i]) return false;
}
// Send back true if everything has been checked
return true;
}
}