ホームページ  >  記事  >  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 関数

質問:

Javaプロトコル バッファー用 API v2.1.0 では、parseDelimitedFrom、mergeDelimitedFrom、writeDelimitedTo などの「区切り付き」I/O 関数のセットが導入されました。これらの関数には C に相当する関数がありますか?そうでない場合、メッセージ サイズのプレフィックスは C でどのように処理できますか?

回答:

v3.3.0 の時点で、Google は区切り文字付き I/O 関数を C に追加しました。 google/protobuf/util/delimited_message_util.h のライブラリ。ただし、さらなる最適化を提供し、潜在的な落とし穴に対処する代替実装があります。

<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>

これらの実装は、個々のメッセージに 64 MB の制限を適用しながら、サイズが 64 MB を超えるメッセージを適切に処理することを保証します。

以上がJava API for Protocol Buffers v2.1.0 で導入された「Delimited」I/O 関数 (parseDelimitedFrom、mergeDelimitedFrom、および writeDelimitedTo) に相当する C 言語はありますか?そうでない場合は、hの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。