• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Adapted from the patch of kenton@google.com (Kenton Varda)
9 // See https://github.com/protocolbuffers/protobuf/pull/710 for details.
10 
11 #ifndef GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
12 #define GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
13 
14 #include <ostream>
15 
16 #include "google/protobuf/io/coded_stream.h"
17 #include "google/protobuf/io/zero_copy_stream_impl.h"
18 #include "google/protobuf/message_lite.h"
19 
20 // Must be included last.
21 #include "google/protobuf/port_def.inc"
22 
23 namespace google {
24 namespace protobuf {
25 namespace util {
26 
27 // Write a single size-delimited message to the given stream. Delimited
28 // format allows a single file or stream to contain multiple messages,
29 // whereas normally writing multiple non-delimited messages to the same
30 // stream would cause them to be merged. A delimited message is a varint
31 // encoding the message size followed by a message of exactly that size.
32 //
33 // Note that if you want to *read* a delimited message from a file descriptor
34 // or istream, you will need to construct an io::FileInputStream or
35 // io::OstreamInputStream (implementations of io::ZeroCopyStream) and use the
36 // utility function ParseDelimitedFromZeroCopyStream(). You must then
37 // continue to use the same ZeroCopyInputStream to read all further data from
38 // the stream until EOF. This is because these ZeroCopyInputStream
39 // implementations are buffered: they read a big chunk of data at a time,
40 // then parse it. As a result, they may read past the end of the delimited
41 // message. There is no way for them to push the extra data back into the
42 // underlying source, so instead you must keep using the same stream object.
43 bool PROTOBUF_EXPORT SerializeDelimitedToFileDescriptor(
44     const MessageLite& message, int file_descriptor);
45 
46 bool PROTOBUF_EXPORT SerializeDelimitedToOstream(const MessageLite& message,
47                                                  std::ostream* output);
48 
49 // Read 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 parsing consumes the entire input. A delimited message
52 // is a varint encoding the message size followed by a message of exactly
53 // that size.
54 //
55 // If |clean_eof| is not NULL, then it will be set to indicate whether the
56 // stream ended cleanly. That is, if the stream ends without this method
57 // having read any data at all from it, then *clean_eof will be set true,
58 // otherwise it will be set false. Note that these methods return false
59 // on EOF, but they also return false on other errors, so |clean_eof| is
60 // needed to distinguish a clean end from errors.
61 bool PROTOBUF_EXPORT ParseDelimitedFromZeroCopyStream(
62     MessageLite* message, io::ZeroCopyInputStream* input, bool* clean_eof);
63 
64 bool PROTOBUF_EXPORT ParseDelimitedFromCodedStream(MessageLite* message,
65                                                    io::CodedInputStream* input,
66                                                    bool* clean_eof);
67 
68 // Write a single size-delimited message from the given stream. Delimited
69 // format allows a single file or stream to contain multiple messages,
70 // whereas normally writing multiple non-delimited messages to the same
71 // stream would cause them to be merged. A delimited message is a varint
72 // encoding the message size followed by a message of exactly that size.
73 bool PROTOBUF_EXPORT SerializeDelimitedToZeroCopyStream(
74     const MessageLite& message, io::ZeroCopyOutputStream* output);
75 
76 bool PROTOBUF_EXPORT SerializeDelimitedToCodedStream(
77     const MessageLite& message, io::CodedOutputStream* output);
78 
79 }  // namespace util
80 }  // namespace protobuf
81 }  // namespace google
82 
83 #include "google/protobuf/port_undef.inc"
84 
85 #endif  // GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
86