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