public void onAction(String name, boolean pressed, float tpf) { if (name.equals("save") && !pressed) { FileOutputStream fos = null; try { long start = System.currentTimeMillis(); fos = new FileOutputStream(new File("terrainsave.jme")); // we just use the exporter and pass in the terrain BinaryExporter.getInstance().save((Savable)terrain, new BufferedOutputStream(fos)); fos.flush(); float duration = (System.currentTimeMillis() - start) / 1000.0f; System.out.println("Save took " + duration + " seconds"); } catch (IOException ex) { Logger.getLogger(TerrainTestReadWrite.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { Logger.getLogger(TerrainTestReadWrite.class.getName()).log(Level.SEVERE, null, e); } } } } };
/** * Saves the object into memory then loads it from memory. * * Used by tests to check if the persistence system is working. * * @param <T> The type of savable. * @param assetManager AssetManager to load assets from. * @param object The object to save and then load. * @return A new instance that has been saved and loaded from the * original object. */ public static <T extends Savable> T saveAndLoad(AssetManager assetManager, T object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { BinaryExporter exporter = new BinaryExporter(); exporter.save(object, baos); BinaryImporter importer = new BinaryImporter(); importer.setAssetManager(assetManager); return (T) importer.load(baos.toByteArray()); } catch (IOException ex) { // Should never happen. throw new AssertionError(ex); } }
public int processBinarySavable(Savable object) throws IOException { if (object == null) { return -1; } Class<? extends Savable> clazz = object.getClass(); BinaryClassObject bco = classes.get(object.getClass().getName()); // is this class been looked at before? in tagTable? if (bco == null) { bco = createClassObject(object.getClass()); } // is object in contentTable? if (contentTable.get(object) != null) { return (contentTable.get(object).getId()); } BinaryIdContentPair newPair = generateIdContentPair(bco); BinaryIdContentPair old = contentTable.put(object, newPair); if (old == null) { contentKeys.add(object); } object.write(this); newPair.getContent().finish(); return newPair.getId(); }
os.write(ByteUtils.convertToBytes(FormatVersion.VERSION)); int id = processBinarySavable(object); byte[] aliasBytes = fixClassAlias(bco.alias, aliasSize); os.write(aliasBytes); BinaryIdContentPair pair = contentTable.get(savable); ArrayList<BinaryIdContentPair> bucket = alreadySaved .get(savableName + getChunk(pair)); int prevLoc = findPrevMatch(pair, bucket); if (prevLoc != -1) { locationTable.put(pair.getId(), prevLoc); if (bucket == null) { bucket = new ArrayList<BinaryIdContentPair>(); alreadySaved.put(savableName + getChunk(pair), bucket); byte[] aliasBytes = fixClassAlias(classes.get(savableName).alias, aliasSize); out.write(aliasBytes); location += aliasSize;
public void save(Savable object, File f) throws IOException { File parentDirectory = f.getParentFile(); if (parentDirectory != null && !parentDirectory.exists()) { parentDirectory.mkdirs(); } FileOutputStream fos = new FileOutputStream(f); try { save(object, fos); } finally { fos.close(); } }
File file = new File(fileName); JmeExporter exporter = BinaryExporter.getInstance(); try { exporter.save(savedNode, file);
public static BinaryExporter getInstance() { return new BinaryExporter(); }
protected void write(Savable object) throws IOException { if (object == null) { write(NULL_OBJECT); return; } int id = exporter.processBinarySavable(object); write(id); }
private BinaryClassObject createClassObject(Class clazz) throws IOException{ BinaryClassObject bco = new BinaryClassObject(); bco.alias = generateTag(); bco.nameFields = new HashMap<String, BinaryClassField>(); bco.classHierarchyVersions = SavableClassUtil.getSavableVersions(clazz); classes.put(clazz.getName(), bco); return bco; }
os.write(ByteUtils.convertToBytes(FormatVersion.VERSION)); int id = processBinarySavable(object); byte[] aliasBytes = fixClassAlias(bco.alias, aliasSize); os.write(aliasBytes); BinaryIdContentPair pair = contentTable.get(savable); ArrayList<BinaryIdContentPair> bucket = alreadySaved .get(savableName + getChunk(pair)); int prevLoc = findPrevMatch(pair, bucket); if (prevLoc != -1) { locationTable.put(pair.getId(), prevLoc); if (bucket == null) { bucket = new ArrayList<BinaryIdContentPair>(); alreadySaved.put(savableName + getChunk(pair), bucket); byte[] aliasBytes = fixClassAlias(classes.get(savableName).alias, aliasSize); out.write(aliasBytes); location += aliasSize;
@Override public void writeObject(ByteBuffer buffer, Object object) throws IOException { Savable s = (Savable) object; BufferOutputStream out = new BufferOutputStream(buffer); exporter.save(s, out); out.close(); }
public static BinaryExporter getInstance() { return new BinaryExporter(); }
protected void write(Savable object) throws IOException { if (object == null) { write(NULL_OBJECT); return; } int id = exporter.processBinarySavable(object); write(id); }
private BinaryClassObject createClassObject(Class clazz) throws IOException{ BinaryClassObject bco = new BinaryClassObject(); bco.alias = generateTag(); bco.nameFields = new HashMap<String, BinaryClassField>(); bco.classHierarchyVersions = SavableClassUtil.getSavableVersions(clazz); classes.put(clazz.getName(), bco); return bco; }
BinaryExporter ex = BinaryExporter.getInstance(); OutputStream os = null; try { ex.save(data, os); Logger.getLogger(SaveGame.class.getName()).log(Level.FINE, "Saving data to: {0}", saveFile.getAbsolutePath()); } catch (IOException ex1) {
@Override public void simpleInitApp() { Spatial ogreModel = assetManager.loadModel("Models/Oto/Oto.mesh.xml"); DirectionalLight dl = new DirectionalLight(); dl.setColor(ColorRGBA.White); dl.setDirection(new Vector3f(0,-1,-1).normalizeLocal()); rootNode.addLight(dl); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryExporter exp = new BinaryExporter(); exp.save(ogreModel, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); BinaryImporter imp = new BinaryImporter(); imp.setAssetManager(assetManager); Node ogreModelReloaded = (Node) imp.load(bais, null, null); AnimComposer composer = ogreModelReloaded.getControl(AnimComposer.class); composer.setCurrentAction("Walk"); rootNode.attachChild(ogreModelReloaded); } catch (IOException ex){ ex.printStackTrace(); } } }
os.write(ByteUtils.convertToBytes(FormatVersion.VERSION)); int id = processBinarySavable(object); byte[] aliasBytes = fixClassAlias(bco.alias, aliasSize); os.write(aliasBytes); BinaryIdContentPair pair = contentTable.get(savable); ArrayList<BinaryIdContentPair> bucket = alreadySaved .get(savableName + getChunk(pair)); int prevLoc = findPrevMatch(pair, bucket); if (prevLoc != -1) { locationTable.put(pair.getId(), prevLoc); if (bucket == null) { bucket = new ArrayList<BinaryIdContentPair>(); alreadySaved.put(savableName + getChunk(pair), bucket); byte[] aliasBytes = fixClassAlias(classes.get(savableName).alias, aliasSize); out.write(aliasBytes); location += aliasSize;
public void save(Savable object, File f) throws IOException { File parentDirectory = f.getParentFile(); if (parentDirectory != null && !parentDirectory.exists()) { parentDirectory.mkdirs(); } FileOutputStream fos = new FileOutputStream(f); try { save(object, fos); } finally { fos.close(); } }
public int processBinarySavable(Savable object) throws IOException { if (object == null) { return -1; } Class<? extends Savable> clazz = object.getClass(); BinaryClassObject bco = classes.get(object.getClass().getName()); // is this class been looked at before? in tagTable? if (bco == null) { bco = createClassObject(object.getClass()); } // is object in contentTable? if (contentTable.get(object) != null) { return (contentTable.get(object).getId()); } BinaryIdContentPair newPair = generateIdContentPair(bco); BinaryIdContentPair old = contentTable.put(object, newPair); if (old == null) { contentKeys.add(object); } object.write(this); newPair.getContent().finish(); return newPair.getId(); }
public static BinaryExporter getInstance() { return new BinaryExporter(); }