一、官方文檔注解如下:
public function readBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void
Reads the number of data bytes, specified by the length parameter, from the byte stream. The bytes are read into the ByteArray object specified by the bytes parameter, and the bytes are written into the destination ByteArray starting at the position specified by offset.
Parameters
bytes:ByteArray — The ByteArray object to read data into.
offset:uint (default = 0) — The offset (position) in bytes at which the read data should be written.
length:uint (default = 0) — The number of bytes to read. The default value of 0 causes all available data to be read.
二、參數(shù)注意
第3個(gè)參數(shù)length,如果傳0表示將ByteArray里的剩余數(shù)據(jù)全部讀進(jìn)bytes里,與通常的API理解不一致。
這點(diǎn)有點(diǎn)怪異,我的程序里,ByteArray類型的buff需要從ByteArray類型的recvBuff里讀取給定數(shù)量的字節(jié)數(shù)。字節(jié)數(shù)有可能是0(protobuf里,如果全為optional字段,并且沒有設(shè)定值,這個(gè)protobuf協(xié)議序列化的結(jié)果就是0字節(jié)),企圖用recvBuff.readBytes(buff, 0, 0)來讀取零字節(jié)是不能達(dá)到目的的。
recvBuff里還有其他的網(wǎng)絡(luò)包數(shù)據(jù),如果用readBytes(buff, 0, 0)會(huì)把recvBuff里其他網(wǎng)絡(luò)包的數(shù)據(jù)讀給了當(dāng)前buff,然后交給當(dāng)前包對應(yīng)的protobuf類來反序列化,就錯(cuò)了。
癥狀如下,方便大家搜索到此文:
Bad data format: **.** cannot be set twice.
invalid nested message
message length = 1
注:as3, protobuf-gen-as3(http://code.google.com/p/protoc-gen-as3/)
ps:2012年9月19日我在CU上的博文