Home > Backend Development > C++ > How to Read and Write Protocol Buffer Messages with Length Prefixes in C and Java?

How to Read and Write Protocol Buffer Messages with Length Prefixes in C and Java?

Patricia Arquette
Release: 2024-12-09 08:17:16
Original
658 people have browsed it

How to Read and Write Protocol Buffer Messages with Length Prefixes in C   and Java?

C Equivalents of Protocol Buffers Delimited I/O Functions in Java

Question:

In C and Java, how can Protocol Buffers messages be read and written to files using length prefixes? The Java API has "Delimited" I/O functions for this purpose, but are there equivalent functions in C ? If not, what is the underlying wire format for the size prefixes?

Answer:

As of version 3.3.0, C now includes equivalent functions in google/protobuf/util/delimited_message_util.h. These functions provide a faster and more optimized implementation than previously suggested approaches.

Original Implementation:

Before the introduction of the official equivalents, the following optimized C implementation was available:

bool writeDelimitedTo(
    const google::protobuf::MessageLite& message,
    google::protobuf::io::ZeroCopyOutputStream* rawOutput) {
  // Use a new coded stream for each message (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: Direct-to-array serialization for messages in a single buffer.
    message.SerializeWithCachedSizesToArray(buffer);
  } else {
    // Slower path for messages across multiple buffers.
    message.SerializeWithCachedSizes(&output);
    if (output.HadError()) return false;
  }

  return true;
}

bool readDelimitedFrom(
    google::protobuf::io::ZeroCopyInputStream* rawInput,
    google::protobuf::MessageLite* message) {
  // Each message uses its own coded stream (fast).
  google::protobuf::io::CodedInputStream input(rawInput);

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

  // Limit the stream to the size of the message.
  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;
}
Copy after login

The above is the detailed content of How to Read and Write Protocol Buffer Messages with Length Prefixes in C and Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template