FlatBuffer在JAVA下的使用

   早听说FatBuffer的大名,据说比Protobuffer效率还要高。出于当初对Protobuf良好的印象,FlatBuffer同样是Google出品,因此在一个项目中尝试使用了FlatBuffer。
   IDL之类的描述语言,轻车熟路地过了。可是实际使用时。真感觉用起来有很多反直觉的东西。
    首先,一个对象的字符串属性时,必须先在创建这个对象前全部添加到FlatBufferBuilder中,然后再向对象中添加字符串的offset。同样当存在对象类型的属性时,也必须这么干。当是一个属性是对象数组时,就更麻烦了。
    其次,最后生成字节数组时。竟然是用这种奇葩的方式来拷贝出来。
                byte[] byteArray = new byte[fbb.offset()];
// buffer.get(byteArray, 0, fbb.offset());
System.arraycopy(buffer.array(), buffer.position(), byteArray, 0,
fbb.offset());
    这段代码,还是在test的例子里找出来的。
    上面两点,折腾很长时间,看文档,看例子才摸索出来的。

暂且把这段代码粘贴出来,找个备忘,也便于有需要的人吧。
IDL:
第一份:  
table OfferMarket
{
	contractType:string;
	buyOrgID:string;
	sellOrgID:string;
	orderTypeForBuy:string;
	orderNumForBuy:int = 0;
	amountForBuy:double = 0;
	amountCanBuy:double = 0;
	priceForBuy:string ;
	priceForSell:string;
	amountCanSell:double = 0;
	amountForSell:double = 0;
	orderNumForSell:int = 0;
	orderTypeForSell:string;
	latestDealPrice:string;
	latestDealAmount:double = 0;
	updateTime:string;
	referencePrice:string;
	highestPrice:string;
	lowestPrice:string;
	errorCode:string;
	errorMessage:string;
	businessMDBookType:int;
	TType:int=0;
}
root_type OfferMarket;

第二份:
include "OfferMarketModel.fbs";

///最新的市场行情列表
table LatestOfferMarketList  
{  
    marketList : [OfferMarket];  
}  
  
root_type LatestOfferMarketList;

测试代码:
public static void main2(String[] args) {
		FlatBufferBuilder fbb = new FlatBufferBuilder();
		int[] buyOrtIdOffsets = new int[5];
		for (int i = 0; i < 5; i++) {
			buyOrtIdOffsets[i] = fbb.createString("buyOrgId" + i);
		}
		int[] marketsOffsets = new int[5];
		for (int i = 0; i < 5; i++) {
			System.out.println("i: " + i);
			// fbb.createString("MMM");
			OfferMarket.startOfferMarket(fbb);
			OfferMarket.addAmountCanBuy(fbb, 100.1 + i);

			OfferMarket.addBuyOrgID(fbb, buyOrtIdOffsets[i]);
			int offset = OfferMarket.endOfferMarket(fbb);
			// OfferMarket.finishOfferMarketBuffer(fbb, offset);
			System.out.println("offset: " + offset);
			marketsOffsets[i] = offset;
		}

		// 写法一
		// fbb.startVector(4, 5, 4);
		// for(int i=0;i<5;i++)
		// {
		// fbb.addOffset(marketsOffsets[i]);
		// }
		// int endOffset=fbb.endVector();

		// 写法二
		int endOffset = LatestOfferMarketList.createMarketListVector(fbb,
				marketsOffsets);

		// 写法一
		// LatestOfferMarketList.startLatestOfferMarketList(fbb);
		// LatestOfferMarketList.addMarketList(fbb, endOffset);
		// int offsetList=LatestOfferMarketList.endLatestOfferMarketList(fbb);

		// 写法二
		int offsetList = LatestOfferMarketList.createLatestOfferMarketList(fbb,
				endOffset);
		LatestOfferMarketList
				.finishLatestOfferMarketListBuffer(fbb, offsetList);
		ByteBuffer buffer = fbb.dataBuffer();
		System.out.println(buffer.position());

		byte[] byteArray = new byte[fbb.offset()];
		buffer.get(byteArray, 0, fbb.offset());
               //System.arraycopy(buffer.array(), buffer.position(), byteArray, 0,
				fbb.offset());
		LatestOfferMarketList om2 = LatestOfferMarketList
				.getRootAsLatestOfferMarketList(ByteBuffer.wrap(byteArray));// ByteBuffer.wrap(buffer.array()));
		System.out.println("========== om2 len: " + om2.marketListLength());
		for (int i = 0; i < om2.marketListLength(); i++) {
			OfferMarket market = om2.marketList(i);
			System.out.println(market.amountCanBuy());
			System.out.println(market.buyOrgID());
		}
}

你可能感兴趣的:(buffer)