Javame RMS Universal Persistence framework

Source: Internet
Author: User
Tags file system reflection serialization

In writing Javame program, we often need to save some data to the phone, but also often want to be able to save objects to the phone inside, but javame there is no reflection mechanism, there is no Java.io.Serializable interface, so there is no serialization mechanism, to save the object, you have to do it yourself.

In Javame, where the program's data is stored, there are two kinds, one is to keep the data in RMS, this is all Javame mobile phones are supported, there is a kind of data stored in the phone's file system, this is not all mobile phones can support, only support JSR075 mobile phones, Support to keep the data in the file system, and if your program is not signed, every time you save or read, the phone will pop up annoying prompts, whether to allow programs to access the file system. Where I usually put the data in RMS, because the read-write RMS is secure and does not require a phone prompt. Because our RMS data is present in a special place. But Javame's RMS is very low-level, and in order to save some data, we have to deal with byte[], so I have the idea of encapsulating a layer of my own program before that, so it's convenient to use when packaged. As long as the implementation of the relevant interface, you can enjoy a more easy-to-use method.

A total of four classes are included in this framework, respectively, as follows:

Serializable class, it is an interface, similar to the javase inside the serializable interface, the only difference is that the interface inside the javase is an empty interface, only for marking, and here's the interface is a method to implement.

Lazy class, it is also an interface, it defines a number of methods, if your object is large, you need to lazy load, you can implement this interface, and this interface is a serializable interface subclass, that is, the implementation of the Lazy interface, You are equivalent to implementing the Serializable interface.

Rmsutil class, this class is a tool class that is used to unify RMS-related operations and is the core class of this framework.

Recordfetcher class, is also an interface, it inherits the Recordcomparator, Recordfilter interface, when fetching data, need to use it.

OK, let's start with the code.

1/*


2 * To change this template, choose Tools | Templates


3 * and open the template in the editor.


4 * *


5 Package com.hadeslee.mobile.rms;


6


7 Import java.io.IOException;


8


9/**


10 * An interface to be implemented by a class that can be serialized by itself


@author Hadeslee


12 * *


public interface Serializable {


14


15/**


16 * Encodes itself into a byte array format


* @return byte array


18 * *


public byte[] Serialize () throws IOException;


20


21/**


22 * Reload an object with this byte array


* @param data byte array


24 * *


public void Unserialize (byte[] data) throws IOException;


26


27/**


28 * Set the ID of the corresponding storage object after serialization of this object


* @param ID ID


30 * *


to public void setId (int id);


32


33/**


34 * Gets the ID
after serialization of this object

35 * This method is only valid if it is invoked on a deserialized object


36 * If an object is not serialized, then its ID is-1;


* @return ID


38 * *


-public int getId ();


40}


41


1/*


2 * To change this template, choose Tools | Templates


3 * and open the template in the editor.


4 * *


5 Package com.hadeslee.mobile.rms;


6


7 Import java.io.IOException;


8


9/**


10 * The interface that the object that can defer loading must be implemented


@author binfeng.li


12 * *


public Interface Lazy extends Serializable {


14


15/**


16 * The method to implement the class that implements this interface


17 * Can be used to defer loading of certain properties. Like
.

("Imgdata"), Get ("Fullimage"). Wait,
.

19 * Because J2ME does not support annotations and does not support reflection, you can only


20 * This method is used to simulate the


21 * This method is rmsutil when the object is to be stored, so that the


22 * Different parts of an object are stored in different RMS


* @param key to get some of the keys


* @return its corresponding value


* @throws IOException


26 * *


public byte[] Getattach (Object key) throws IOException;


28


29/**


30 * When an attached object is saved, the
to be invoked

31 * Method, this method tells the principal that its attachment is saved after


32 * What is the corresponding ID in RMS


* @param key


* @param ID


35 * *


public void Savedattach (Object key, int id);


37


38/**


39 * Gets an array of all the key supported by this object

An array of
@return key, which cannot be null


41 * *


public object[] Getattachkeys ();


43


44/**


45 * The name of the RMS to which this object's subordinate object is stored


* @return RMS name


47 * *


public String getnameofattachrms ();


49}


50

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.