⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operaterms.java

📁 手机上一个坦克游戏
💻 JAVA
字号:
package demo;

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

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class OperateRMS {
    private String dbname;
    RecordStore rs;

    public OperateRMS(String name) {
        this.dbname = name;
    }

    public int recordNum() { //读取记录个数
        int n = 0;
        try {
            n = rs.getNumRecords();
        }
        catch (Exception e) {}
        return n;
    }

    public RecordStore openRSAnyway() {
        //打开rms
        rs = null; //名稱大於32 個字元就不接受
        if (dbname.length() > 32) {
            return null;
        }
        try {
            rs = RecordStore.openRecordStore(dbname, true);
            return rs;
        }
        catch (Exception e) {
            return null;
        }
    }

    public boolean deleteRS() { //删除rms
        if (dbname.length() > 32) {
            return false;
        }
        try {
            RecordStore.deleteRecordStore(dbname);
        }
        catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean closeRSAnyway() { //关闭rms
        rs = null; //名稱大於32 個字元就不接受
        if (dbname.length() > 32) {
            return false;
        }
        try {
            rs.closeRecordStore();
        }
        catch (Exception e) {}
        return true;
    }

    public void addMap(short[] mapInfo, int id, int map_wid, int map_hei,
                       String map_name, byte isNew) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        byte[] tmp = null;
        try {
            //System.out.println("add map:");
            dos.writeInt(id); // map id
            dos.writeInt(map_wid); // map width
            dos.writeInt(map_hei); // map height
            dos.writeUTF(map_name);
            dos.writeInt(mapInfo.length); // write length
            dos.writeByte(isNew);
            //System.out.println("mapInfo.length" + mapInfo.length);
            for (int n = 0; n < mapInfo.length; n++) {
                dos.writeShort(mapInfo[n]);
            }
            ////System.out.println("id: " + id + " wid: " + map_wid + " hei: " + map_hei + " name:" + map_name);
            tmp = bos.toByteArray();
            dos.close();
            bos.close();
            //System.out.println("before add record:" + rs.getSize());
            if (rs.getNumRecords() > 0) {
                rs.setRecord(1, tmp, 0, tmp.length);
            }
            else {
                rs.addRecord(tmp, 0, tmp.length);
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public int getMapIndex(int map_id) {
        try {
            for (int i = 1; i <= rs.getNumRecords(); i++) {
                //BattleField.debug("record " + i + " id: " + getMapID(i));
                if (getMapID(i) == map_id) {
                    return i;
                }
            }
        }
        catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public short[] getMap(int index) {
        try {
            ////System.out.println("get map:");
            byte tmp[] = rs.getRecord(index);
            ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
            DataInputStream dis = new DataInputStream(bis);
            int i = dis.readInt();
            i = dis.readInt();
            i = dis.readInt();
            String temp = dis.readUTF();
            int length = dis.readInt();
            short[] mapInfo = new short[length];
            byte isNew = dis.readByte();
            for (int m = 0; m < length; m++) {
                mapInfo[m] = dis.readShort();
            }
            return mapInfo;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public byte getIsNew(int index){
        try {
            byte tmp[] = rs.getRecord(index);
            ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
            DataInputStream dis = new DataInputStream(bis);
            int i = dis.readInt();
            i = dis.readInt();
            i = dis.readInt();
            String temp = dis.readUTF();
            int length = dis.readInt();
            short[] mapInfo = new short[length];
            byte isNew = dis.readByte();
            return isNew;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public int getMapID(int index) {
        ////System.out.println("get Map ID of " + index);
        try {
            byte tmp[] = rs.getRecord(index);
            ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
            DataInputStream dis = new DataInputStream(bis);
            // id wid hei name length content
            int id = dis.readInt();
            ////System.out.println("id = " + id);
            return id;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public int getMapWidth(int index) {
        try {
            byte tmp[] = rs.getRecord(index);
            ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
            DataInputStream dis = new DataInputStream(bis);
            // id wid hei name length content
            int wid = dis.readInt();
            wid = dis.readInt();
            return wid;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public int getMapHeight(int index) {
        try {
            byte tmp[] = rs.getRecord(index);
            ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
            DataInputStream dis = new DataInputStream(bis);
            // id wid hei name length content
            int hei = dis.readInt();
            hei = dis.readInt();
            hei = dis.readInt();
            return hei;

        }
        catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public String getMapName(int index) {
        try {
            byte tmp[] = rs.getRecord(index);
            ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
            DataInputStream dis = new DataInputStream(bis);
            // id wid hei name length content
            int id = dis.readInt();
            id = dis.readInt();
            id = dis.readInt();
            String name = dis.readUTF();
            return name;

        }
        catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public String UIManagerIn(int index) {
        String result = "";
        try {
            //如果是从RMS中取得数据,并且RMS不为空
            byte abyte0[] = rs.getRecord(index);
            ByteArrayInputStream byteArrayInputStream = new
                ByteArrayInputStream(abyte0);
            DataInputStream dataInputStream = new DataInputStream(
                byteArrayInputStream);
            result = dataInputStream.readUTF();
            dataInputStream.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public void UIManagerOut(String data) {
        boolean isFound = false;
        try {
            int num = recordNum();
            for (int i = 0; i < num; i++) {
                if (UIManagerIn(i + 1).equals(data)) {
                    isFound = true;
                    break;
                }
            }
            if (!isFound) {
                ////System.out.println("add user to User's RMS");
                ByteArrayOutputStream byteArrayOutputStream = new
                    ByteArrayOutputStream();
                DataOutputStream dataOutputStream = new DataOutputStream(
                    byteArrayOutputStream);
                dataOutputStream.writeUTF(data);
                byte abyte1[] = byteArrayOutputStream.toByteArray();
                rs.addRecord(abyte1, 0, abyte1.length);
                dataOutputStream.close();
            }

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

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -