diff --git a/src/bdf/file/BdfCompressedFileManager.java b/src/bdf/file/BdfCompressedFileManager.java new file mode 100644 index 0000000..0570ee2 --- /dev/null +++ b/src/bdf/file/BdfCompressedFileManager.java @@ -0,0 +1,68 @@ +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(); + } + + 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 33ac3f5..4084e0d 100644 --- a/src/bdf/file/BdfFileManager.java +++ b/src/bdf/file/BdfFileManager.java @@ -3,6 +3,7 @@ package bdf.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import bdf.data.BdfDatabase; import bdf.types.BdfObject; @@ -44,7 +45,7 @@ public class BdfFileManager extends BdfObject file.getAbsoluteFile().getParentFile().mkdirs(); // Get the database file for output - FileOutputStream out = new FileOutputStream(path); + OutputStream out = new FileOutputStream(path); // Write the database to the file out.write(this.serialize().getBytes()); diff --git a/src/bdf/util/FileHelpers.java b/src/bdf/util/FileHelpers.java index ef2f45b..e5658ba 100644 --- a/src/bdf/util/FileHelpers.java +++ b/src/bdf/util/FileHelpers.java @@ -1,27 +1,31 @@ package bdf.util; +import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.util.zip.InflaterInputStream; +import java.util.zip.ZipException; + +import com.sun.corba.se.impl.ior.ByteBuffer; public class FileHelpers { - public static byte[] readAll(FileInputStream in) + public static byte[] readAll(InputStream in) { try { // Get bytes to return - int available = in.available(); - byte[] bytes = new byte[available]; + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + byte[] bytes = new byte[1024]; + int size = 0; - // Loop over the available bytes - for(int i=0;i