All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.tom.protocol.str.StrMsg Maven / Gradle / Ivy

The newest version!
package cn.tom.protocol.str;

import cn.tom.kit.IoBuffer;
import cn.tom.transport.Id;

public class StrMsg implements Id{
	
	public String msg;
	public long id;
	
	public StrMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public Id setMsgId(String id) {
		this.id = Long.parseLong(id);
		return this;
	}

	@Override
	public String getMsgId() {
		if(id == 0) return null;
		return String.valueOf(id);
	}
	
	
	public IoBuffer toBytes(){
		byte [] bs =  msg.getBytes();
		IoBuffer buffer = IoBuffer.allocate(bs.length +4+ 8);
		buffer.writeInt(bs.length + 8 ); //不包括自身4
		buffer.writeLong(id);
		buffer.writeBytes(bs);
		return buffer.flip();
	}

	@Override
	public String toString() {
		return "StrMsg [msg=" + msg + ", id=" + id + "]";
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy