/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

  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. }