package com.dd.model.protocol;
public class TTL implements IProtocolBase
{
private short tag;
private DataTypeEnum type;
private short len;
public TTL()
{
}
public TTL(short tag, DataTypeEnum type)
{
this.tag = tag;
this.type = type;
this.len = getLenByType(type);
}
private short getLenByType(DataTypeEnum type)
{
short byteWidth = 0;
switch (type)
{
case BOOL:
byteWidth = 1;
break;
case INT8:
byteWidth = 1;
break;
case INT16:
byteWidth = 2;
break;
case INT32:
byteWidth = 4;
break;
case FLOAT:
byteWidth = 4;
break;
default:
break;
}
return byteWidth;
}
public TTL(short tag, DataTypeEnum type, short len)
{
super();
this.tag = tag;
this.type = type;
this.len = len;
}
@Override
public int getTotalLen()
{
return 2+2+2;//tag type len
}
@Override
public byte[] serial()
{
int byteLen = 2+2+2;
byte result[] = new byte[byteLen];
result[0] = (byte)(tag >> 8);
result[1] = (byte)tag ;
result[2] = (byte)(type.ordinal() >> 8);
result[3] = (byte)type.ordinal();
result[4] = (byte)(len >> 8);
result[5] = (byte)len;
return result;
}
public void setLen(short len)
{
this.len = len;
}
@Override
public String toString()
{
return "TTL [tag=" + tag + ", type=" + type + ", len=" + len + "]";
}
public short getTag()
{
return tag;
}
public void setTag(short tag)
{
this.tag = tag;
}
public DataTypeEnum getType()
{
return type;
}
public void setType(DataTypeEnum type)
{
this.type = type;
}
public short getLen()
{
return len;
}
public int getSerialTotalLen()
{
return getTotalLen()*2;
}
}
阅读(1625) | 评论(0) | 转发(0) |