栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java中协议缓冲区分隔的I / O功能是否有C ++等效项?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中协议缓冲区分隔的I / O功能是否有C ++等效项?

我在这里参加聚会有点晚,但是下面的实现包括其他答案中缺少的一些优化,并且在输入64MB后不会失败(尽管它仍然对每个单独的消息强制执行64MB的限制,只是对整个消息流没有限制))。

(我是C ++和Java protobuf库的作者,但我不再在Google上工作。很抱歉,此代码从未将其添加到官方lib中。如果有的话,它的样子。)

bool writeDelimitedTo(    const google::protobuf::MessageLite& message,    google::protobuf::io::ZeroCopyOutputStream* rawOutput) {  // We create a new pred stream for each message.  Don't worry, this is fast.  google::protobuf::io::CodedOutputStream output(rawOutput);  // Write the size.  const int size = message.ByteSize();  output.WriteVarint32(size);  uint8_t* buffer = output.GetDirectBufferForNBytesAndAdvance(size);  if (buffer != NULL) {    // Optimization:  The message fits in one buffer, so use the faster    // direct-to-array serialization path.    message.SerializeWithCachedSizesToArray(buffer);  } else {    // Slightly-slower path when the message is multiple buffers.    message.SerializeWithCachedSizes(&output);    if (output.HadError()) return false;  }  return true;}bool readDelimitedFrom(    google::protobuf::io::ZeroCopyInputStream* rawInput,    google::protobuf::MessageLite* message) {  // We create a new pred stream for each message.  Don't worry, this is fast,  // and it makes sure the 64MB total size limit is imposed per-message rather  // than on the whole stream.  (See the CodedInputStream interface for more  // info on this limit.)  google::protobuf::io::CodedInputStream input(rawInput);  // Read the size.  uint32_t size;  if (!input.ReadVarint32(&size)) return false;  // Tell the stream not to read beyond that size.  google::protobuf::io::CodedInputStream::Limit limit =      input.PushLimit(size);  // Parse the message.  if (!message->MergeFromCodedStream(&input)) return false;  if (!input.ConsumedEntireMessage()) return false;  // Release the limit.  input.PopLimit(limit);  return true;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号