定义数据包头
package com.net.tcp;
import java.nio.ByteBuffer;
/**
*
* 数据包头
*
*/
public class DataHead {
public final static int HEAD_LEN = 4;
/**数据包升序*/
private int len;
/**包头buffer(默认只要长度4字节,安全需要可往包头里追加相应key)*/
private ByteBuffer content;
public DataHead(){
content = ByteBuffer.allocate(HEAD_LEN);
}
public int getLen() {
return len;
}
public void setLen(int len) {
this.len = len;
this.writeLen();
}
public ByteBuffer getContent() {
return content;
}
private void writeLen(){
this.content.position(0);
this.content.putInt(this.len);
this.content.position(0);
}
public boolean isComplete(){
return !content.hasRemaining();
}
public void init(){
content.rewind();
this.len = content.getInt();
content.rewind();
}
}