delimited_message_util.h 3.4 KB

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