www.pudn.com > phonebk.rar > Index.java


/* 
 * Index.java 
 * 
 * Created on 2006Äê3ÔÂ19ÈÕ, ÉÏÎç11:14 
 * 
 * To change this template, choose Tools | Options and locate the template under 
 * the Source Creation and Management node. Right-click the template and choose 
 * Open. You can then make changes to the template in the Source Editor. 
 */ 
 
package com.j2medev.sample.phonebook; 
 
import java.io.*; 
/** 
 * 
 * @author Admin 
 */ 
public class Index { 
    private String key; 
    private int recordID; 
     
    /** Creates a new instance of Index */ 
    public Index() { 
    } 
    public Index(String key,int recordID){ 
        this.key = key; 
        this.recordID = recordID; 
    } 
     
    public void setKey(String key){ 
        this.key = key; 
    } 
     
    public void setRecorID(int recordID){ 
        this.recordID = recordID; 
    } 
    public String getKey(){ 
        return key; 
    } 
    public int getRecordID(){ 
        return 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(); 
        dis.close(); 
        bais.close(); 
        return index; 
    } 
    public static boolean matches(byte[] data,String key,int flag) throws IOException{ 
        ByteArrayInputStream bais = new ByteArrayInputStream(data); 
        DataInputStream dis = new DataInputStream(bais); 
        if(flag == 0){ 
        return (dis.readUTF()).equals(key); 
        }else if((dis.readUTF()).indexOf(key) == -1){ 
            return false; 
        }else{ 
        return true; 
        } 
        } 
         
    }