delimited_message_util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Adapted from the patch of kenton@google.com (Kenton Varda)
  31. // See https://github.com/protocolbuffers/protobuf/pull/710 for details.
  32. #ifndef GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
  33. #define GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
  34. #include <ostream>
  35. #include <google/protobuf/message_lite.h>
  36. #include <google/protobuf/io/coded_stream.h>
  37. #include <google/protobuf/io/zero_copy_stream_impl.h>
  38. #include <google/protobuf/port_def.inc>
  39. namespace google {
  40. namespace protobuf {
  41. namespace util {
  42. // Write a single size-delimited message from the given stream. Delimited
  43. // format allows a single file or stream to contain multiple messages,
  44. // whereas normally writing multiple non-delimited messages to the same
  45. // stream would cause them to be merged. A delimited message is a varint
  46. // encoding the message size followed by a message of exactly that size.
  47. //
  48. // Note that if you want to *read* a delimited message from a file descriptor
  49. // or istream, you will need to construct an io::FileInputStream or
  50. // io::OstreamInputStream (implementations of io::ZeroCopyStream) and use the
  51. // utility function ParseDelimitedFromZeroCopyStream(). You must then
  52. // continue to use the same ZeroCopyInputStream to read all further data from
  53. // the stream until EOF. This is because these ZeroCopyInputStream
  54. // implementations are buffered: they read a big chunk of data at a time,
  55. // then parse it. As a result, they may read past the end of the delimited
  56. // message. There is no way for them to push the extra data back into the
  57. // underlying source, so instead you must keep using the same stream object.
  58. bool PROTOBUF_EXPORT SerializeDelimitedToFileDescriptor(
  59. const MessageLite& message, int file_descriptor);
  60. bool PROTOBUF_EXPORT SerializeDelimitedToOstream(const MessageLite& message,
  61. std::ostream* output);
  62. // Read a single size-delimited message from the given stream. Delimited
  63. // format allows a single file or stream to contain multiple messages,
  64. // whereas normally parsing consumes the entire input. A delimited message
  65. // is a varint encoding the message size followed by a message of exactly
  66. // that size.
  67. //
  68. // If |clean_eof| is not NULL, then it will be set to indicate whether the
  69. // stream ended cleanly. That is, if the stream ends without this method
  70. // having read any data at all from it, then *clean_eof will be set true,
  71. // otherwise it will be set false. Note that these methods return false
  72. // on EOF, but they also return false on other errors, so |clean_eof| is
  73. // needed to distinguish a clean end from errors.
  74. bool PROTOBUF_EXPORT ParseDelimitedFromZeroCopyStream(
  75. MessageLite* message, io::ZeroCopyInputStream* input, bool* clean_eof);
  76. bool PROTOBUF_EXPORT ParseDelimitedFromCodedStream(MessageLite* message,
  77. io::CodedInputStream* input,
  78. bool* clean_eof);
  79. // Write a single size-delimited message from the given stream. Delimited
  80. // format allows a single file or stream to contain multiple messages,
  81. // whereas normally writing multiple non-delimited messages to the same
  82. // stream would cause them to be merged. A delimited message is a varint
  83. // encoding the message size followed by a message of exactly that size.
  84. bool PROTOBUF_EXPORT SerializeDelimitedToZeroCopyStream(
  85. const MessageLite& message, io::ZeroCopyOutputStream* output);
  86. bool PROTOBUF_EXPORT SerializeDelimitedToCodedStream(
  87. const MessageLite& message, io::CodedOutputStream* output);
  88. } // namespace util
  89. } // namespace protobuf
  90. } // namespace google
  91. #include <google/protobuf/port_undef.inc>
  92. #endif // GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__