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 31 // Adapted from the patch of kenton@google.com (Kenton Varda) 32 // See https://github.com/protocolbuffers/protobuf/pull/710 for details. 33 34 #ifndef GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__ 35 #define GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__ 36 37 38 #include <ostream> 39 40 #include <google/protobuf/message_lite.h> 41 #include <google/protobuf/io/coded_stream.h> 42 #include <google/protobuf/io/zero_copy_stream_impl.h> 43 44 #include <google/protobuf/port_def.inc> 45 46 namespace google { 47 namespace protobuf { 48 namespace util { 49 50 // Write a single size-delimited message from the given stream. Delimited 51 // format allows a single file or stream to contain multiple messages, 52 // whereas normally writing multiple non-delimited messages to the same 53 // stream would cause them to be merged. A delimited message is a varint 54 // encoding the message size followed by a message of exactly that size. 55 // 56 // Note that if you want to *read* a delimited message from a file descriptor 57 // or istream, you will need to construct an io::FileInputStream or 58 // io::OstreamInputStream (implementations of io::ZeroCopyStream) and use the 59 // utility function ParseDelimitedFromZeroCopyStream(). You must then 60 // continue to use the same ZeroCopyInputStream to read all further data from 61 // the stream until EOF. This is because these ZeroCopyInputStream 62 // implementations are buffered: they read a big chunk of data at a time, 63 // then parse it. As a result, they may read past the end of the delimited 64 // message. There is no way for them to push the extra data back into the 65 // underlying source, so instead you must keep using the same stream object. 66 bool PROTOBUF_EXPORT SerializeDelimitedToFileDescriptor( 67 const MessageLite& message, int file_descriptor); 68 69 bool PROTOBUF_EXPORT SerializeDelimitedToOstream(const MessageLite& message, 70 std::ostream* output); 71 72 // Read a single size-delimited message from the given stream. Delimited 73 // format allows a single file or stream to contain multiple messages, 74 // whereas normally parsing consumes the entire input. A delimited message 75 // is a varint encoding the message size followed by a message of exactly 76 // that size. 77 // 78 // If |clean_eof| is not NULL, then it will be set to indicate whether the 79 // stream ended cleanly. That is, if the stream ends without this method 80 // having read any data at all from it, then *clean_eof will be set true, 81 // otherwise it will be set false. Note that these methods return false 82 // on EOF, but they also return false on other errors, so |clean_eof| is 83 // needed to distinguish a clean end from errors. 84 bool PROTOBUF_EXPORT ParseDelimitedFromZeroCopyStream( 85 MessageLite* message, io::ZeroCopyInputStream* input, bool* clean_eof); 86 87 bool PROTOBUF_EXPORT ParseDelimitedFromCodedStream(MessageLite* message, 88 io::CodedInputStream* input, 89 bool* clean_eof); 90 91 // Write a single size-delimited message from the given stream. Delimited 92 // format allows a single file or stream to contain multiple messages, 93 // whereas normally writing multiple non-delimited messages to the same 94 // stream would cause them to be merged. A delimited message is a varint 95 // encoding the message size followed by a message of exactly that size. 96 bool PROTOBUF_EXPORT SerializeDelimitedToZeroCopyStream( 97 const MessageLite& message, io::ZeroCopyOutputStream* output); 98 99 bool PROTOBUF_EXPORT SerializeDelimitedToCodedStream( 100 const MessageLite& message, io::CodedOutputStream* output); 101 102 } // namespace util 103 } // namespace protobuf 104 } // namespace google 105 106 #include <google/protobuf/port_undef.inc> 107 108 #endif // GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__ 109