Added support for compression
This commit is contained in:
parent
376e5c5261
commit
a05cbe7375
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
|
|
@ -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<available;i++)
|
||||
{
|
||||
// Add the next byte from the stream to the bytes
|
||||
bytes[i] = (byte) in.read();
|
||||
while((size = in.read(bytes)) != -1) {
|
||||
buffer.write(bytes, 0, size);
|
||||
}
|
||||
|
||||
// Send the bytes collected from the file stream back
|
||||
return bytes;
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
catch (IOException e)
|
||||
|
@ -46,6 +50,21 @@ public class FileHelpers
|
|||
return data;
|
||||
}
|
||||
|
||||
public static byte[] readAllCompressed(String path) throws IOException
|
||||
{
|
||||
// Create the file input stream
|
||||
InflaterInputStream in = new InflaterInputStream(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 {
|
||||
|
@ -56,4 +75,15 @@ public class FileHelpers
|
|||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] readAllCompressedIgnoreErrors(String path)
|
||||
{
|
||||
try {
|
||||
return readAllCompressed(path);
|
||||
}
|
||||
|
||||
catch(IOException | RuntimeException e) {
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package tests;
|
|||
import java.nio.ByteBuffer;
|
||||
|
||||
import bdf.data.BdfDatabase;
|
||||
import bdf.file.BdfCompressedFileManager;
|
||||
import bdf.file.BdfFileManager;
|
||||
import bdf.types.BdfNamedList;
|
||||
import bdf.types.BdfObject;
|
||||
|
||||
|
@ -10,31 +12,15 @@ public class Tests {
|
|||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
BdfNamedList nl = new BdfNamedList();
|
||||
BdfCompressedFileManager bdf = new BdfCompressedFileManager("./db.bdf");
|
||||
|
||||
float[] array = {0.1F, 5.3F, 42.0F};
|
||||
nl.set("array", BdfObject.withFloatArray(array));
|
||||
nl.set("string", BdfObject.withString("Hello, World!"));
|
||||
BdfNamedList nl = bdf.getNamedList();
|
||||
|
||||
System.out.println(BdfObject.withNamedList(nl).serializeHumanReadable());
|
||||
nl.set("it", BdfObject.withInteger(nl.get("it").getInteger() + 1));
|
||||
|
||||
array[1] = 8.9F;
|
||||
|
||||
System.out.println(BdfObject.withNamedList(nl).serializeHumanReadable());
|
||||
nl.set("array", BdfObject.withFloatArray(array));
|
||||
|
||||
System.out.println(BdfObject.withNamedList(nl).serializeHumanReadable());
|
||||
BdfObject bdf = new BdfObject(new BdfDatabase(BdfObject.withNamedList(nl).serialize().getBytes()));
|
||||
nl = bdf.getNamedList();
|
||||
|
||||
byte[] array2 = nl.get("array").getByteArray();
|
||||
|
||||
for(byte i : array2) {
|
||||
System.out.println(i);
|
||||
}
|
||||
|
||||
System.out.println(nl.get("string").getString());
|
||||
System.out.println(bdf.serializeHumanReadable());
|
||||
|
||||
bdf.saveDatabase();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue