| 1 | // Adapted from the patch of kenton@google.com (Kenton Varda) |
| 2 | // See https://github.com/google/protobuf/pull/710 for details. |
| 3 | |
| 4 | #ifndef GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__ |
| 5 | #define GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__ |
| 6 | |
| 7 | #include <ostream> |
| 8 | |
| 9 | #include <google/protobuf/message_lite.h> |
| 10 | #include <google/protobuf/io/coded_stream.h> |
| 11 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 12 | |
| 13 | namespace google { |
| 14 | namespace protobuf { |
| 15 | namespace util { |
| 16 | |
| 17 | // Write a single size-delimited message from the given stream. Delimited |
| 18 | // format allows a single file or stream to contain multiple messages, |
| 19 | // whereas normally writing multiple non-delimited messages to the same |
| 20 | // stream would cause them to be merged. A delimited message is a varint |
| 21 | // encoding the message size followed by a message of exactly that size. |
| 22 | // |
| 23 | // Note that if you want to *read* a delimited message from a file descriptor |
| 24 | // or istream, you will need to construct an io::FileInputStream or |
| 25 | // io::OstreamInputStream (implementations of io::ZeroCopyStream) and use the |
| 26 | // utility function ParseDelimitedFromZeroCopyStream(). You must then |
| 27 | // continue to use the same ZeroCopyInputStream to read all further data from |
| 28 | // the stream until EOF. This is because these ZeroCopyInputStream |
| 29 | // implementations are buffered: they read a big chunk of data at a time, |
| 30 | // then parse it. As a result, they may read past the end of the delimited |
| 31 | // message. There is no way for them to push the extra data back into the |
| 32 | // underlying source, so instead you must keep using the same stream object. |
| 33 | bool LIBPROTOBUF_EXPORT SerializeDelimitedToFileDescriptor(const MessageLite& message, int file_descriptor); |
| 34 | |
| 35 | bool LIBPROTOBUF_EXPORT SerializeDelimitedToOstream(const MessageLite& message, std::ostream* output); |
| 36 | |
| 37 | // Read a single size-delimited message from the given stream. Delimited |
| 38 | // format allows a single file or stream to contain multiple messages, |
| 39 | // whereas normally parsing consumes the entire input. A delimited message |
| 40 | // is a varint encoding the message size followed by a message of exactly |
| 41 | // that size. |
| 42 | // |
| 43 | // If |clean_eof| is not NULL, then it will be set to indicate whether the |
| 44 | // stream ended cleanly. That is, if the stream ends without this method |
| 45 | // having read any data at all from it, then *clean_eof will be set true, |
| 46 | // otherwise it will be set false. Note that these methods return false |
| 47 | // on EOF, but they also return false on other errors, so |clean_eof| is |
| 48 | // needed to distinguish a clean end from errors. |
| 49 | bool LIBPROTOBUF_EXPORT ParseDelimitedFromZeroCopyStream(MessageLite* message, io::ZeroCopyInputStream* input, bool* clean_eof); |
| 50 | |
| 51 | bool LIBPROTOBUF_EXPORT ParseDelimitedFromCodedStream(MessageLite* message, io::CodedInputStream* input, bool* clean_eof); |
| 52 | |
| 53 | // Write a single size-delimited message from the given stream. Delimited |
| 54 | // format allows a single file or stream to contain multiple messages, |
| 55 | // whereas normally writing multiple non-delimited messages to the same |
| 56 | // stream would cause them to be merged. A delimited message is a varint |
| 57 | // encoding the message size followed by a message of exactly that size. |
| 58 | bool LIBPROTOBUF_EXPORT SerializeDelimitedToZeroCopyStream(const MessageLite& message, io::ZeroCopyOutputStream* output); |
| 59 | |
| 60 | bool LIBPROTOBUF_EXPORT SerializeDelimitedToCodedStream(const MessageLite& message, io::CodedOutputStream* output); |
| 61 | |
| 62 | } // namespace util |
| 63 | } // namespace protobuf |
| 64 | } // namespace google |
| 65 | |
| 66 | #endif // GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__ |
| 67 | |