java解析TLV格式数据

TLV:TLV格式数据是指由Tag,Length,Value组成的数据。具体说明如下:

           tag标签的属性为bit,由16进制表示,占1~2个字节长度。例如,“9F33”为一个占用两个字节 的tag标签。而“95”为一个占用一个字节的tag标签。若tag标签的第一个字节(注:字节排序方向为从左往右数,第一个字节即为最左边的字节。bit排序规则同理。)的后四个bit为“1111”,则说明该tag占两个字节,例如“9F33”;否则占一个字节,例“95”。 

           子域长度(即L本身)的属性也为bit,占1~3个字节长度。具体编码规则如下: 
           a)  当 L 字段最左边字节的最左 bit 位(即 bit8)为 0,表示该 L 字段占一个字节,它的后续 7个 bit 位(即 bit7~bit1)表示子域取值的长度,采用二进制数表示子域取值长度的十进制数。例如,某个域取值占 3 个字节,那么其子域取值长度表示为“00000011”。所以,若子域取值的长度在 1~127 字节之间,那么该 L 字段本身仅占一个字节。 
           b)  当 L 字段最左边字节的最左 bit 位(即 bit8)为 1,表示该 L 字段不止占一个字节,那么它到底占几个字节由该最左字节的后续 7 个 bit 位(即 bit7~bit1)的十进制取值表示。例如,若最左字节为 10000010,表示 L 字段除该字节外,后面还有两个字节。其后续字节的十进制
取值表示子域取值的长度。例如,若 L 字段为“1000 0001 1111 1111”,表示该子域取值占255 个字节。所以,若子域取值的长度在 127~255 字节之间,那么该 L 字段本身需占两个字节。 

   解析代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Tlv {  
  2.   
  3.     /** 子域Tag标签 */  
  4.     private String tag;  
  5.   
  6.     /** 子域取值的长度 */  
  7.     private int length;  
  8.   
  9.     /** 子域取值 */  
  10.     private String value;  
  11.   
  12.     public Tlv(String tag, int length, String value) {  
  13.         this.length = length;  
  14.         this.tag = tag;  
  15.         this.value = value;  
  16.     }  
  17.   
  18.     public String getTag() {  
  19.         return tag;  
  20.     }  
  21.   
  22.     public void setTag(String tag) {  
  23.         this.tag = tag;  
  24.     }  
  25.   
  26.     public int getLength() {  
  27.         return length;  
  28.     }  
  29.   
  30.     public void setLength(int length) {  
  31.         this.length = length;  
  32.     }  
  33.   
  34.     public String getValue() {  
  35.         return value;  
  36.     }  
  37.   
  38.     public void setValue(String value) {  
  39.         this.value = value;  
  40.     }  
  41.   
  42.     @Override  
  43.     public String toString() {  
  44.         return "tag=[" + this.tag + "]," + "length=[" + this.length + "]," + "value=[" + this.value + "]";  
  45.     }  
  46. }  

TLV解析类:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * 将字符串转换为TLV对象 
  3.  *  
  4.  */  
  5. public abstract class TlvUtils {  
  6.   
  7.     /** 
  8.      * 将16进制字符串转换为TLV对象列表 
  9.      * @param hexString 
  10.      * @return 
  11.      */  
  12.     public static List<Tlv> builderTlvList(String hexString) {  
  13.         List<Tlv> tlvs = new ArrayList<Tlv>();  
  14.   
  15.         int position = 0;  
  16.         while (position != StringUtils.length(hexString)) {  
  17.             String _hexTag = getTag(hexString, position);  
  18.             position += _hexTag.length();  
  19.   
  20.             LPositon l_position = getLengthAndPosition(hexString, position);  
  21.             int _vl = l_position.get_vL();  
  22.   
  23.             position = l_position.get_position();  
  24.   
  25.             String _value = StringUtils.substring(hexString, position, position + _vl * 2);  
  26.   
  27.             position = position + _value.length();  
  28.   
  29.             tlvs.add(new Tlv(_hexTag, _vl, _value));  
  30.         }  
  31.         return tlvs;  
  32.     }  
  33.       
  34.     /** 
  35.      * 将16进制字符串转换为TLV对象MAP 
  36.      * @param hexString 
  37.      * @return 
  38.      */  
  39.     public static Map<String, Tlv> builderTlvMap(String hexString) {  
  40.   
  41.         Map<String, Tlv> tlvs = new HashMap<String, Tlv>();  
  42.   
  43.         int position = 0;  
  44.         while (position != hexString.length()) {  
  45.             String _hexTag = getTag(hexString, position);  
  46.               
  47.             position += _hexTag.length();  
  48.               
  49.             LPositon l_position = getLengthAndPosition(hexString, position);  
  50.               
  51.             int _vl = l_position.get_vL();  
  52.             position = l_position.get_position();  
  53.             String _value = hexString.substring(position, position + _vl * 2);  
  54.             position = position + _value.length();  
  55.               
  56.             tlvs.put(_hexTag, new Tlv(_hexTag, _vl, _value));  
  57.         }  
  58.         return tlvs;  
  59.     }  
  60.       
  61.     /** 
  62.      * 返回最后的Value的长度 
  63.      *  
  64.      * @param hexString 
  65.      * @param position 
  66.      * @return  
  67.      */  
  68.     private static LPositon getLengthAndPosition(String hexString, int position) {  
  69.         String firstByteString = hexString.substring(position, position + 2);  
  70.         int i = Integer.parseInt(firstByteString, 16);  
  71.         String hexLength = "";  
  72.   
  73.         if (((i >>> 7) & 1) == 0) {  
  74.             hexLength = hexString.substring(position, position + 2);  
  75.             position = position + 2;  
  76.         } else {  
  77.             // 当最左侧的bit位为1的时候,取得后7bit的值,  
  78.             int _L_Len = i & 127;  
  79.             position = position + 2;  
  80.             hexLength = hexString.substring(position, position + _L_Len * 2);  
  81.             // position表示第一个字节,后面的表示有多少个字节来表示后面的Value值  
  82.             position = position + _L_Len * 2;  
  83.         }  
  84.         return new LPositon(Integer.parseInt(hexLength, 16), position);  
  85.   
  86.     }  
  87.   
  88.     /** 
  89.      * 取得子域Tag标签 
  90.      *  
  91.      * @param hexString 
  92.      * @param position 
  93.      * @return 
  94.      */  
  95.     private static String getTag(String hexString, int position) {  
  96.         String firstByte = StringUtils.substring(hexString, position, position + 2);  
  97.         int i = Integer.parseInt(firstByte, 16);  
  98.         if ((i & 0x1f) == 0x1f) {  
  99.             return hexString.substring(position, position + 4);  
  100.   
  101.         } else {  
  102.             return hexString.substring(position, position + 2);  
  103.         }  
  104.     }  
  105. }  

LPosition类内容如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * value的长度 
  3.  */  
  4. public class LPositon {  
  5.   
  6.     private int _vL;  
  7.     private int _position;  
  8.   
  9.     public LPositon(int _vL, int position) {  
  10.         this._vL = _vL;  
  11.         this._position = position;  
  12.     }  
  13.   
  14.     public int get_vL() {  
  15.         return _vL;  
  16.     }  
  17.   
  18.     public void set_vL(int _vL) {  
  19.         this._vL = _vL;  
  20.     }  
  21.   
  22.     public int get_position() {  
  23.         return _position;  
  24.     }  
  25.   
  26.     public void set_position(int _position) {  
  27.         this._position = _position;  
  28.     }  
  29. }  


猜你喜欢

转载自blog.csdn.net/adminlxb89/article/details/80463268