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

 

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