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

一、官方文档注解如下:

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 阅读(1884) 评论(0)  编辑 收藏 引用 所属分类: 其他语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值