import java.io.*; import java.util.Vector; import javax.microedition.io.*; import javax.microedition.io.file.FileConnection; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; import javax.microedition.rms.RecordStore; public class RMSBackup extends MIDlet implements CommandListener { String NameApp = getAppProperty("MIDlet-Name"); Display dsp = Display.getDisplay(this); Form frm = new Form(NameApp); Form frm1 = new Form("RMS Viewer"); List list = new List(NameApp, 2); TextField tf = new TextField("URL:", model().concat(NameApp).concat(".sav"), 50, 0); TextField tf2[]; Command Backup = new Command("Backup", 8, 0); Command Restore = new Command("Restore", 8, 0); Command Exit = new Command("Exit", 7, 1); Command Back = new Command("Back", 3, 1); Command ok = new Command("OK", 8, 1); Command view = new Command("View RMS Selected", 8, 0); Command delete = new Command("Delete", 8, 0); String URL; String ST[]; String ST2[]; boolean bb = false; boolean removed = false; public RMSBackup() { list.addCommand(Backup); list.addCommand(Restore); list.addCommand(view); list.addCommand(delete); list.addCommand(Exit); list.setCommandListener(this); frm.append(tf); frm.addCommand(Back); frm.setCommandListener(this); loadRMS(); dsp.setCurrent(list); } public void startApp() { } public void pauseApp() { } public void destroyApp(boolean flag) { } protected void loadRMS() { if(!removed) { list.removeCommand(Backup); list.removeCommand(view); list.removeCommand(delete); removed = true; } try { ST = RecordStore.listRecordStores(); if(ST == null) return; for(int i = 0; i < ST.length; i++) list.append(ST[i], null); } catch(Exception exception) { } if(removed) { list.addCommand(Backup); list.addCommand(view); list.addCommand(delete); removed = false; } } protected void getSelected() { Vector vector = new Vector(); for(int i = 0; i < ST.length; i++) if(list.isSelected(i)) vector.addElement(ST[i]); ST2 = null; if(vector.isEmpty()) { return; } else { ST2 = new String[vector.size()]; vector.copyInto(ST2); vector.removeAllElements(); vector = null; return; } } protected void store() { try { FileConnection fileconnection = (FileConnection)Connector.open("file:///".concat(URL)); if(fileconnection.exists()) fileconnection.delete(); fileconnection.create(); DataOutputStream dataoutputstream = fileconnection.openDataOutputStream(); dataoutputstream.writeInt(1380799232); dataoutputstream.writeInt(ST2.length); for(int i = 0; i < ST2.length; i++) { RecordStore recordstore = RecordStore.openRecordStore(ST2[i], false); int j = recordstore.getNextRecordID(); dataoutputstream.writeUTF(ST2[i]); dataoutputstream.writeInt(j); for(int k = 1; k < j; k++) try { byte dat[] = new byte[recordstore.getRecordSize(k)]; recordstore.getRecord(k, dat, 0); dataoutputstream.writeInt(k); dataoutputstream.writeChar(43); dataoutputstream.writeInt(dat.length); dataoutputstream.write(dat, 0, dat.length); } catch(Exception exception1) { dataoutputstream.writeInt(k); dataoutputstream.writeChar(45); } recordstore.closeRecordStore(); } dataoutputstream.close(); fileconnection.close(); msg("Saved Successfully"); } catch(Exception exception) { msg(exception.toString()); } } protected void restore() { try { FileConnection fileconnection = (FileConnection)Connector.open("file:///".concat(URL)); if(!fileconnection.exists()) { fileconnection.close(); msg("Backup Does Not Exist!"); return; } DataInputStream datainputstream = fileconnection.openDataInputStream(); if(datainputstream.readInt() != 1380799232) { fileconnection.close(); msg("Invalid backup"); return; } int i = datainputstream.readInt(); for(int j = 0; j < i; j++) { String s = datainputstream.readUTF(); try { RecordStore.deleteRecordStore(s); } catch(Exception exception1) { } RecordStore recordstore = RecordStore.openRecordStore(s, true); int k = datainputstream.readInt(); for(int l = 1; l < k; l++) { datainputstream.readInt(); char c = datainputstream.readChar(); int j1 = recordstore.addRecord(null, 0, 0); if(c == '+') { int k1 = datainputstream.readInt(); byte dat[] = new byte[k1]; datainputstream.read(dat, 0, dat.length); recordstore.setRecord(j1, dat, 0, dat.length); } else { recordstore.deleteRecord(j1); } } recordstore.closeRecordStore(); } datainputstream.close(); fileconnection.close(); msg("Restored successfully!"); } catch(Exception exception) { msg(exception.toString()); } } protected void msg(String s) { Alert alert = new Alert("INFO"); alert.setTimeout(-2); alert.setString(s); dsp.setCurrent(alert, list); } public void commandAction(Command command, Displayable displayable) { if(displayable == frm1) { if(command == ok) { getSelected(); save(); dsp.setCurrent(list); msg("Modified successful!"); } else if(command == Exit) { destroyApp(true); notifyDestroyed(); } } if(displayable == list) { if(command == Backup) { frm.addCommand(Backup); bb = true; dsp.setCurrent(frm); } else if(command == Restore) { frm.addCommand(Restore); bb = false; dsp.setCurrent(frm); } else if(command == view) { getSelected(); if(ST2 == null) msg("None RMS Marked!"); else modify(); } else if(command == Exit) { destroyApp(true); notifyDestroyed(); } else if(command == delete) { getSelected(); if(ST2 == null) msg("None RMS Marked!"); else delete(); } } else if(displayable == frm) if(command == Backup) { URL = tf.getString(); getSelected(); frm.removeCommand(Backup); if(ST2 == null) msg("None RMS Marked!"); else store(); } else if(command == Restore) { URL = tf.getString(); frm.removeCommand(Restore); restore(); list.deleteAll(); loadRMS(); System.gc(); } else if(command == Back) { if(bb) frm.removeCommand(Backup); else frm.removeCommand(Restore); dsp.setCurrent(list); } } protected void delete() { int i = 0; try { while(i < ST2.length) { RecordStore.deleteRecordStore(ST2[i]); i++; } list.deleteAll(); loadRMS(); } catch(Throwable throwable) { } msg("Deleted successfully!"); System.gc(); } protected String model() { String s = System.getProperty("microedition.platform").toLowerCase(); if(s.indexOf("lg") != -1) return "card/"; if(s.indexOf("samsung") != -1) return "memorycard/"; else return "E:/"; } void modify() { frm1.deleteAll(); dsp.setCurrent(frm1); frm1.addCommand(Exit); frm1.setCommandListener(this); try { tf2 = new TextField[ST2.length]; for(int i = 0; i < ST2.length; i++) { RecordStore rs = RecordStore.openRecordStore(ST2[i], true); byte data[] = rs.getRecord(1); String string = ""; for (int j = 0; j < data.length; j++) string = string + data[j] + " "; rs.closeRecordStore(); tf2[i] = new TextField("RMS of " + ST2[i], string, 5000, 0); frm1.append(tf2[i]); } frm1.addCommand(ok); frm1.setCommandListener(this); } catch(Exception exception) { } } void save() { try { int z = 0; while(z < ST2.length) { String s = tf2[z].getString(); int i = 0; for(int j = 0; j < s.length(); j++) if(s.substring(j, j + 1).equals(" ")) i++; byte data[] = new byte[i]; int k = 0; int i1 = 0; for(int j1 = 0; j1 < s.length(); j1++) if(s.substring(j1, j1 + 1).equals(" ")) { data[i1] = (byte)Integer.parseInt(s.substring(k, j1), 10); k = j1 + 1; i1++; } saverms(ST2[z], data); z++; } } catch(Exception exception) {} } public static final void saverms(String str, byte[] Byte) { RecordStore rs; try { RecordStore.deleteRecordStore(str); } catch (Exception RecordStore) { } try { rs = RecordStore.openRecordStore(str, true); rs.addRecord(Byte, 0, Byte.length); rs.closeRecordStore(); } catch (Exception RecordStore) { } } }