Added basic file management

This commit is contained in:
jsrobson10 2019-07-07 19:26:18 +10:00
parent 3d1b08ae41
commit 331f820c60
4 changed files with 127 additions and 20 deletions

View File

@ -1,8 +1,7 @@
package bdf;
import bdf.data.BdfDatabase;
import bdf.file.BdfFileManager;
import bdf.types.BdfArray;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
public class Main {
@ -10,18 +9,17 @@ public class Main {
public static void main(String[] args)
{
BdfObject object = BdfObject.getNew(new BdfNamedList());
BdfFileManager file = new BdfFileManager("db.bdf");
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));
file.setArray(new BdfArray());
file.getArray().add(BdfObject.getNew("Hello"));
System.out.println(file.getArray().get(0).getString());
file.saveDatabase();
System.out.println("value = \""+ object2.getNamedList().get("20").getString()+"\"");
System.out.println(object2.getNamedList().contains("54"));
BdfFileManager file2 = new BdfFileManager("db.bdf");
System.out.println(file2.getArray().get(0).getString());
}

42
bdf/file/BdfFileManager.java Executable file
View File

@ -0,0 +1,42 @@
package bdf.file;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import bdf.data.BdfDatabase;
import bdf.types.BdfObject;
import bdf.util.FileHelpers;
public class BdfFileManager extends BdfObject
{
protected String path;
public BdfFileManager(String path) {
super(new BdfDatabase(FileHelpers.readAllIgnoreErrors(path)));
this.path = path;
}
public void saveDatabase(String path)
{
try
{
// Get the database file for output
FileOutputStream out = new FileOutputStream(path);
// Write the database to the file
out.write(this.serialize().getBytes());
// Close the file output stream
out.close();
}
catch(IOException e) {
return;
}
}
public void saveDatabase() {
this.saveDatabase(this.path);
}
}

View File

@ -18,16 +18,24 @@ public class BdfObject implements IBdfType
public BdfObject(BdfDatabase data)
{
this();
// Is the database length greater than 1
if(data.length() > 1)
{
// 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);
}
// 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);
else
{
// Create a new database
database = new BdfDatabase();
}
}
public BdfObject() {

59
bdf/util/FileHelpers.java Executable file
View File

@ -0,0 +1,59 @@
package bdf.util;
import java.io.FileInputStream;
import java.io.IOException;
public class FileHelpers
{
public static byte[] readAll(FileInputStream in)
{
try
{
// Get bytes to return
int available = in.available();
byte[] bytes = new byte[available];
// Loop over the available bytes
for(int i=0;i<available;i++)
{
// Add the next byte from the stream to the bytes
bytes[i] = (byte) in.read();
}
// Send the bytes collected from the file stream back
return bytes;
}
catch (IOException e)
{
// Throw the IOException as a runtime exception
throw new RuntimeException(e);
}
}
public static byte[] readAll(String path) throws IOException
{
// Create the file input stream
FileInputStream in = new FileInputStream(path);
// Load all of its data
byte[] data = readAll(in);
// Close the file input stream
in.close();
// Send back the data
return data;
}
public static byte[] readAllIgnoreErrors(String path)
{
try {
return readAll(path);
}
catch(IOException e) {
return new byte[0];
}
}
}