首頁  >  文章  >  Java  >  Java API for Protocol Buffers v2.1.0 中引入的「Delimited」 I/O 函數(parseDelimitedFrom、mergeDelimitedFrom 和 writeDelimitedTo)是否有 C 等效項?如果沒有,h

Java API for Protocol Buffers v2.1.0 中引入的「Delimited」 I/O 函數(parseDelimitedFrom、mergeDelimitedFrom 和 writeDelimitedTo)是否有 C 等效項?如果沒有,h

DDD
DDD原創
2024-10-28 22:30:02744瀏覽

Are there C   equivalents for the

C 語言協定緩衝區的分隔I/O 函數

問題:

問題:

問題:

問題:

<code class="cpp">bool writeDelimitedTo(
    const google::protobuf::MessageLite&amp; message,
    google::protobuf::io::ZeroCopyOutputStream* rawOutput) {
  // Create a new coded stream for each message.
  google::protobuf::io::CodedOutputStream output(rawOutput);

  // Write the size.
  int size = message.ByteSize();
  output.WriteVarint32(size);

  // Optimize for messages that fit in a single buffer.
  uint8_t* buffer = output.GetDirectBufferForNBytesAndAdvance(size);
  if (buffer != NULL) {
    message.SerializeWithCachedSizesToArray(buffer);
    return true;
  }

  // Slower path for messages that span multiple buffers.
  message.SerializeWithCachedSizes(&amp;output);
  return !output.HadError();
}

bool readDelimitedFrom(
    google::protobuf::io::ZeroCopyInputStream* rawInput,
    google::protobuf::MessageLite* message) {
  // Create a new coded stream for each message.
  google::protobuf::io::CodedInputStream input(rawInput);

  // Read the size.
  uint32_t size;
  if (!input.ReadVarint32(&amp;size)) return false;

  // Limit the stream to the message size.
  google::protobuf::io::CodedInputStream::Limit limit = input.PushLimit(size);

  // Parse the message.
  if (!message->MergeFromCodedStream(&amp;input)) return false;
  if (!input.ConsumedEntireMessage()) return false;

  // Release the limit.
  input.PopLimit(limit);

  return true;
}</code>

問題:

問題:問題:問題:問Protocol Buffers API v2.1.0 引入了一組「定界」I/O 函數,包括parseDelimitedFrom、mergeDelimitedFrom 和writeDelimitedTo。這些函數有 C 等效函數嗎?如果沒有,C 中如何處理訊息大小前綴? 答案:從v3.3.0 開始,Google 已向C 中添加了分隔I/O 函數google/protobuf/util/delimited_message_util.h 中的庫。但是,還有一些替代實作可以提供更多最佳化並解決潛在的缺陷:這些實作可確保正確處理大小超過 64MB 的訊息,同時仍對單一訊息強制執行 64MB 限制。

以上是Java API for Protocol Buffers v2.1.0 中引入的「Delimited」 I/O 函數(parseDelimitedFrom、mergeDelimitedFrom 和 writeDelimitedTo)是否有 C 等效項?如果沒有,h的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn