Skip to content

Commit

Permalink
Added the compressed file manager functionality to the BdfFileManager
Browse files Browse the repository at this point in the history
class and fixed issues with boolean.
  • Loading branch information
jsrobson10 committed Jul 21, 2020
1 parent 23a4927 commit 984c780
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 78 deletions.
68 changes: 0 additions & 68 deletions src/bdf/file/BdfCompressedFileManager.java

This file was deleted.

32 changes: 26 additions & 6 deletions src/bdf/file/BdfFileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -12,28 +13,42 @@
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())
{
// Return the files contents as a database
return new BdfDatabase(FileHelpers.readAllIgnoreErrors(path));
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
Expand All @@ -47,8 +62,13 @@ public void saveDatabase(String path)
// 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();
Expand Down
6 changes: 3 additions & 3 deletions src/bdf/types/BdfObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,14 @@ public BdfObject setInteger(int value) {

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;
}

Expand Down
1 change: 0 additions & 1 deletion src/tests/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;

import bdf.data.BdfDatabase;
import bdf.file.BdfCompressedFileManager;
import bdf.file.BdfFileManager;
import bdf.types.BdfArray;
import bdf.types.BdfIndent;
Expand Down

0 comments on commit 984c780

Please sign in to comment.