package j; 

import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class RW0  
{

 public static RecordStore S0000=null;
 public static int Length0000=366;
public static String REC_STORE_NAME_0="My_RC_0";
public static RecordEnumeration re=null;
// public static int i=0;

 public static void Init(){
 
S0000=null;
Length0000=366;
REC_STORE_NAME_0="My_RC_9999";
re=null;
 
 
 }
 class DataStorage {
 
    private RecordStore recordStore;
 
    public DataStorage(String storeName) {
        openRecordStore(storeName);
    }
 
    private void openRecordStore(String storeName) {
        try {
            recordStore = RecordStore.openRecordStore(storeName, true);
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public void closeRecordStore() {
        try {
            if (recordStore != null) {
                recordStore.closeRecordStore();
            }
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public void addData(String data) {
        try {
            byte[] byteData = data.getBytes();
            recordStore.addRecord(byteData, 0, byteData.length);
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public String[] getAllData() {
        try {
            RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
            int numRecords = enumeration.numRecords();
            String[] data = new String[numRecords];
            int index = 0;
            while (enumeration.hasNextElement()) {
                int recordId = enumeration.nextRecordId();
                byte[] record = recordStore.getRecord(recordId);
                data[index++] = new String(record);
            }
            enumeration.destroy();
            return data;
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public void deleteAllData() {
        try {
            RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
            while (enumeration.hasNextElement()) {
                int recordId = enumeration.nextRecordId();
                recordStore.deleteRecord(recordId);
            }
            enumeration.destroy();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
}
  public static void Open_Or_Create_Storage() throws RecordStoreNotOpenException, RecordStoreException
  {
    
   //   S0000 = RecordStore.openRecordStore(REC_STORE_NAME_0, true);
   

if(RecordStore.listRecordStores() != null)
            {S0000 = RecordStore.openRecordStore(REC_STORE_NAME_0, false);}  

else {       S0000 = RecordStore.openRecordStore(REC_STORE_NAME_0, true); 
             for(int i=1;i<=Length0000;i++){
             if(S0000.getNumRecords()<=0){
             appendRecord((""));}
   }
}  
  

    
    
   
    
    //S0000.closeRecordStore();
    printing("recordstore has created/or/opened : " + REC_STORE_NAME_0,true);
    
  }
 
 /*
public static void openRecordsStore_0()
  {
    try
    {
      
      // The second parameter indicates that the record store
      // should be created if it does not exist
      S0000 = RecordStore.openRecordStore(REC_STORE_NAME_0, false );
      
    //S0000.closeRecordStore();
      
      System.out.println("RecordStore: "+REC_STORE_NAME_0 +" is opened");
    }
    catch (Exception e)
    {
       printing(e.toString(),true);
    }
  }    
  */

public static void FillRecordStrore_0(){

    
    
}


  public static void closeRecStore_0()
  {
    try
    {
      S0000.closeRecordStore();
      System.out.println("RecordStore: "+REC_STORE_NAME_0 +" is closed");
    }
    catch (Exception e)
    {
       printing(e.toString(),true);
    }
  }
  public static void deleteRecStore()
  {
    if (RecordStore.listRecordStores() != null)
    {
      try
      {
        RecordStore.deleteRecordStore(REC_STORE_NAME_0);
        System.out.println("RecordStore: "+REC_STORE_NAME_0 +" is deleted");
      }
      catch (Exception e)
      {
         printing(e.toString(),true);
      }
    }      
  }

  public static void appendRecord(String str)
  {
    byte[] rec = str.getBytes();

    try
    {
      S0000.addRecord(rec, 0, rec.length);
    }
    catch (Exception e)
    {
       printing(e.toString(),Midlet0.F0);
    }
  }
  
public static void setRecord(int i, String str)
{
 try  {
     
byte [] rec=null;
 rec= str.getBytes(); 
           
      S0000.setRecord(i, rec, 0, rec.length);
   rec=null;}
    catch (Exception e ){
 System.out.println("Set Record was: " +i+ str   );
    }
  }
  

  public static void readRecords(boolean Flag0)
  {
    try
    {
      // Intentionally make this too small to test code below
      byte[] recData = new byte[5]; 
      int len;

      for (int i = 1; i <= S0000.getNumRecords(); i++)      
      {
        if (S0000.getRecordSize(i) > recData.length)
          recData = new byte[S0000.getRecordSize(i)];
        
        len = S0000.getRecord(i, recData, 0);
        if(Flag0==true){System.out.println("Record [" + i + "] : " + new String(recData, 0, len));}
        
      }
    }
    catch (Exception e)
    {
      printing(e.toString(),true);
    }
  }

  
  
public static void ReadHot() throws RecordStoreException{
int i=0;
re = S0000.enumerateRecords(null, null, false);
while (re.hasNextElement()){
i++;
String s = new String(re.nextRecord());
//j.Midlet0.TTT[i-1]=s;
System.out.println(" Record [" +i+ "] is"+ s);
}
}
  
  
  
  
  
  /*
  
  
  
  
  public static String getRecord(int i)  {
      int ii=0;
      String Out="";
    try
    {
      // Intentionally make this too small to test code below
      byte[] recData = new byte[5]; 
      int len;
      if (i<=S0000.getNumRecords()){
      
        if (S0000.getRecordSize(i) > recData.length)
          recData = new byte[S0000.getRecordSize(i)];
        
        len = S0000.getRecord(i, recData, 0);
       // System.out.println("got Record at #" + i + ": " + new String(recData, 0, len));
        Out = new String(recData, 0, len);
        ii=i;
        //System.out.println("i"+i"------------------------------"); 
       System.out.println("Record Got: "+ "["+ ii+ "]"+ Out);}
      }
   
    
    catch (Exception e)
    {
      printing(e.toString(),true);
    }
    return Out;
  }
 */ 
  static void printing(String str, boolean F0){if(F0 == true){ System.err.println("Msg: " + str);}}
  static void printing(){System.err.println("Msg: " + "NoMassages");}

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        
    }

    protected void pauseApp() {
        
    }

    protected void startApp() throws MIDletStateChangeException {
        
    }

    protected void paint(Graphics g) {
        
    }
      
}