Home >Backend Development >C++ >C Protocol Buffer Delimited I/O: Are There Java-Equivalent Functions?

C Protocol Buffer Delimited I/O: Are There Java-Equivalent Functions?

Barbara Streisand
Barbara StreisandOriginal
2024-12-05 00:32:12208browse

C   Protocol Buffer Delimited I/O: Are There Java-Equivalent Functions?

Protocol Buffers Delimited I/O Functions: C Equivalents?

In an attempt to read and write multiple Protocol Buffers messages from files using both C and Java, it has been observed that Java provides a set of "Delimited" I/O functions for this purpose. However, it remains uncertain if C offers similar functionality.

The Java API functions are:

  • parseDelimitedFrom
  • mergeDelimitedFrom
  • writeDelimitedTo

C Equivalents

As of version 3.3.0, Google has addressed this issue by introducing the following C equivalents in google/protobuf/util/delimited_message_util.h:

bool writeDelimitedTo(
    const google::protobuf::MessageLite& message,
    google::protobuf::io::ZeroCopyOutputStream* rawOutput) {
  // Code to write delimited messages
}

bool readDelimitedFrom(
    google::protobuf::io::ZeroCopyInputStream* rawInput,
    google::protobuf::MessageLite* message) {
  // Code to read delimited messages
}

Wire Format for Java Size Prefixes

For those seeking to parse the size-prefixed messages in C without using the official library, the wire format is as follows:

  1. Size of the message is represented as a 32-bit variable-length integer.
  2. A single byte is added after the size to ensure backward-compatibility.
  3. The message data is appended after the size and compatibility byte.

Optimizations

The provided C implementations include optimizations that were missing from other responses. These optimizations ensure that the functions:

  • Do not fail after 64MB of input.
  • Enforce the 64MB limit on each individual message.
  • Use a faster direct-to-array serialization path when possible.

The above is the detailed content of C Protocol Buffer Delimited I/O: Are There Java-Equivalent Functions?. For more information, please follow other related articles on the PHP Chinese website!

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