/** * Add a persistor. * @param persistor The persistor to add. */ public void add(final EncogPersistor persistor) { this.map.put(persistor.getPersistClassString(), persistor); }
/** * Save the specified object. * @param os The output stream to write to. * @param obj The object to save. */ public static void saveObject(final OutputStream os, final Object obj) { try { final EncogPersistor p = PersistorRegistry.getInstance() .getPersistor(obj.getClass()); if (p == null) { throw new PersistError("Do not know how to persist object: " + obj.getClass().getSimpleName()); } os.flush(); final PrintWriter pw = new PrintWriter(os); final Date now = new Date(); pw.println("encog," + obj.getClass().getSimpleName() + ",java," + Encog.VERSION + "," + p.getFileVersion() + "," + now.getTime()); pw.flush(); p.save(os, obj); } catch (final IOException ex) { throw new PersistError(ex); } }
/** * Load an object from an input stream. * @param is The input stream to read from. * @return The loaded object. */ public static Object loadObject(final InputStream is) { final String header = EncogDirectoryPersistence.readLine(is); final String[] params = header.split(","); if (!"encog".equals(params[0])) { throw new PersistError("Not a valid EG file."); } final String name = params[1]; final EncogPersistor p = PersistorRegistry.getInstance().getPersistor( name); if (p == null) { throw new PersistError("Do not know how to read the object: " + name); } if (p.getFileVersion() < Integer.parseInt(params[4])) { throw new PersistError( "The file you are trying to read is from a later version of Encog. Please upgrade Encog to read this file."); } return p.read(is); }