PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/commands/save.bsh

#
Unknown | 34 lines | 26 code | 8 blank | 0 comment | 0 complexity | 162ce4530b3b7dcd409759db4b5aab7e MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /**
  2. Save a serializable Java object to filename.
  3. */
  4. bsh.help.save = "usage: save( object, filename )";
  5. void save( Object obj, String filename )
  6. {
  7. File file = pathToFile( filename );
  8. if ( !(obj instanceof Serializable) ) {
  9. print("Type "+obj.getClass()+" is not serializable");
  10. return;
  11. }
  12. // Detach bsh objects from the caller's namespace during serialization
  13. // NOTE: THIS IS NOT THREAD SAFE
  14. if ( obj instanceof org.gjt.sp.jedit.bsh.This ) {
  15. super.parent = obj.namespace.getParent();
  16. obj.namespace.prune();
  17. }
  18. OutputStream out = new FileOutputStream( file );
  19. ObjectOutputStream oout = new ObjectOutputStream(out);
  20. oout.writeObject( obj );
  21. oout.close();
  22. // Reattach bsh objects to the caller's namespace after serialization
  23. // NOTE: THIS IS NOT THREAD SAFE
  24. if ( obj instanceof org.gjt.sp.jedit.bsh.This )
  25. obj.namespace.setParent( super.parent );
  26. }