Home >Java >javaTutorial >Are there C equivalents for the \'Delimited\' I/O functions (parseDelimitedFrom, mergeDelimitedFrom, and writeDelimitedTo) introduced in the Java API for Protocol Buffers v2.1.0? If not, h
Delimited I/O Functions for Protocol Buffers in C
Question:
The Java API for Protocol Buffers v2.1.0 introduced a set of "Delimited" I/O functions, including parseDelimitedFrom, mergeDelimitedFrom, and writeDelimitedTo. Do these functions have C equivalents? If not, how can message size prefixes be handled in C ?
Answer:
As of v3.3.0, Google has added delimited I/O functions to the C library in google/protobuf/util/delimited_message_util.h. However, there are alternative implementations that provide more optimizations and address potential pitfalls:
<code class="cpp">bool writeDelimitedTo( const google::protobuf::MessageLite& 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(&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(&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(&input)) return false; if (!input.ConsumedEntireMessage()) return false; // Release the limit. input.PopLimit(limit); return true; }</code>
These implementations ensure proper handling of messages with sizes exceeding 64MB, while still enforcing a 64MB limit on individual messages.
The above is the detailed content of Are there C equivalents for the 'Delimited' I/O functions (parseDelimitedFrom, mergeDelimitedFrom, and writeDelimitedTo) introduced in the Java API for Protocol Buffers v2.1.0? If not, h. For more information, please follow other related articles on the PHP Chinese website!