Subversion Repositories SmartDukaan

Rev

Rev 1061 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Utility functions to store and retrieve serialised objects
 * 
 * @author naveen
 *
 */
public class DBUtils {

        /**
         * Serialises object into given file
         * 
         * @param objectToStore Object to store
         * @param dbFile File to use
         * @throws Exception
         */
        public static void store(Object objectToStore, String dbFile) 
                throws Exception {              
                File f = new File(dbFile);
                if(!f.exists()) {
                        f.createNewFile();
                }
                
                ObjectOutputStream out = null;
                try {
                        FileOutputStream fos = new FileOutputStream(f);
                        out = new ObjectOutputStream(fos);
                        
                        out.writeObject(objectToStore);
                }
                finally {
                        if(out != null) {
                                out.close();
                        }
                }
                Utils.info("Serialization complete");
        }
        
        /**
         * 
         * @param text
         * @param dbFile
         * @throws Exception
         */
        public static void store(String text, String filename) 
                throws Exception {              
                File f = new File(filename);
                if(!f.exists()) {
                        f.createNewFile();
                }
                
                ObjectOutputStream out = null;
                try {
                        FileOutputStream fos = new FileOutputStream(f);
                        fos.write(text.getBytes());
                }
                finally {
                        if(out != null) {
                                out.close();
                        }
                }
                Utils.info("Text writing is complete to " + filename );
        }
        /**
         * Retrieves serialised object from given file
         * 
         * @param dbFile
         * @return De-serialised object
         * @throws Exception
         */
        public static Object read(String dbFile) throws Exception {
                
                File f = new File(dbFile);
                if(!f.exists()) {
                        Utils.severe(dbFile + " - does not exist");
                        return null;
                }
                
                Object obj = null;
                ObjectInputStream in = null;
                try {
                        FileInputStream fis = new FileInputStream(f);
                        in = new ObjectInputStream(fis);
                        obj = in.readObject();
                }
                finally {
                        if(in != null) {
                                in.close();
                        }
                }
                
                Utils.info("De-serialization complete");
                return obj;
        }
        
        /**
         * 
         * @param dbFile
         * @throws Exception
         */
        public static boolean delete(String dbFile) throws Exception {
                File f = new File(dbFile);
                if(f.exists()) {
                        return f.delete();
                }
                
                return true;
        }
}