Added the compressed file manager functionality to the BdfFileManager

class and fixed issues with boolean.
This commit is contained in:
josua 2020-07-21 21:17:51 +10:00
parent 23a49277c1
commit 984c78020a
4 changed files with 29 additions and 78 deletions

View File

@ -1,68 +0,0 @@
package bdf.file;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream;
import bdf.data.BdfDatabase;
import bdf.types.BdfObject;
import bdf.util.FileHelpers;
public class BdfCompressedFileManager extends BdfObject
{
protected String path;
private static BdfDatabase init(String path)
{
// Get the file
File file = new File(path);
// Does the file have read access
if(file.canRead())
{
// Return the files contents as a database
return new BdfDatabase(FileHelpers.readAllCompressedIgnoreErrors(path));
}
// Return an empty database if there is no read access
return new BdfDatabase(0);
}
public BdfCompressedFileManager(String path) {
super(init(path));
this.path = path;
}
public void saveDatabase(String path)
{
try
{
// Get the file handler
File file = new File(path);
// Create the parent directories
file.getAbsoluteFile().getParentFile().mkdirs();
// Get the database file for output
OutputStream out = new DeflaterOutputStream(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

@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream;
import bdf.data.BdfDatabase;
import bdf.types.BdfObject;
@ -12,28 +13,42 @@ import bdf.util.FileHelpers;
public class BdfFileManager extends BdfObject
{
protected String path;
private boolean compressed;
private static BdfDatabase init(String path)
private static BdfDatabase init(String path, boolean compressed)
{
// Get the file
File file = new File(path);
// Does the file have read access
if(file.canRead())
{
if(compressed)
{
// Return the files contents as a database
return new BdfDatabase(FileHelpers.readAllCompressedIgnoreErrors(path));
}
else
{
// Return the files contents as a database
return new BdfDatabase(FileHelpers.readAllIgnoreErrors(path));
}
}
// Return an empty database if there is no read access
return new BdfDatabase(0);
}
public BdfFileManager(String path) {
super(init(path));
public BdfFileManager(String path, boolean compressed) {
super(init(path, compressed));
this.path = path;
}
public BdfFileManager(String path) {
this(path, false);
}
public void saveDatabase(String path)
{
try
@ -47,8 +62,13 @@ public class BdfFileManager extends BdfObject
// Get the database file for output
OutputStream out = new FileOutputStream(path);
if(compressed) {
out = new DeflaterOutputStream(out);
}
// Write the database to the file
out.write(this.serialize().getBytes());
BdfDatabase db = this.serialize();
db.writeToStream(out);
// Close the file output stream
out.close();

View File

@ -515,14 +515,14 @@ public class BdfObject implements IBdfType
public BdfObject setByte(byte value) {
this.type = BdfTypes.BYTE;
database = new BdfDatabase(value);
database = new BdfDatabase(new byte[] {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);
if(value) database = new BdfDatabase(new byte[] {0x01});
else database = new BdfDatabase(new byte[] {0x00});
return this;
}

View File

@ -5,7 +5,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import bdf.data.BdfDatabase;
import bdf.file.BdfCompressedFileManager;
import bdf.file.BdfFileManager;
import bdf.types.BdfArray;
import bdf.types.BdfIndent;