View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.datastore;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileNotFoundException;
24  import java.io.FileOutputStream;
25  import java.io.ObjectInputStream;
26  import java.io.ObjectOutputStream;
27  
28  public class SerializedFileSoup extends FileSoup
29  {
30      public SerializedFileSoup( String aPath )
31      {
32          super( aPath );
33      }
34  
35      // file access methods    
36      
37      protected boolean writeFile( String name, Object anObject )
38      {
39      	try
40  	{
41  	    File f = new File( getHomeDirectory(), name );
42  	    ObjectOutputStream oos = new ObjectOutputStream(
43  		new FileOutputStream( f ) );
44  	    oos.writeObject( anObject );
45  	    oos.flush();
46  	    oos.close();
47  	    return true;
48  	} 
49  	catch ( Exception exc )
50  	{
51  	    System.err.println( "SerializedFileSoup.writeFile: " + exc );
52  	}
53  	
54  	return false;
55      }
56      
57      protected Object readFile( String name )
58      {
59      	Object result = null;
60  
61          try
62          {
63              File f = new File( getHomeDirectory(), name );
64              ObjectInputStream ois = new ObjectInputStream(
65              new FileInputStream( f ) );
66              result = ois.readObject();
67              ois.close();
68          }
69  		catch ( FileNotFoundException exc )
70  		{
71  			result = null;
72  		}
73          catch ( Exception exc )
74          {
75              System.err.println( "SerializedFileSoup.readFile: " + exc );
76              exc.printStackTrace();
77          }
78          return result;
79      }
80      
81      protected boolean deleteFile( String name )
82      {
83      	try
84          {
85              File f = new File( getHomeDirectory(), name );
86              if ( f.exists() )
87              {
88                  f.delete();
89  				return true;
90              }
91          }
92          catch ( Exception exc )
93          {
94              System.err.println( "SerializedFileSoup.deleteFile: " + exc );
95          }
96  
97          return false;
98      }
99      
100 }
101 
102 /*
103  * $Log$
104  * Revision 1.2  2006/02/19 16:26:19  cgruber
105  * Move non-unit-test code to tests project
106  * Fix up code to work with proper imports
107  * Fix maven dependencies.
108  *
109  * Revision 1.1  2006/02/16 13:18:56  cgruber
110  * Check in all sources in eclipse-friendly maven-enabled packages.
111  *
112  * Revision 1.1.1.1  2000/12/21 15:47:20  mpowers
113  * Contributing wotonomy.
114  *
115  * Revision 1.2  2000/12/20 16:25:37  michael
116  * Added log to all files.
117  *
118  *
119  */
120