MCPcopy Create free account
hub / github.com/OpenSourcePhysics/osp / saveState

Method saveState

src/org/opensourcephysics/ejs/Simulation.java:537–566  ·  view source on GitHub ↗

Saves the state of the model either to a file on the disk or to memory. If the name of the file starts with the prefix "ejs:", then the state of the model will be saved to memory, otherwise it will be dumped to disk. Security considerations apply when running the simulation as an applet. The sta

(String _filename)

Source from the content-addressed store, hash-verified

535 * @return true if the file was correctly saved
536 */
537 public boolean saveState(String _filename) {
538 if (model == null) {
539 return false;
540 }
541 try {
542 java.io.OutputStream out;
543 if (_filename.startsWith("ejs:")) { //$NON-NLS-1$
544 out = new java.io.ByteArrayOutputStream();
545 } else {
546 out = new java.io.FileOutputStream(_filename);
547 }
548 java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(out);
549 java.io.ObjectOutputStream dout = new java.io.ObjectOutputStream(bout);
550 java.lang.reflect.Field[] fields = model.getClass().getFields();
551 for (int i = 0; i < fields.length; i++) {
552 if (fields[i].get(model) instanceof java.io.Serializable) {
553 dout.writeObject(fields[i].get(model));
554 }
555 }
556 dout.close();
557 if (_filename.startsWith("ejs:")) { //$NON-NLS-1$
558 memory.put(_filename, ((java.io.ByteArrayOutputStream) out).toByteArray());
559 }
560 return true;
561 } catch (java.lang.Exception ioe) {
562 errorMessage("Error when trying to save" + _filename); //$NON-NLS-1$
563 ioe.printStackTrace(System.err);
564 return false;
565 }
566 }
567
568 public boolean readState(String _filename) {
569 return readState(_filename, null);

Callers 1

_saveStateMethod · 0.80

Calls 6

errorMessageMethod · 0.95
writeObjectMethod · 0.80
getMethod · 0.65
closeMethod · 0.65
getClassMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected