Skip to content

Instantly share code, notes, and snippets.

@wjch
Created December 20, 2015 14:33
Show Gist options
  • Save wjch/0d61987ab9bb223d1b3f to your computer and use it in GitHub Desktop.
Save wjch/0d61987ab9bb223d1b3f to your computer and use it in GitHub Desktop.
TlvBox.java
/*
* COPYRIGHT NOTICE
* Copyright (C) 2015, Jhuster, All Rights Reserved
* Author: Jhuster(lujun.hust@gmail.com)
*
* https://github.com/Jhuster/TLV
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
public class TLvTester {
public static final int TEST_TYPE_1 = 0x01;
public static final int TEST_TYPE_2 = 0x02;
public static final int TEST_TYPE_3 = 0x03;
public static final int TEST_TYPE_4 = 0x04;
public static final int TEST_TYPE_5 = 0x05;
public static final int TEST_TYPE_6 = 0x06;
public static final int TEST_TYPE_7 = 0x07;
public static final int TEST_TYPE_8 = 0x08;
public static final int TEST_TYPE_9 = 0x09;
public static void main(String args[]) {
TlvBox box = new TlvBox();
box.putByteValue(TEST_TYPE_1, (byte)1);
box.putShortValue(TEST_TYPE_2,(short)2);
box.putIntValue(TEST_TYPE_3,(int)3);
box.putLongValue(TEST_TYPE_4,(long)4);
box.putFloatValue(TEST_TYPE_5,(float)5.67);
box.putDoubleValue(TEST_TYPE_6,(double)8.91);
box.putStringValue(TEST_TYPE_7, "hello world !");
box.putBytesValue(TEST_TYPE_8,new byte[] {1,2,3,4,5,6} );
TlvBox boxes = new TlvBox();
boxes.putObjectValue(TEST_TYPE_9, box);
byte[] serialized = boxes.serialize();
TlvBox parsedBox = TlvBox.parse(serialized, 0, serialized.length);
TlvBox parsedObject = parsedBox.getObjectValue(TEST_TYPE_9);
System.out.println("TEST_TYPE_1: " + parsedObject.getByteValue(TEST_TYPE_1));
System.out.println("TEST_TYPE_2: " + parsedObject.getShortValue(TEST_TYPE_2));
System.out.println("TEST_TYPE_3: " + parsedObject.getIntValue(TEST_TYPE_3));
System.out.println("TEST_TYPE_4: " + parsedObject.getLongValue(TEST_TYPE_4));
System.out.println("TEST_TYPE_5: " + parsedObject.getFloatValue(TEST_TYPE_5));
System.out.println("TEST_TYPE_6: " + parsedObject.getDoubleValue(TEST_TYPE_6));
System.out.println("TEST_TYPE_7: " + parsedObject.getStringValue(TEST_TYPE_7));
byte[] bytes = parsedObject.getBytesValue(TEST_TYPE_8);
for(byte value : bytes) {
System.out.println("TEST_TYPE_8: " + value);
}
}
}
/*
* COPYRIGHT NOTICE
* Copyright (C) 2015, Jhuster, All Rights Reserved
* Author: Jhuster(lujun.hust@gmail.com)
*
* https://github.com/Jhuster/TLV
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Set;
public class TlvBox {
private static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
private HashMap<Integer,byte[]> mObjects;
private int mTotalBytes = 0;
public TlvBox() {
mObjects = new HashMap<Integer,byte[]>();
}
public static TlvBox parse(byte[] buffer,int offset,int length) {
TlvBox box = new TlvBox();
int parsed = 0;
while(parsed < length) {
int type = ByteBuffer.wrap(buffer,offset+parsed,4).order(DEFAULT_BYTE_ORDER).getInt();
parsed += 4;
int size = ByteBuffer.wrap(buffer,offset+parsed,4).order(DEFAULT_BYTE_ORDER).getInt();
parsed += 4;
byte[] value = new byte[size];
System.arraycopy(buffer, offset+parsed, value, 0, size);
box.putBytesValue(type,value);
parsed += size;
}
return box;
}
public byte[] serialize() {
int offset = 0;
byte[] result = new byte[mTotalBytes];
Set<Integer> keys = mObjects.keySet();
for(Integer key : keys) {
byte[] bytes = mObjects.get(key);
byte[] type = ByteBuffer.allocate(4).order(DEFAULT_BYTE_ORDER).putInt(key).array();
byte[] length = ByteBuffer.allocate(4).order(DEFAULT_BYTE_ORDER).putInt(bytes.length).array();
System.arraycopy(type, 0, result, offset, type.length);
offset += 4;
System.arraycopy(length, 0, result, offset, length.length);
offset += 4;
System.arraycopy(bytes, 0, result, offset, bytes.length);
offset += bytes.length;
}
return result;
}
public void putByteValue(int type,byte value) {
byte[] bytes = new byte[1];
bytes[0] = value;
putBytesValue(type,bytes);
}
public void putShortValue(int type,short value) {
byte[] bytes = ByteBuffer.allocate(2).order(DEFAULT_BYTE_ORDER).putShort(value).array();
putBytesValue(type,bytes);
}
public void putIntValue(int type,int value) {
byte[] bytes = ByteBuffer.allocate(4).order(DEFAULT_BYTE_ORDER).putInt(value).array();
putBytesValue(type,bytes);
}
public void putLongValue(int type,long value) {
byte[] bytes = ByteBuffer.allocate(8).order(DEFAULT_BYTE_ORDER).putLong(value).array();
putBytesValue(type,bytes);
}
public void putFloatValue(int type,float value) {
byte[] bytes = ByteBuffer.allocate(4).order(DEFAULT_BYTE_ORDER).putFloat(value).array();
putBytesValue(type,bytes);
}
public void putDoubleValue(int type,double value) {
byte[] bytes = ByteBuffer.allocate(8).order(DEFAULT_BYTE_ORDER).putDouble(value).array();
putBytesValue(type,bytes);
}
public void putStringValue(int type,String value) {
putBytesValue(type,value.getBytes());
}
public void putObjectValue(int type,TlvBox value) {
putBytesValue(type,value.serialize());
}
public void putBytesValue(int type,byte[] value) {
mObjects.put(type, value);
mTotalBytes += value.length + 8;
}
public Byte getByteValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return bytes[0];
}
public Short getShortValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return ByteBuffer.wrap(bytes).order(DEFAULT_BYTE_ORDER).getShort();
}
public Integer getIntValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return ByteBuffer.wrap(bytes).order(DEFAULT_BYTE_ORDER).getInt();
}
public Long getLongValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return ByteBuffer.wrap(bytes).order(DEFAULT_BYTE_ORDER).getLong();
}
public Float getFloatValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return ByteBuffer.wrap(bytes).order(DEFAULT_BYTE_ORDER).getFloat();
}
public Double getDoubleValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return ByteBuffer.wrap(bytes).order(DEFAULT_BYTE_ORDER).getDouble();
}
public TlvBox getObjectValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return TlvBox.parse(bytes, 0, bytes.length);
}
public String getStringValue(int type) {
byte[] bytes = mObjects.get(type);
if(bytes == null) {
return null;
}
return new String(bytes).trim();
}
public byte[] getBytesValue(int type) {
byte[] bytes = mObjects.get(type);
return bytes;
}
}
@wjch
Copy link
Author

wjch commented Dec 20, 2015

TLV格式

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment