Modified the human readable serialization to use streams instead of
strings, to make human readable formats able to handle an output of greater than 2GB.
This commit is contained in:
parent
39de6a5df4
commit
23a49277c1
|
@ -1,5 +1,7 @@
|
|||
package bdf.types;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
@ -66,39 +68,39 @@ public class BdfArray implements IBdfType, Iterable<BdfObject>
|
|||
}
|
||||
|
||||
@Override
|
||||
public String serializeHumanReadable(BdfIndent indent, int it)
|
||||
public void serializeHumanReadable(OutputStream stream, BdfIndent indent, int it) throws IOException
|
||||
{
|
||||
if(elements.size() == 0) {
|
||||
return "[]";
|
||||
stream.write("[]".getBytes());
|
||||
return;
|
||||
}
|
||||
|
||||
String data = "[";
|
||||
stream.write('[');
|
||||
|
||||
for(int i=0;i<elements.size();i++)
|
||||
{
|
||||
BdfObject o = elements.get(i);
|
||||
|
||||
data += indent.breaker;
|
||||
stream.write(indent.breaker.getBytes());
|
||||
|
||||
for(int n=0;n<=it;n++) {
|
||||
data += indent.indent;
|
||||
stream.write(indent.indent.getBytes());
|
||||
}
|
||||
|
||||
data += o.serializeHumanReadable(indent, it + 1);
|
||||
o.serializeHumanReadable(stream, indent, it + 1);
|
||||
|
||||
if(elements.size() > i+1)
|
||||
{
|
||||
data += ", ";
|
||||
if(elements.size() > i+1) {
|
||||
stream.write(", ".getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
data += indent.breaker;
|
||||
stream.write(indent.breaker.getBytes());
|
||||
|
||||
for(int n=0;n<it;n++) {
|
||||
data += indent.indent;
|
||||
stream.write(indent.indent.getBytes());
|
||||
}
|
||||
|
||||
return data + "]";
|
||||
stream.write(']');
|
||||
}
|
||||
|
||||
public BdfArray add(BdfObject o)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package bdf.types;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -93,41 +95,40 @@ public class BdfNamedList implements IBdfType
|
|||
}
|
||||
|
||||
@Override
|
||||
public String serializeHumanReadable(BdfIndent indent, int it)
|
||||
public void serializeHumanReadable(OutputStream stream, BdfIndent indent, int it) throws IOException
|
||||
{
|
||||
if(elements.size() == 0) {
|
||||
return "{}";
|
||||
stream.write("{}".getBytes());
|
||||
return;
|
||||
}
|
||||
|
||||
String data = "{";
|
||||
|
||||
stream.write('{');
|
||||
|
||||
for(int i=0;i<elements.size();i++)
|
||||
{
|
||||
Element e = elements.get(i);
|
||||
|
||||
data += indent.breaker;
|
||||
stream.write(indent.breaker.getBytes());
|
||||
|
||||
for(int n=0;n<=it;n++) {
|
||||
data += indent.indent;
|
||||
stream.write(indent.indent.getBytes());
|
||||
}
|
||||
|
||||
data += DataHelpers.serializeString(new String(e.key, StandardCharsets.UTF_8));
|
||||
data += ": ";
|
||||
data += e.object.serializeHumanReadable(indent, it + 1);
|
||||
stream.write((DataHelpers.serializeString(new String(e.key, StandardCharsets.UTF_8)) + ": ").getBytes());
|
||||
e.object.serializeHumanReadable(stream, indent, it + 1);
|
||||
|
||||
if(elements.size() > i+1)
|
||||
{
|
||||
data += ", ";
|
||||
if(elements.size() > i+1) {
|
||||
stream.write(", ".getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
data += indent.breaker;
|
||||
stream.write(indent.breaker.getBytes());
|
||||
|
||||
for(int n=0;n<it;n++) {
|
||||
data += indent.indent;
|
||||
stream.write(indent.indent.getBytes());
|
||||
}
|
||||
|
||||
return data + "}";
|
||||
stream.write('}');
|
||||
}
|
||||
|
||||
public BdfObject get(String key)
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package bdf.types;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import bdf.data.BdfDatabase;
|
||||
|
@ -113,100 +116,168 @@ public class BdfObject implements IBdfType
|
|||
return t;
|
||||
}
|
||||
|
||||
public String serializeHumanReadable(BdfIndent indent) {
|
||||
return serializeHumanReadable(indent, 0);
|
||||
}
|
||||
|
||||
public String serializeHumanReadable() {
|
||||
return serializeHumanReadable(new BdfIndent("", ""), 0);
|
||||
}
|
||||
|
||||
public String serializeHumanReadable(BdfIndent indent, int it)
|
||||
{
|
||||
if(type == BdfTypes.BOOLEAN) {
|
||||
if(this.getBoolean()) return "true";
|
||||
else return "false";
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
|
||||
try {
|
||||
serializeHumanReadable(stream, indent, it);
|
||||
return stream.toString();
|
||||
}
|
||||
|
||||
// Objects
|
||||
if(type == BdfTypes.ARRAY) return ((IBdfType)object).serializeHumanReadable(indent, it);
|
||||
if(type == BdfTypes.NAMED_LIST) return ((IBdfType)object).serializeHumanReadable(indent, it);
|
||||
if(type == BdfTypes.STRING) return DataHelpers.serializeString((String)object);
|
||||
catch(IOException e) {
|
||||
return "undefined";
|
||||
}
|
||||
}
|
||||
|
||||
public void serializeHumanReadable(OutputStream stream, BdfIndent indent) throws IOException {
|
||||
serializeHumanReadable(stream, indent, 0);
|
||||
}
|
||||
|
||||
public void serializeHumanReadable(OutputStream stream) throws IOException {
|
||||
serializeHumanReadable(stream, new BdfIndent("", ""), 0);
|
||||
}
|
||||
|
||||
public void serializeHumanReadable(OutputStream stream, BdfIndent indent, int it) throws IOException
|
||||
{
|
||||
String str = null;
|
||||
|
||||
// Primitives
|
||||
if(type == BdfTypes.BYTE) return (Byte.toString(this.getByte())+"B");
|
||||
if(type == BdfTypes.DOUBLE) return (Double.toString(this.getDouble())+"D");
|
||||
if(type == BdfTypes.FLOAT) return (Float.toString(this.getFloat())+"F");
|
||||
if(type == BdfTypes.INTEGER) return (Integer.toString(this.getInteger())+"I");
|
||||
if(type == BdfTypes.LONG) return (Long.toString(this.getLong())+"L");
|
||||
if(type == BdfTypes.SHORT) return (Short.toString(this.getShort())+"S");
|
||||
if(type == BdfTypes.BOOLEAN) return this.getBoolean() ? "true" : "false";
|
||||
|
||||
// Arrays
|
||||
if(type == BdfTypes.ARRAY_INTEGER) {
|
||||
String str = "(" + calcIndent(indent, it);
|
||||
for(int i : this.getIntegerArray()) {
|
||||
str += indent.breaker + calcIndent(indent, it) + Integer.toString(i) + "I, ";
|
||||
switch(type)
|
||||
{
|
||||
case BdfTypes.ARRAY:
|
||||
((IBdfType)object).serializeHumanReadable(stream, indent, it);
|
||||
return;
|
||||
|
||||
case BdfTypes.NAMED_LIST:
|
||||
((IBdfType)object).serializeHumanReadable(stream, indent, it);
|
||||
return;
|
||||
|
||||
case BdfTypes.STRING:
|
||||
str = DataHelpers.serializeString((String)object);
|
||||
break;
|
||||
|
||||
case BdfTypes.BOOLEAN:
|
||||
if(this.getBoolean()) str = "true";
|
||||
else str = "false";
|
||||
break;
|
||||
|
||||
case BdfTypes.BYTE:
|
||||
str = Byte.toString(this.getByte())+"B";
|
||||
break;
|
||||
|
||||
case BdfTypes.INTEGER:
|
||||
str = Integer.toString(this.getInteger())+"I";
|
||||
break;
|
||||
|
||||
case BdfTypes.SHORT:
|
||||
str = Short.toString(this.getShort())+"S";
|
||||
break;
|
||||
|
||||
case BdfTypes.LONG:
|
||||
str = Long.toString(this.getLong())+"L";
|
||||
break;
|
||||
|
||||
case BdfTypes.DOUBLE:
|
||||
str = Double.toString(this.getDouble())+"D";
|
||||
break;
|
||||
|
||||
case BdfTypes.FLOAT:
|
||||
str = Float.toString(this.getFloat())+"F";
|
||||
break;
|
||||
|
||||
case BdfTypes.ARRAY_INTEGER: {
|
||||
stream.write(("(" + calcIndent(indent, it)).getBytes());
|
||||
int[] array = this.getIntegerArray();
|
||||
for(int i=0;i<array.length;i++) {
|
||||
stream.write((indent.breaker + calcIndent(indent, it) + Integer.toString(array[i]) + "I").getBytes());
|
||||
if(i == array.length - 1) stream.write(", ".getBytes());
|
||||
}
|
||||
str = str.substring(0, str.length() - 2);
|
||||
str += indent.breaker + calcIndent(indent, it - 1) + ")";
|
||||
return str;
|
||||
stream.write((indent.breaker + calcIndent(indent, it - 1) + ")").getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
if(type == BdfTypes.ARRAY_BOOLEAN) {
|
||||
String str = "(";
|
||||
for(boolean i : this.getBooleanArray()) {
|
||||
str += indent.breaker + calcIndent(indent, it) + (i ? "true" : "false") + ", ";
|
||||
|
||||
case BdfTypes.ARRAY_BOOLEAN: {
|
||||
stream.write(("(" + calcIndent(indent, it)).getBytes());
|
||||
boolean[] array = this.getBooleanArray();
|
||||
for(int i=0;i<array.length;i++) {
|
||||
stream.write((indent.breaker + calcIndent(indent, it) + (array[i] ? "true" : "false")).getBytes());
|
||||
if(i == array.length - 1) stream.write(", ".getBytes());
|
||||
}
|
||||
str = str.substring(0, str.length() - 2);
|
||||
str += indent.breaker + calcIndent(indent, it - 1) + ")";
|
||||
return str;
|
||||
stream.write((indent.breaker + calcIndent(indent, it - 1) + ")").getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
if(type == BdfTypes.ARRAY_BYTE) {
|
||||
String str = "(" + calcIndent(indent, it);
|
||||
for(byte i : this.getByteArray()) {
|
||||
str += indent.breaker + calcIndent(indent, it) + Byte.toString(i) + "B, ";
|
||||
|
||||
case BdfTypes.ARRAY_SHORT: {
|
||||
stream.write(("(" + calcIndent(indent, it)).getBytes());
|
||||
short[] array = this.getShortArray();
|
||||
for(int i=0;i<array.length;i++) {
|
||||
stream.write((indent.breaker + calcIndent(indent, it) + Short.toString(array[i]) + "S").getBytes());
|
||||
if(i == array.length - 1) stream.write(", ".getBytes());
|
||||
}
|
||||
str = str.substring(0, str.length() - 2);
|
||||
str += indent.breaker + calcIndent(indent, it - 1) + ")";
|
||||
return str;
|
||||
stream.write((indent.breaker + calcIndent(indent, it - 1) + ")").getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
if(type == BdfTypes.ARRAY_LONG) {
|
||||
String str = "(" + calcIndent(indent, it);
|
||||
for(long i : this.getLongArray()) {
|
||||
str += indent.breaker + calcIndent(indent, it) + Long.toString(i) + "L, ";
|
||||
|
||||
case BdfTypes.ARRAY_LONG: {
|
||||
stream.write(("(" + calcIndent(indent, it)).getBytes());
|
||||
long[] array = this.getLongArray();
|
||||
for(int i=0;i<array.length;i++) {
|
||||
stream.write((indent.breaker + calcIndent(indent, it) + Long.toString(array[i]) + "L").getBytes());
|
||||
if(i == array.length - 1) stream.write(", ".getBytes());
|
||||
}
|
||||
str = str.substring(0, str.length() - 2);
|
||||
str += indent.breaker + calcIndent(indent, it - 1) + ")";
|
||||
return str;
|
||||
stream.write((indent.breaker + calcIndent(indent, it - 1) + ")").getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
if(type == BdfTypes.ARRAY_SHORT) {
|
||||
String str = "(" + calcIndent(indent, it);
|
||||
for(short i : this.getShortArray()) {
|
||||
str += indent.breaker + calcIndent(indent, it) + Short.toString(i) + "S, ";
|
||||
case BdfTypes.ARRAY_BYTE: {
|
||||
stream.write(("(" + calcIndent(indent, it)).getBytes());
|
||||
byte[] array = this.getByteArray();
|
||||
for(int i=0;i<array.length;i++) {
|
||||
stream.write((indent.breaker + calcIndent(indent, it) + Byte.toString(array[i]) + "B").getBytes());
|
||||
if(i == array.length - 1) stream.write(", ".getBytes());
|
||||
}
|
||||
str = str.substring(0, str.length() - 2);
|
||||
str += indent.breaker + calcIndent(indent, it - 1) + ")";
|
||||
return str;
|
||||
stream.write((indent.breaker + calcIndent(indent, it - 1) + ")").getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
if(type == BdfTypes.ARRAY_DOUBLE) {
|
||||
String str = "(" + calcIndent(indent, it);
|
||||
for(double i : this.getDoubleArray()) {
|
||||
str += indent.breaker + calcIndent(indent, it) + Double.toString(i) + "D, ";
|
||||
case BdfTypes.ARRAY_DOUBLE: {
|
||||
stream.write(("(" + calcIndent(indent, it)).getBytes());
|
||||
double[] array = this.getDoubleArray();
|
||||
for(int i=0;i<array.length;i++) {
|
||||
stream.write((indent.breaker + calcIndent(indent, it) + Double.toString(array[i]) + "D").getBytes());
|
||||
if(i == array.length - 1) stream.write(", ".getBytes());
|
||||
}
|
||||
str = str.substring(0, str.length() - 2);
|
||||
str += indent.breaker + calcIndent(indent, it - 1) + ")";
|
||||
return str;
|
||||
stream.write((indent.breaker + calcIndent(indent, it - 1) + ")").getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
if(type == BdfTypes.ARRAY_FLOAT) {
|
||||
String str = "(" + calcIndent(indent, it);
|
||||
for(float i : this.getFloatArray()) {
|
||||
str += indent.breaker + calcIndent(indent, it) + Float.toString(i) + "F, ";
|
||||
case BdfTypes.ARRAY_FLOAT: {
|
||||
stream.write(("(" + calcIndent(indent, it)).getBytes());
|
||||
float[] array = this.getFloatArray();
|
||||
for(int i=0;i<array.length;i++) {
|
||||
stream.write((indent.breaker + calcIndent(indent, it) + Float.toString(array[i]) + "F").getBytes());
|
||||
if(i == array.length - 1) stream.write(", ".getBytes());
|
||||
}
|
||||
str = str.substring(0, str.length() - 2);
|
||||
str += indent.breaker + calcIndent(indent, it - 1) + ")";
|
||||
return str;
|
||||
stream.write((indent.breaker + calcIndent(indent, it - 1) + ")").getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
str = "undefined";
|
||||
break;
|
||||
}
|
||||
|
||||
// Return null if the object is undefined
|
||||
return "undefined";
|
||||
if(str != null) {
|
||||
stream.write(str.getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
public BdfObject() {
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package bdf.types;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import bdf.data.IBdfDatabase;
|
||||
|
||||
interface IBdfType
|
||||
|
@ -7,13 +10,5 @@ interface IBdfType
|
|||
int serialize(IBdfDatabase database);
|
||||
int serializeSeeker();
|
||||
|
||||
String serializeHumanReadable(BdfIndent indent, int it);
|
||||
|
||||
public default String serializeHumanReadable(BdfIndent indent) {
|
||||
return this.serializeHumanReadable(indent, 0);
|
||||
}
|
||||
|
||||
public default String serializeHumanReadable() {
|
||||
return this.serializeHumanReadable(new BdfIndent("", ""), 0);
|
||||
}
|
||||
void serializeHumanReadable(OutputStream stream, BdfIndent indent, int it) throws IOException;
|
||||
}
|
||||
|
|
|
@ -37,16 +37,16 @@ public class Tests {
|
|||
data.writeToStream(file);
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
BdfObject bdf = new BdfObject();
|
||||
BdfArray a = bdf.getArray();
|
||||
|
||||
byte[] bytes = new byte[1024*1024*1024];
|
||||
byte[] bytes = new byte[1024*1024*1024/2];
|
||||
for(int i=0;i<bytes.length;i++) {
|
||||
bytes[i] = (byte)0;
|
||||
}
|
||||
|
||||
for(int i=0;i<1000;i++) {
|
||||
for(int i=0;i<10;i++) {
|
||||
BdfArray a2 = new BdfArray();
|
||||
a.add(BdfObject.withArray(a2));
|
||||
a = a2;
|
||||
|
@ -58,11 +58,11 @@ public class Tests {
|
|||
|
||||
FileOutputStream file = new FileOutputStream("./database.bdf");
|
||||
data.writeToStream(file);
|
||||
*/
|
||||
|
||||
BdfFileManager bdf = new BdfFileManager("./database.bdf");
|
||||
System.out.println("Loaded bdf");
|
||||
Thread.sleep(5000);
|
||||
|
||||
//BdfFileManager bdf = new BdfFileManager("./database.bdf");
|
||||
//System.out.println("Loaded bdf");
|
||||
//Thread.sleep(5000);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue