Added basic file management
This commit is contained in:
parent
3d1b08ae41
commit
331f820c60
|
@ -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());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -18,8 +18,9 @@ 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());
|
||||
|
@ -30,6 +31,13 @@ public class BdfObject implements IBdfType
|
|||
if(type == BdfTypes.NAMED_LIST) object = new BdfNamedList(database);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// Create a new database
|
||||
database = new BdfDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
public BdfObject() {
|
||||
database = new BdfDatabase();
|
||||
}
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue