定义数据包内容,为使ByteBuffer创建后能够重用,数据包内容用List<ByteBuffer>存储创建的ByteBuffer,每个ByteBuffer都是固定大小
package com.net.tcp;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
/**
*
* 数据内容
*
*/
public class DataContent {
/**数据内容*/
private List<ByteBuffer> content;
/**当前ByteBuffer在List中的位置*/
private int index = 0;
public DataContent(){
content = new ArrayList<ByteBuffer>();
}
public void setContent(List<ByteBuffer> content) {
this.content = content;
this.index = 0;
}
public int remaining(){
int len = 0;
for(ByteBuffer bf : content){
len += bf.remaining();
}
return len;
}
public int position(){
int position = 0;
for(ByteBuffer bf : content){
position += bf.position();
}
return position;
}
public int getSize(){
return content.size();
}
public void flip(){
if(content.size() > 0){
int head = -1;
index = head;
for(ByteBuffer bf : content){
bf.flip();
head++;
if(index == -1 && bf.hasRemaining()){
index = head;
}
}
if(index == -1){
index = 0;
}
}
}
public void rewind(){
if(content.size() > 0){
index = 0;
for(ByteBuffer bf : content){
bf.rewind();
}
}
}
public ByteBuffer getByteBuffer(int index){
return content.get(index);
}
public boolean hasNext(){
if(content.size() > 0)
return index < content.size();
else
return false;
}
public ByteBuffer current(boolean create){
if(content.size() > 0 && content.size() > index)
return content.get(index);
else if(create){
this.createByteBuffer();
if(content.size() <= index){
index = content.size() - 1;
}
return this.current(false);
}
return null;
}
public ByteBuffer next(boolean create){
if(content.size() > 0 && content.size() > index + 1){
return content.get(++index);
} else if(create){
this.createByteBuffer();
if(content.size() <= index){
index = content.size() - 1;
}
}
return null;
}
public byte getByte(){
ByteBuffer buffer = this.current(false);
if(buffer.remaining() > 1)
return buffer.get();
else{
byte[] bytes = getBytes(1);
return (Byte)ByteUtils.getObject(bytes, DataType.TYPE_BYTE);
}
}
public short getShort(){
ByteBuffer buffer = this.current(false);
if(buffer.remaining() > 2)
return buffer.getShort();
else{
byte[] bytes = getBytes(2);
return (Short)ByteUtils.getObject(bytes, DataType.TYPE_SHORT);
}
}
public int getInt(){
ByteBuffer buffer = this.current(false);
if(buffer.remaining() > 4)
return buffer.getInt();
else{
byte[] bytes = getBytes(4);
return (Integer)ByteUtils.getObject(bytes, DataType.TYPE_INT);
}
}
public double getDouble(){
ByteBuffer buffer = this.current(false);
if(buffer.remaining() > 8)
return buffer.getDouble();
else{
byte[] bytes = getBytes(8);
return (Double)ByteUtils.getObject(bytes, DataType.TYPE_DOUBLE);
}
}
public boolean getBoolean(){
byte[] bytes = this.getBytes(1);
return bytes[0] == 1;
}
public String getString(int len){
byte[] bytes = this.getBytes(len);
return (String)ByteUtils.getObject(bytes, DataType.TYPE_STRING);
}
public byte[] getBytes(int size){
byte[] bytes = new byte[size];
ByteBuffer buffer = this.current(false);
int index = 0;
while(index < size){
while(buffer.hasRemaining() && index < size){
bytes[index] = buffer.get();
index++;
}
if(index < size)
buffer = next(false);
}
return bytes;
}
public void putByte(byte value){
ByteBuffer buffer = null;
buffer = this.current(true);
if(buffer.hasRemaining()){
buffer.put(value);
}else{
putBytes(ByteUtils.getBytes(value));
}
}
public void putShort(short value){
ByteBuffer buffer = null;
buffer = this.current(true);
if(buffer.remaining() > 2){
buffer.putShort(value);
}else{
putBytes(ByteUtils.getBytes(value));
}
}
public void putInt(int value){
ByteBuffer buffer = null;
buffer = this.current(true);
if(buffer.remaining() > 4){
buffer.putInt(value);
}else{
putBytes(ByteUtils.getBytes(value));
}
}
public void putDouble(double value){
ByteBuffer buffer = null;
buffer = this.current(true);
if(buffer.remaining() > 8){
buffer.putDouble(value);
}else{
putBytes(ByteUtils.getBytes(value));
}
}
public void putBoolean(boolean value){
byte b = value ? (byte)1 : (byte)0;
this.putByte(b);
}
public void putString(String value){
putBytes(ByteUtils.getBytes(value));
}
public void putBytes(byte[] bytes){
if(bytes.length > 0){
ByteBuffer buffer = null;
int index = 0;
while(index < bytes.length){
buffer = this.current(true);
if(buffer.hasRemaining()){
buffer.put(bytes[index]);
index++;
}else{
buffer = this.next(true);
}
}
}
}
public void put(ByteBuffer buffer){
while(buffer.hasRemaining()){
this.putByte(buffer.get());
}
}
private void createByteBuffer(){
this.content.add(ByteBuffer.allocate(100));
this.index++;
}
}