Tous les collages #755211 Brut Modifier

Untitled

public text v1 · immuable
#755211 ·publié 2007-10-30 15:31 UTC
corps du collage affiché
/*
 * rmsHandler.java
 *
 * Created on October 28, 2007, 1:06 PM
 *
 */
package mmc;

import java.io.*;
import javax.microedition.rms.*;


/**
 *
 * @author metamorfozis
 * 
 * Created by: Kopias Csaba [http://metamorfozis.hu] with the help of the #j2me channel @ irc.freenode.net
 * Licence: GPL
 * Description: This class enables you to store /maxFields/ of key:value pairs
 *              in the Record Management Store.
 * 
 * 
 * Example:
 * *****************************************************************************
 * 
 * private rmsHandler options;
 * 
 * ...
 * 
 * options = new rmsHandler("myAppletsSettings",16);
 * 
 * ...
 * 
 * options.set("foo","bar");
 * System.out.println(options.get("foo")); // this prints bar
 * 
 * ...
 * 
 * options.saveDb(); // if you forget this, the database is not stored.
 * 
 * *****************************************************************************
 * 
 * 
 * Parameters:
 * *****************************************************************************
 *
 * String db : The name of your database
 * int maxFields : The maximum number of key:value pairs that you want to store
 * 
 * *****************************************************************************
 */
public class rmsHandler {

    private RecordStore _rs;
    private String[] _db;
    private boolean we_are_opened = false;

    public rmsHandler(String db, int maxFields) {
        _db = new String[maxFields];
        byte[] data = null;
        int i = 0;
        int size;
        String foo;

        try {
            _rs = RecordStore.openRecordStore(db, true);
            we_are_opened = true;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            we_are_opened = false;
        }
       
        
        try {

            RecordEnumeration re = _rs.enumerateRecords(null, null, true);


            if (_rs.getNumRecords() > 0) {
                /* get the first record */
                data = re.nextRecord();
                if (data != null) {
                    ByteArrayInputStream bin;
                    DataInputStream din;

                    bin = new ByteArrayInputStream(data);
                    din = new DataInputStream(bin);

                    while (din.available() > 0) {

                        _db[i] = din.readUTF();

                        i++;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void saveDb() {
        int recordID;


        if (!we_are_opened) {
            return;
        }

        try {
            RecordEnumeration re = _rs.enumerateRecords(null, null, true);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream(bout);




            for (int i = _db.length; i > 0; i--) {
                if (_db[i - 1] != null) {

                    dout.writeUTF(_db[i - 1]);
                }
            }


            dout.close();
            byte[] data = bout.toByteArray();

            if (re.numRecords() > 0) {

                _rs.setRecord(re.nextRecordId(), data, 0, data.length);
            } else {

                _rs.addRecord(data, 0, data.length);
            }

            _rs.closeRecordStore();
            we_are_opened = false;

        } catch (Exception e) {
            e.printStackTrace();
        }



    }

    public void set(String key, String val) {

        String most;
        boolean weAlreadyHasOne = false;
        int elso = -1;

        if (!we_are_opened) {
            return;
        }

        for (int i = _db.length; i > 0; i--) {
            most = _db[i - 1];

            if (most != null && most.substring(0, most.indexOf(":")).equals(key)) {
                _db[i - 1] = key + ":" + val;
                weAlreadyHasOne = true;
                break;
            }
            if (elso == -1 && most != null) {
                elso = i;
            }
        }

        if (!weAlreadyHasOne) {
            _db[elso + 1] = key + ":" + val;
        }

    }

    public String get(String key) {
        String most;

        if (!we_are_opened) {
            return null;
        }

        for (int i = _db.length; i > 0; i--) {
            most = _db[i - 1];

            if (most != null && most.substring(0, most.indexOf(":")).equals(key)) {
                return most.substring(most.indexOf(":") + 1, most.length());
            }


        }
        return null;


    }
}