Subversion Repositories SmartDukaan

Rev

Rev 323 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 naveen 1
/**
2
 * 
3
 */
64 naveen 4
package in.shop2020.util;
17 naveen 5
 
18 naveen 6
 
17 naveen 7
import java.io.File;
8
import java.io.FileInputStream;
9
import java.io.FileOutputStream;
10
import java.io.ObjectInputStream;
11
import java.io.ObjectOutputStream;
12
 
13
/**
49 naveen 14
 * Utility functions to store and retrieve serialised objects
15
 * 
17 naveen 16
 * @author naveen
17
 *
18
 */
19
public class DBUtils {
18 naveen 20
 
17 naveen 21
	/**
49 naveen 22
	 * Serialises object into given file
17 naveen 23
	 * 
49 naveen 24
	 * @param objectToStore Object to store
25
	 * @param dbFile File to use
18 naveen 26
	 * @throws Exception
17 naveen 27
	 */
18 naveen 28
	public static void store(Object objectToStore, String dbFile) 
29
		throws Exception {		
21 naveen 30
		File f = new File(dbFile);
31
		if(!f.exists()) {
32
			f.createNewFile();
33
		}
34
 
49 naveen 35
		ObjectOutputStream out = null;
36
		try {
37
			FileOutputStream fos = new FileOutputStream(f);
38
			out = new ObjectOutputStream(fos);
39
 
40
			out.writeObject(objectToStore);
41
		}
42
		finally {
43
			if(out != null) {
44
				out.close();
45
			}
46
		}
41 naveen 47
		Utils.info("Serialization complete");
17 naveen 48
	}
49
 
50
	/**
64 naveen 51
	 * 
52
	 * @param text
53
	 * @param dbFile
54
	 * @throws Exception
55
	 */
56
	public static void store(String text, String filename) 
57
		throws Exception {		
58
		File f = new File(filename);
59
		if(!f.exists()) {
60
			f.createNewFile();
61
		}
62
 
63
		ObjectOutputStream out = null;
64
		try {
65
			FileOutputStream fos = new FileOutputStream(f);
66
			fos.write(text.getBytes());
67
		}
68
		finally {
69
			if(out != null) {
70
				out.close();
71
			}
72
		}
73
		Utils.info("Text writing is complete");
74
	}
75
	/**
49 naveen 76
	 * Retrieves serialised object from given file
17 naveen 77
	 * 
18 naveen 78
	 * @param dbFile
49 naveen 79
	 * @return De-serialised object
18 naveen 80
	 * @throws Exception
17 naveen 81
	 */
82
	public static Object read(String dbFile) throws Exception {
43 naveen 83
 
21 naveen 84
		File f = new File(dbFile);
85
		if(!f.exists()) {
43 naveen 86
			Utils.severe(dbFile + " - does not exist");
21 naveen 87
			return null;
88
		}
89
 
49 naveen 90
		Object obj = null;
91
		ObjectInputStream in = null;
92
		try {
93
			FileInputStream fis = new FileInputStream(f);
94
			in = new ObjectInputStream(fis);
95
			obj = in.readObject();
96
		}
97
		finally {
98
			if(in != null) {
99
				in.close();
100
			}
101
		}
17 naveen 102
 
42 naveen 103
		Utils.info("De-serialization complete");
17 naveen 104
		return obj;
105
	}
246 naveen 106
 
107
	/**
108
	 * 
109
	 * @param dbFile
110
	 * @throws Exception
111
	 */
112
	public static boolean delete(String dbFile) throws Exception {
113
		File f = new File(dbFile);
114
		if(f.exists()) {
115
			return f.delete();
116
		}
117
 
118
		return true;
119
	}
17 naveen 120
}