|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright (c) 2009-2021 jMonkeyEngine
|
|
|
|
|
|
+ * Copyright (c) 2009-2025 jMonkeyEngine
|
|
* All rights reserved.
|
|
* All rights reserved.
|
|
*
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* Redistribution and use in source and binary forms, with or without
|
|
@@ -38,7 +38,13 @@ import com.jme3.export.OutputCapsule;
|
|
import com.jme3.export.Savable;
|
|
import com.jme3.export.Savable;
|
|
import com.jme3.export.SavableClassUtil;
|
|
import com.jme3.export.SavableClassUtil;
|
|
import com.jme3.math.FastMath;
|
|
import com.jme3.math.FastMath;
|
|
-import java.io.*;
|
|
|
|
|
|
+
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.OutputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.IdentityHashMap;
|
|
import java.util.IdentityHashMap;
|
|
@@ -116,32 +122,33 @@ import java.util.logging.Logger;
|
|
*
|
|
*
|
|
* @author Joshua Slack
|
|
* @author Joshua Slack
|
|
*/
|
|
*/
|
|
-
|
|
|
|
public class BinaryExporter implements JmeExporter {
|
|
public class BinaryExporter implements JmeExporter {
|
|
- private static final Logger logger = Logger.getLogger(BinaryExporter.class
|
|
|
|
- .getName());
|
|
|
|
|
|
+
|
|
|
|
+ private static final Logger logger = Logger.getLogger(BinaryExporter.class.getName());
|
|
|
|
|
|
protected int aliasCount = 1;
|
|
protected int aliasCount = 1;
|
|
protected int idCount = 1;
|
|
protected int idCount = 1;
|
|
|
|
|
|
- private final IdentityHashMap<Savable, BinaryIdContentPair> contentTable
|
|
|
|
- = new IdentityHashMap<>();
|
|
|
|
-
|
|
|
|
- protected HashMap<Integer, Integer> locationTable
|
|
|
|
- = new HashMap<>();
|
|
|
|
|
|
+ private final IdentityHashMap<Savable, BinaryIdContentPair> contentTable = new IdentityHashMap<>();
|
|
|
|
+ protected HashMap<Integer, Integer> locationTable = new HashMap<>();
|
|
|
|
|
|
// key - class name, value = bco
|
|
// key - class name, value = bco
|
|
- private final HashMap<String, BinaryClassObject> classes
|
|
|
|
- = new HashMap<>();
|
|
|
|
-
|
|
|
|
|
|
+ private final HashMap<String, BinaryClassObject> classes = new HashMap<>();
|
|
private final ArrayList<Savable> contentKeys = new ArrayList<>();
|
|
private final ArrayList<Savable> contentKeys = new ArrayList<>();
|
|
|
|
|
|
public static boolean debug = false;
|
|
public static boolean debug = false;
|
|
- public static boolean useFastBufs = true;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Constructs a new {@code BinaryExporter}.
|
|
|
|
+ */
|
|
public BinaryExporter() {
|
|
public BinaryExporter() {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns a new instance of {@code BinaryExporter}.
|
|
|
|
+ *
|
|
|
|
+ * @return A new {@code BinaryExporter} instance.
|
|
|
|
+ */
|
|
public static BinaryExporter getInstance() {
|
|
public static BinaryExporter getInstance() {
|
|
return new BinaryExporter();
|
|
return new BinaryExporter();
|
|
}
|
|
}
|
|
@@ -163,9 +170,11 @@ public class BinaryExporter implements JmeExporter {
|
|
try {
|
|
try {
|
|
BinaryExporter exporter = new BinaryExporter();
|
|
BinaryExporter exporter = new BinaryExporter();
|
|
exporter.save(object, baos);
|
|
exporter.save(object, baos);
|
|
|
|
+
|
|
BinaryImporter importer = new BinaryImporter();
|
|
BinaryImporter importer = new BinaryImporter();
|
|
importer.setAssetManager(assetManager);
|
|
importer.setAssetManager(assetManager);
|
|
return (T) importer.load(baos.toByteArray());
|
|
return (T) importer.load(baos.toByteArray());
|
|
|
|
+
|
|
} catch (IOException ex) {
|
|
} catch (IOException ex) {
|
|
// Should never happen.
|
|
// Should never happen.
|
|
throw new AssertionError(ex);
|
|
throw new AssertionError(ex);
|
|
@@ -191,9 +200,7 @@ public class BinaryExporter implements JmeExporter {
|
|
// write out tag table
|
|
// write out tag table
|
|
int classTableSize = 0;
|
|
int classTableSize = 0;
|
|
int classNum = classes.keySet().size();
|
|
int classNum = classes.keySet().size();
|
|
- int aliasSize = ((int) FastMath.log(classNum, 256) + 1); // make all
|
|
|
|
- // aliases a
|
|
|
|
- // fixed width
|
|
|
|
|
|
+ int aliasSize = ((int) FastMath.log(classNum, 256) + 1); // make all aliases a fixed width
|
|
|
|
|
|
os.write(ByteUtils.convertToBytes(classNum)); // 3. "number of classes"
|
|
os.write(ByteUtils.convertToBytes(classNum)); // 3. "number of classes"
|
|
for (String key : classes.keySet()) {
|
|
for (String key : classes.keySet()) {
|
|
@@ -212,7 +219,7 @@ public class BinaryExporter implements JmeExporter {
|
|
}
|
|
}
|
|
classTableSize += 1 + bco.classHierarchyVersions.length * 4;
|
|
classTableSize += 1 + bco.classHierarchyVersions.length * 4;
|
|
|
|
|
|
- // write classname size & classname
|
|
|
|
|
|
+ // write class name size & class name
|
|
byte[] classBytes = key.getBytes();
|
|
byte[] classBytes = key.getBytes();
|
|
os.write(ByteUtils.convertToBytes(classBytes.length)); // 5. "full class-name size"
|
|
os.write(ByteUtils.convertToBytes(classBytes.length)); // 5. "full class-name size"
|
|
os.write(classBytes); // 6. "full class name"
|
|
os.write(classBytes); // 6. "full class name"
|
|
@@ -286,17 +293,13 @@ public class BinaryExporter implements JmeExporter {
|
|
// append stream to the output stream
|
|
// append stream to the output stream
|
|
out.writeTo(os);
|
|
out.writeTo(os);
|
|
|
|
|
|
-
|
|
|
|
- out = null;
|
|
|
|
- os = null;
|
|
|
|
-
|
|
|
|
if (debug) {
|
|
if (debug) {
|
|
- logger.fine("Stats:");
|
|
|
|
- logger.log(Level.FINE, "classes: {0}", classNum);
|
|
|
|
- logger.log(Level.FINE, "class table: {0} bytes", classTableSize);
|
|
|
|
- logger.log(Level.FINE, "objects: {0}", numLocations);
|
|
|
|
- logger.log(Level.FINE, "location table: {0} bytes", locationTableSize);
|
|
|
|
- logger.log(Level.FINE, "data: {0} bytes", location);
|
|
|
|
|
|
+ logger.log(Level.INFO, "BinaryExporter Stats:");
|
|
|
|
+ logger.log(Level.INFO, "classes: {0}", classNum);
|
|
|
|
+ logger.log(Level.INFO, "class table: {0} bytes", classTableSize);
|
|
|
|
+ logger.log(Level.INFO, "objects: {0}", numLocations);
|
|
|
|
+ logger.log(Level.INFO, "location table: {0} bytes", locationTableSize);
|
|
|
|
+ logger.log(Level.INFO, "data: {0} bytes", location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -305,14 +308,14 @@ public class BinaryExporter implements JmeExporter {
|
|
.getContent().bytes.length));
|
|
.getContent().bytes.length));
|
|
}
|
|
}
|
|
|
|
|
|
- private int findPrevMatch(BinaryIdContentPair oldPair,
|
|
|
|
- ArrayList<BinaryIdContentPair> bucket) {
|
|
|
|
|
|
+ private int findPrevMatch(BinaryIdContentPair oldPair, ArrayList<BinaryIdContentPair> bucket) {
|
|
if (bucket == null)
|
|
if (bucket == null)
|
|
return -1;
|
|
return -1;
|
|
for (int x = bucket.size(); --x >= 0;) {
|
|
for (int x = bucket.size(); --x >= 0;) {
|
|
BinaryIdContentPair pair = bucket.get(x);
|
|
BinaryIdContentPair pair = bucket.get(x);
|
|
- if (pair.getContent().equals(oldPair.getContent()))
|
|
|
|
|
|
+ if (pair.getContent().equals(oldPair.getContent())) {
|
|
return locationTable.get(pair.getId());
|
|
return locationTable.get(pair.getId());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
@@ -345,7 +348,7 @@ public class BinaryExporter implements JmeExporter {
|
|
return contentTable.get(object).getContent();
|
|
return contentTable.get(object).getContent();
|
|
}
|
|
}
|
|
|
|
|
|
- private BinaryClassObject createClassObject(Class<? extends Savable> clazz) throws IOException{
|
|
|
|
|
|
+ private BinaryClassObject createClassObject(Class<? extends Savable> clazz) throws IOException {
|
|
BinaryClassObject bco = new BinaryClassObject();
|
|
BinaryClassObject bco = new BinaryClassObject();
|
|
bco.alias = generateTag();
|
|
bco.alias = generateTag();
|
|
bco.nameFields = new HashMap<>();
|
|
bco.nameFields = new HashMap<>();
|
|
@@ -361,10 +364,10 @@ public class BinaryExporter implements JmeExporter {
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
Class<? extends Savable> clazz = object.getClass();
|
|
Class<? extends Savable> clazz = object.getClass();
|
|
- BinaryClassObject bco = classes.get(object.getClass().getName());
|
|
|
|
|
|
+ BinaryClassObject bco = classes.get(clazz.getName());
|
|
// is this class been looked at before? in tagTable?
|
|
// is this class been looked at before? in tagTable?
|
|
if (bco == null) {
|
|
if (bco == null) {
|
|
- bco = createClassObject(object.getClass());
|
|
|
|
|
|
+ bco = createClassObject(clazz);
|
|
}
|
|
}
|
|
|
|
|
|
// is object in contentTable?
|
|
// is object in contentTable?
|
|
@@ -379,7 +382,6 @@ public class BinaryExporter implements JmeExporter {
|
|
object.write(this);
|
|
object.write(this);
|
|
newPair.getContent().finish();
|
|
newPair.getContent().finish();
|
|
return newPair.getId();
|
|
return newPair.getId();
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
protected byte[] generateTag() {
|
|
protected byte[] generateTag() {
|
|
@@ -401,4 +403,4 @@ public class BinaryExporter implements JmeExporter {
|
|
new BinaryOutputCapsule(this, bco));
|
|
new BinaryOutputCapsule(this, bco));
|
|
return pair;
|
|
return pair;
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|