PageRenderTime 179ms CodeModel.GetById 176ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/bsh/commands/save.bsh

#
Unknown | 34 lines | 26 code | 8 blank | 0 comment | 0 complexity | 1b7aa6f0bce0654b048e4780e3a7d82e MD5 | raw file
 1/**
 2	Save a serializable Java object to filename. 
 3*/
 4
 5bsh.help.save = "usage: save( object, filename )";
 6
 7void 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