Sheppard Y

keep thinking keep coding.

as3里ByteArray的readBytes方法length参数为0时并非是读0个字节

2016-07-03 日更新 此篇博客已经迁移到新博客,并做行文检查和优化排版:


一、官方文档注解如下:
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.
二、参数注意
第3个参数length,如果传0表示将ByteArray里的剩余数据全部读进bytes里,与通常的API理解不一致。
这点有点怪异,我的程序里,ByteArray类型的buff需要从ByteArray类型的recvBuff里读取给定数量的字节数。字节数有可能是0(protobuf里,如果全为optional字段,并且没有设定值,这个protobuf协议序列化的结果就是0字节),企图用recvBuff.readBytes(buff, 0, 0)来读取零字节是不能达到目的的。
recvBuff里还有其他的网络包数据,如果用readBytes(buff, 0, 0)会把recvBuff里其他网络包的数据读给了当前buff,然后交给当前包对应的protobuf类来反序列化,就错了。
症状如下,方便大家搜索到此文:
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上的博文

posted on 2013-04-08 17:54 Sheppard Y 阅读(2038) 评论(0)  编辑 收藏 引用 所属分类: 其他语言


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2013年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

留言簿(1)

随笔分类(77)

随笔档案(58)

me

基友

同行

业界前辈

最新随笔

搜索

积分与排名

最新评论

阅读排行榜