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

📄 messengepack.java

📁 JAVA IP Messenge0.1版说明 作者:吴拓 E-mail:setsail_wu@163.com 完成时间:2008年8月18日 开发环境:j2sdk-1_4_2_18-windo
💻 JAVA
字号:

/*作者:吴拓 Email:setsail_wu@163.com
 */

import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.net.*;

public class MessengePack {
	private byte[] packByte;
	private long version, no, command;
	private String user = null, host = null, extra = null;
	InetAddress hostip;
	
	public MessengePack(long versionIn, long noIn, String userIn, String hostIn,
			long commandIn, String extraIn, InetAddress hostiptemp){
		version = versionIn;
		no = noIn;
		user = userIn;
		host = hostIn;
		command = commandIn;
		extra = extraIn;
		hostip = hostiptemp;
	}
	public MessengePack(byte[] buf, InetAddress hostiptemp) {
		packByte = buf;
		hostip = hostiptemp;
		unpack();
	}
	
	public void pack(){
		StringBuffer strbuf = new StringBuffer();
		strbuf.append(Long.toString(version)).append(":");
		strbuf.append(Long.toString(no)).append(":");
		strbuf.append(user).append(":");
		strbuf.append(host).append(":");
		strbuf.append(Long.toString(command)).append(":");
		strbuf.append(extra);
		String tmpstr = new String(strbuf);
		String ls = System.getProperty("line.separator", "\n");
		String cr = "\n";
		tmpstr = tmpstr.replaceAll(ls, cr);
		packByte = tmpstr.getBytes();		
	}
	
	public void unpack(){
		byte[] buf = packByte;
		int j = buf.length - 1;
		if (buf[j] == 0) {
			while (buf[j] == 0)
				j--;
			byte[] tmpbuf = new byte[j + 1];
			System.arraycopy(buf, 0, tmpbuf, 0, tmpbuf.length);
			buf = tmpbuf;
		}
		int i = 0;
		for (i = 0; i < buf.length; i++)
			if (buf[i] == 0)
				break;
		if (i < buf.length) {
			j = buf.length - 1;
			byte[] tmpbuf = new byte[j - i];
			System.arraycopy(buf, i + 1, tmpbuf, 0, j - i);
			/*try {
				group = new String(tmpbuf, "SJIS");
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace();
			}*/
			tmpbuf = new byte[i];
			System.arraycopy(buf, 0, tmpbuf, 0, i);
			buf = tmpbuf;
		}
		String tmpstr;
		//try {
			tmpstr = new String(buf, 0, buf.length);//, "ISO-8859-1");//SJIS
		//} catch (UnsupportedEncodingException ex) {
		//	ex.printStackTrace();
		//	return;
		//}
		String ls = System.getProperty("line.separator", "\n");
		String cr = "\n";
		tmpstr = tmpstr.replaceAll(ls,cr);
		//tmpstr = StringReplacer.replaceString(tmpstr, ls, cr).trim();
		StringTokenizer tokenizer = new StringTokenizer(tmpstr, ":", false);
		try {
			version = Long.parseLong(tokenizer.nextToken());
			no = Long.parseLong(tokenizer.nextToken());
			user = tokenizer.nextToken();
			host = tokenizer.nextToken();
			command = Long.parseLong(tokenizer.nextToken());
		} catch (NoSuchElementException ex) {
			ex.printStackTrace();
			return;
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
			return;
		}
		if (tokenizer.hasMoreTokens())
			extra = tokenizer.nextToken();
		//while (tokenizer.hasMoreTokens())
			//extra = extra + ':' + tokenizer.nextToken();
	}

	public boolean compare(MessengePack argpack) {
		if (user.equals(argpack.getUser())
			&& host.equals(argpack.getHost())
			&& no == argpack.getNo()
			&& command == argpack.getCommand())
			return true;
		return false;
	}
	
	public byte[] getBytes() {
		pack();
		return packByte;
	}

	public void setBytes(byte[] argpack, InetAddress hostiptemp) {
		packByte = argpack;
		hostip = hostiptemp;
		unpack();
	}	

	public void setVersion(long argver) {
		version = argver;
	}

	public long getVersion() {
		return version;
	}

	public long getNo() {
		return no;
	}

	public void setNo(long argno) {
		no = argno;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String arguser) {
		user = arguser;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String arghost) {
		host = arghost;
	}

	public void setCommand(long argcommand) {
		command = argcommand;
	}

	public long getCommand() {
		return command;
	}

	public String getExtra() {
		return extra;
	}

	public void setExtra(String argextra) {
		extra = argextra;
	}
	
	public InetAddress gethostip(){
		return hostip;
	}
	
	public void sethostip(InetAddress hostiptemp){
		hostip = hostiptemp;
	}
}

⌨️ 快捷键说明

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