index.java

来自「J2ME 从入门到精通(程序开发使用案例) 书中代码」· Java 代码 · 共 103 行

JAVA
103
字号
/*
 * Created on 2004-7-11
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.north.phonebook.model;

import java.io.*;

/**
 * @author P2800
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Index
{
    private String key;
    private int recordID;

    public Index(String key, int recordID)
    {
        this.key = key;
        this.recordID = recordID;
    }

    private Index()
    {
    }

    /**
     * @return Returns the key.
     */
    public String getKey()
    {
        return key;
    }
    /**
     * @param key The key to set.
     */
    public void setKey(String key)
    {
        this.key = key;
    }
    /**
     * @return Returns the recordID.
     */
    public int getRecordID()
    {
        return recordID;
    }
    /**
     * @param recordID The recordID to set.
     */
    public void setRecordID(int recordID)
    {
        this.recordID = recordID;
    }
    public byte[] serialize() throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        dos.writeUTF(key);
        dos.writeInt(recordID);
        baos.close();
        dos.close();

        return baos.toByteArray();
    }

    public static Index deserialize(byte[] data) throws IOException
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);

        Index index = new Index();

        index.key = dis.readUTF();
        index.recordID = dis.readInt();
	bais.close();
	dis.close();
        return index;
    }

    public static boolean matches(byte[] data, String key,int type) throws IOException
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        
        if(type == 101)
        {
            return (dis.readUTF()).equals(key);
        }
        else if(type == 102)
        {
            return (dis.readUTF()).startsWith(key);
        }
        return false;
    }

}

⌨️ 快捷键说明

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