www.pudn.com > WordTip.zip > Word.java


/* 
 * Created on 2005-1-29 
 * 
 * TODO To change the template for this generated file go to 
 * Window - Preferences - Java - Code Style - Code Templates 
 */ 
package net.hyweb.wordmodel; 
import java.util.*; 
import java.io.*; 
 
/** 
 * @author user 
 * 
 * TODO To change the template for this generated type comment go to 
 * Window - Preferences - Java - Code Style - Code Templates 
 */ 
public class Word { 
	 
	private String enWord; 
	private String cnWord; 
	private long   dateTime; 
	private String detail; 
	private Date   date; 
	 
	public Word(){ 
		this.date = new Date(); 
		this.dateTime = date.getTime(); 
	} 
	 
	public Word(String enword, String cnword, String detail){ 
		this.date = new Date(); 
		this.enWord = enword; 
		this.cnWord = cnword; 
		this.dateTime = date.getTime(); 
		this.detail = detail; 
	} 
	 
	/**生成序列化的byte数组数据 
	 * 逻辑数据 -> byte 
	 * @return 
	 * @throws IOException 
	 */ 
	public byte[] serialize() throws IOException{ 
		 
		//Creates a new data output stream to write data  
		//to the specified underlying output stream 
		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		DataOutputStream dos = new DataOutputStream(baos); 
		 
		dos.writeUTF(this.enWord); 
		dos.writeUTF(this.cnWord); 
		dos.writeLong(this.dateTime); 
		dos.writeUTF(this.detail); 
		 
		baos.close(); 
		dos.close(); 
		 
		return baos.toByteArray(); 
	} 
 
	/**将传入的byte类型数据反序列化为已知数据结构 
	 * @param data 
	 * @return 
	 * @throws IOException 
	 */ 
	public static Word deserialize(byte[] data) throws IOException{ 
		ByteArrayInputStream bais = new ByteArrayInputStream(data); 
		DataInputStream dis = new DataInputStream(bais); 
		 
		Word word = new Word(); 
		word.enWord = dis.readUTF(); 
		word.cnWord = dis.readUTF(); 
		word.dateTime = dis.readLong(); 
		word.detail = dis.readUTF(); 
		 
		bais.close(); 
		dis.close(); 
		return word; 
	} 
	 
	public static boolean matchEN(byte[] data, String enword) throws IOException{ 
		ByteArrayInputStream bais = new ByteArrayInputStream(data); 
		DataInputStream dis = new DataInputStream(bais); 
		try{ 
		    return (dis.readUTF().equals(enword)); 
		}catch(IOException e){ 
			e.printStackTrace(); 
			return false; 
		} 
	} 
	 
	public static boolean matchEN_StartWith(byte[] data, String enword) throws IOException{ 
		ByteArrayInputStream bais = new ByteArrayInputStream(data); 
		DataInputStream dis = new DataInputStream(bais); 
		try{ 
		    return (dis.readUTF().startsWith(enword)); 
		}catch(IOException e){ 
			e.printStackTrace(); 
			return false; 
		} 
	} 
	 
	public static boolean matchCN(byte[] data, String cnword) throws IOException{ 
		ByteArrayInputStream bais = new ByteArrayInputStream(data); 
		DataInputStream dis = new DataInputStream(bais); 
		try{ 
			dis.readUTF(); 
		    return (dis.readUTF().equals(cnword)); 
		}catch(IOException e){ 
			e.printStackTrace(); 
			return false; 
		} 
	} 
 
	/** 
	 * @return Returns the cnWord. 
	 */ 
	public String getCnWord() { 
		return cnWord; 
	} 
	/** 
	 * @param cnWord The cnWord to set. 
	 */ 
	public void setCnWord(String cnWord) { 
		this.cnWord = cnWord; 
	} 
	/** 
	 * @return Returns the dateTime. 
	 */ 
	public long getDateTime() { 
		return dateTime; 
	} 
	/** 
	 * @param dateTime The dateTime to set. 
	 */ 
	public void setDateTime(long dateTime) { 
		this.dateTime = dateTime; 
	} 
	/** 
	 * @return Returns the detail. 
	 */ 
	public String getDetail() { 
		return detail; 
	} 
	/** 
	 * @param detail The detail to set. 
	 */ 
	public void setDetail(String detail) { 
		this.detail = detail; 
	} 
	/** 
	 * @return Returns the enWord. 
	 */ 
	public String getEnWord() { 
		return enWord; 
	} 
	/** 
	 * @param enWord The enWord to set. 
	 */ 
	public void setEnWord(String enWord) { 
		this.enWord = enWord; 
	} 
}