/jEdit/tags/jedit-4-2-pre4/bsh/commands/save.bsh
Unknown | 34 lines | 26 code | 8 blank | 0 comment | 0 complexity | 1b7aa6f0bce0654b048e4780e3a7d82e 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
- /**
- Save a serializable Java object to filename.
- */
- bsh.help.save = "usage: save( object, filename )";
- void save( Object obj, String filename )
- {
- File file = pathToFile( filename );
- if ( !(obj instanceof Serializable) ) {
- print("Type "+obj.getClass()+" is not serializable");
- return;
- }
- // Detach bsh objects from the caller's namespace during serialization
- // NOTE: THIS IS NOT THREAD SAFE
- if ( obj instanceof bsh.This ) {
- super.parent = obj.namespace.getParent();
- obj.namespace.prune();
- }
-
- OutputStream out = new FileOutputStream( file );
- ObjectOutputStream oout = new ObjectOutputStream(out);
- oout.writeObject( obj );
- oout.close();
- // Reattach bsh objects to the caller's namespace after serialization
- // NOTE: THIS IS NOT THREAD SAFE
- if ( obj instanceof bsh.This )
- obj.namespace.setParent( super.parent );
- }