Subversion Repositories SmartDukaan

Rev

Rev 43 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 43 Rev 49
Line 10... Line 10...
10
import java.io.FileOutputStream;
10
import java.io.FileOutputStream;
11
import java.io.ObjectInputStream;
11
import java.io.ObjectInputStream;
12
import java.io.ObjectOutputStream;
12
import java.io.ObjectOutputStream;
13
 
13
 
14
/**
14
/**
-
 
15
 * Utility functions to store and retrieve serialised objects
-
 
16
 * 
15
 * @author naveen
17
 * @author naveen
16
 *
18
 *
17
 */
19
 */
18
public class DBUtils {
20
public class DBUtils {
19
 
21
 
20
	/**
22
	/**
-
 
23
	 * Serialises object into given file
21
	 * 
24
	 * 
22
	 * @param objectToStore
25
	 * @param objectToStore Object to store
23
	 * @param dbFile
26
	 * @param dbFile File to use
24
	 * @throws Exception
27
	 * @throws Exception
25
	 */
28
	 */
26
	public static void store(Object objectToStore, String dbFile) 
29
	public static void store(Object objectToStore, String dbFile) 
27
		throws Exception {		
30
		throws Exception {		
28
		File f = new File(dbFile);
31
		File f = new File(dbFile);
29
		if(!f.exists()) {
32
		if(!f.exists()) {
30
			f.createNewFile();
33
			f.createNewFile();
31
		}
34
		}
32
		
35
		
-
 
36
		ObjectOutputStream out = null;
-
 
37
		try {
33
		FileOutputStream fos = new FileOutputStream(f);
38
			FileOutputStream fos = new FileOutputStream(f);
34
		ObjectOutputStream out = new ObjectOutputStream(fos);
39
			out = new ObjectOutputStream(fos);
35
		
40
			
36
		out.writeObject(objectToStore);
41
			out.writeObject(objectToStore);
-
 
42
		}
-
 
43
		finally {
-
 
44
			if(out != null) {
37
		out.close();
45
				out.close();
-
 
46
			}
-
 
47
		}
38
		Utils.info("Serialization complete");
48
		Utils.info("Serialization complete");
39
	}
49
	}
40
	
50
	
41
	/**
51
	/**
-
 
52
	 * Retrieves serialised object from given file
42
	 * 
53
	 * 
43
	 * @param dbFile
54
	 * @param dbFile
44
	 * @return
55
	 * @return De-serialised object
45
	 * @throws Exception
56
	 * @throws Exception
46
	 */
57
	 */
47
	public static Object read(String dbFile) throws Exception {
58
	public static Object read(String dbFile) throws Exception {
48
		
59
		
49
		File f = new File(dbFile);
60
		File f = new File(dbFile);
50
		if(!f.exists()) {
61
		if(!f.exists()) {
51
			Utils.severe(dbFile + " - does not exist");
62
			Utils.severe(dbFile + " - does not exist");
52
			return null;
63
			return null;
53
		}
64
		}
54
		
65
		
-
 
66
		Object obj = null;
-
 
67
		ObjectInputStream in = null;
-
 
68
		try {
55
		FileInputStream fis = new FileInputStream(f);
69
			FileInputStream fis = new FileInputStream(f);
56
		ObjectInputStream in = new ObjectInputStream(fis);
70
			in = new ObjectInputStream(fis);
57
		Object obj = in.readObject();
71
			obj = in.readObject();
-
 
72
		}
-
 
73
		finally {
-
 
74
			if(in != null) {
58
		in.close();
75
				in.close();
-
 
76
			}
-
 
77
		}
59
		
78
		
60
		Utils.info("De-serialization complete");
79
		Utils.info("De-serialization complete");
61
		return obj;
80
		return obj;
62
	}
81
	}
63
}
82
}