java flash tcp字節流通信(一)-java 協議(3)

定義數據包頭

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();
 }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章