Home > Java > javaTutorial > J2ME learning - RMS from entry to mastery 2

J2ME learning - RMS from entry to mastery 2

黄舟
Release: 2016-12-19 13:56:04
Original
1061 people have browsed it


 In part one of the series, we focused on the basics of Record Management System. Before introducing how to use the Record Management System, I would like to first introduce java IO and the implementation of serialization on the J2ME platform. The above content is very important for both the Record Management System and the general networking framework in MIDP.
 
 Java defined in CLDC IO is very short and concise, but it also provides enough classes to complete our IO operations. Since the implementation is common to J2SE, you can use J2ME to communicate with J2SE or J2EE platforms. For example, communicating with servlets through the Internet. inRecord Management The main classes we use in System are ByteArrayInputStream, ByteArrayOutputStream, DataInputStream and DataOutputStream. The first two are byte-based. ByteArrayInputStream is used to convert byte arrays into streams, while ByteArrayOutputStream is used to convert data in the memory buffer into bytes. The latter two classes are based on Java basic data types and String operations. Usually they pass the first two classes as parameters to the constructor so that they can read and write basic data types and String. One thing worth noting is that the toByteArray() method of ByteArrayOutputStream copies and returns the data in the memory. This wastes an extra memory. In order to use the limited storage space more effectively, you can extend the ByteArrayOutputSteam class and provide the getByteArray() method. , the following is an example:
  
 public class MyByteArrayOutputStream extends ByteArrayOutputStream
 
 {
 
 public byte[] getByteArray()
 
 {
 
  return buf;
 
  }
 
 }
 
  There is no object serialization provided in J2ME mechanism, but we can implement it ourselves. Consider the following class: public class Bank ing aBankName,String aPhone , int aEmployeeNum)
 
 {
 
  this.bankName = aBankName;
 
 this.phone = aPhone;
 
 this.employeeNum = aEmployeeNum;
 
 }
 
 public String getBankName()
 
 {
 
  return bankName != null?bankName:"";
 
  }
 
 public String getPhone()
 
 {
 
  return phone!=null?phone:"";
 
 }
 
 public int getEmployeeNum()
 
 {
 
  return employeeNum ;
 
 }
 
 }
 
  We add two methods to this class to implement object serialization. As shown below:
  
 public class Bank
  
 {
  
  private String phone; BankName,String aPhone,int aEmployeeNum)
 
 {
 
  this.bankName = aBankName;
 
 this.phone = aPhone;
 
 this.employeeNum = aEmployeeNum;
 
 }
 
 public String getBankName()

 {
 
  return bankName !=null? bankName:"";
  
  }
  
  public String getPhone()
  
 {
  
  return phone!=null?phone:"";
  
  }
  
  public int getEmployeeNum()
  
 {
 
 return employeeNum;

  }
  
  public byte[] serialize() throws IOException
  
 {
  
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
 DataOutputStream dos = new DataOutputStream(bos);
  
 do s.writeUTF(getBankName());
 
dos.writeUTF(getPhone());
 
 dos.writeInt(getEmployeeNum());
 
 dos.flush();
 
  return bos.toByteArray();
 
 }
 
  public Bank deserialize(byte[] data ) throws IOException
 
 {
 
 ByteArrayInputStream bis = new ByteArrayInputStream(data);
 
 DataInputStream dis = new DataInputStream(bis);
 
 Bank myBank = new Bank();
 
  myBank.bankName = dis. readUTF();
  
  myBank.phone = dis.readUTF();
  
 myBank.employeeNum = dis.readInt();
  
  return myBank;
  
  }
  
  }
 
In this way, we have achieved serialization of objects, and it is very simple to use. The operations of serialization and deserialization are as follows:
 
 Bank aBank = .....;
 
 RecordStore rs = .....;
 
 try
 
 {
 
 byte[] data = aBank .serialize();
 
rs.addRecord(data,0,data.length); catch(RecordStoreException e)

 {
 
  //do something
 
 }
 
  ????????????????????????
 
 byte[] data = .. .......;
 
 Bank aBank = null;
 
 try
 
 {
 
 aBank = Bank.deserialize(data);
 
 }
 
  catch(IOException e)
 
  {

 }

 One thing worth noting is that in the Bank class, our members are all basic data types and String types, and there are no references to other objects. This is the most ideal and simplest situation. In fact, we design serialization in J2ME You should also try to do this when creating classes to avoid unnecessary trouble.

The above is the content of J2ME learning - RMS from entry to mastery. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template