home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 25 / FreelogHS25.iso / Dessin / ArtOfIllusion2.2.1 / ArtOfIllusion.jar / bsh / commands / save.bsh < prev    next >
Text File  |  2005-05-23  |  847b  |  35 lines

  1. /**
  2.     Save a serializable Java object to filename. 
  3. */
  4.  
  5. bsh.help.save = "usage: save( object, filename )";
  6.  
  7. void save( Object obj, String filename ) 
  8. {
  9.     File file = pathToFile( filename );
  10.  
  11.     if ( !(obj instanceof Serializable) ) {
  12.         print("Type "+obj.getClass()+" is not serializable");
  13.         return;
  14.     }
  15.  
  16.     // Detach bsh objects from the caller's namespace during serialization
  17.     // NOTE: THIS IS NOT THREAD SAFE
  18.     if ( obj instanceof bsh.This ) {
  19.         super.parent = obj.namespace.getParent();
  20.         obj.namespace.prune();
  21.     }
  22.     
  23.     OutputStream out = new FileOutputStream( file );
  24.     ObjectOutputStream oout = new ObjectOutputStream(out);
  25.     oout.writeObject( obj );
  26.     oout.close();
  27.  
  28.     // Reattach bsh objects to the caller's namespace after serialization
  29.     // NOTE: THIS IS NOT THREAD SAFE
  30.     if ( obj instanceof bsh.This ) 
  31.         obj.namespace.setParent( super.parent );
  32. }
  33.  
  34.  
  35.