Implementing Delimited I/O Functions in C for Protocol Buffers
In Java, Protocol Buffers version 2.1.0 introduced delimited I/O functions to facilitate reading and writing multiple messages from files. These functions, parseDelimitedFrom, mergeDelimitedFrom, and writeDelimitedTo, work by attaching length prefixes to the messages.
The original question inquired about the existence of equivalent functions for C . While the Java API provides these functions, the official C library initially lacked them. However, as of version 3.3.0, Google added these functions to google/protobuf/util/delimited_message_util.h.
If you are using an older version of the C library, you can implement delimited I/O using the following code:
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 message size. const int size = message.ByteSize(); output.WriteVarint32(size); uint8_t* buffer = output.GetDirectBufferForNBytesAndAdvance(size); if (buffer != NULL) { // Optimization: Serialize message directly to array if it fits. message.SerializeWithCachedSizesToArray(buffer); } else { // Slower path for messages spanning multiple buffers. message.SerializeWithCachedSizes(&output); if (output.HadError()) return false; } return true; } 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 message 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 stream limit. input.PopLimit(limit); return true; }
The above is the detailed content of How Can I Implement Delimited I/O for Protocol Buffers in C ?. For more information, please follow other related articles on the PHP Chinese website!