home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Shareware / Comunicatii / advwebrank / awr.msi / disk1.cab / bsh_1.2b6.jar / bsh / commands / save.bsh < prev    next >
Encoding:
Text File  |  2002-05-24  |  809 b   |  34 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.     file = pathToFile( filename );
  9.  
  10.     if ( !(obj instanceof Serializable) ) {
  11.         print("Type "+obj.getClass()+" is not serializable");
  12.         return;
  13.     }
  14.  
  15.     // Detach bsh objects from the caller's namespace during serialization
  16.     // NOTE: THIS IS NOT THREAD SAFE
  17.     if ( obj instanceof bsh.This ) {
  18.         super.parent = obj.namespace.getParent();
  19.         obj.namespace.prune();
  20.     }
  21.     
  22.     out = new FileOutputStream( file );
  23.     oout = new ObjectOutputStream(out);
  24.     oout.writeObject( obj );
  25.     oout.close();
  26.  
  27.     // Reattach bsh objects to the caller's namespace after serialization
  28.     // NOTE: THIS IS NOT THREAD SAFE
  29.     if ( obj instanceof bsh.This ) 
  30.         obj.namespace.setParent( super.parent );
  31. }
  32.  
  33.  
  34.