定義數據包頭
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();
}
}