程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Java中协议缓冲区分隔的I / O功能是否有C ++等效项?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到Java中协议缓冲区分隔的I / O功能是否有C ++等效项?的问题如何解决?下面主要结合日常开发的经验,给出你关于Java中协议缓冲区分隔的I / O功能是否有C ++等效项?的解决方法建议,希望对你解决Java中协议缓冲区分隔的I / O功能是否有C ++等效项?有所启发或帮助;

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

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

@H_772_7@bool writedelimitedTo(
    const Google::protobuf::messagelite& message,
    Google::protobuf::io::ZerocopyOutputStream* rawOutput) {
  // We create a new coded 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 coded stream for each message.  Don't worry, this is fast,
  // and it makes sure the 64MB @R_202_10586@l 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;
}

解决方法

我正在尝试从文件(使用C ++和Java)读取/写入多个协议缓冲区消息。Google建议在邮件前写上长度前缀,但是默认情况下没有办法做到这一点(我可以看到)。@H_197_3@

但是,版本2.1.0的Java API收到了一组“定界” I / O函数,这些函数显然可以完成此工作:@H_197_3@

@H_772_7@parseDelimitedFrom
mergeDelimitedFrom
writeDelimitedTo

是否有C 解析这些消息?@H_197_3@

大佬总结

以上是大佬教程为你收集整理的Java中协议缓冲区分隔的I / O功能是否有C ++等效项?全部内容,希望文章能够帮你解决Java中协议缓冲区分隔的I / O功能是否有C ++等效项?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。