From 984c78020afd75a29f646d4257513db7d27c09af Mon Sep 17 00:00:00 2001 From: josua Date: Tue, 21 Jul 2020 21:17:51 +1000 Subject: [PATCH] Added the compressed file manager functionality to the BdfFileManager class and fixed issues with boolean. --- src/bdf/file/BdfCompressedFileManager.java | 68 ---------------------- src/bdf/file/BdfFileManager.java | 32 ++++++++-- src/bdf/types/BdfObject.java | 6 +- src/tests/Tests.java | 1 - 4 files changed, 29 insertions(+), 78 deletions(-) delete mode 100644 src/bdf/file/BdfCompressedFileManager.java diff --git a/src/bdf/file/BdfCompressedFileManager.java b/src/bdf/file/BdfCompressedFileManager.java deleted file mode 100644 index 42d2d51..0000000 --- a/src/bdf/file/BdfCompressedFileManager.java +++ /dev/null @@ -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); - } - -} diff --git a/src/bdf/file/BdfFileManager.java b/src/bdf/file/BdfFileManager.java index 008c461..57839bd 100644 --- a/src/bdf/file/BdfFileManager.java +++ b/src/bdf/file/BdfFileManager.java @@ -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,8 +13,9 @@ 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); @@ -21,19 +23,32 @@ public class BdfFileManager extends BdfObject // 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 @@ -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(); diff --git a/src/bdf/types/BdfObject.java b/src/bdf/types/BdfObject.java index a1e79b4..b7a3d5d 100644 --- a/src/bdf/types/BdfObject.java +++ b/src/bdf/types/BdfObject.java @@ -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; } diff --git a/src/tests/Tests.java b/src/tests/Tests.java index 7c782bb..ca7d29b 100755 --- a/src/tests/Tests.java +++ b/src/tests/Tests.java @@ -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;