zero_copy_stream_impl_lite.h
Go to the documentation of this file.
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 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // This file contains common implementations of the interfaces defined in
36 // zero_copy_stream.h which are included in the "lite" protobuf library.
37 // These implementations cover I/O on raw arrays and strings, as well as
38 // adaptors which make it easy to implement streams based on traditional
39 // streams. Of course, many users will probably want to write their own
40 // implementations of these interfaces specific to the particular I/O
41 // abstractions they prefer to use, but these should cover the most common
42 // cases.
43 
44 #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
45 #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
46 
47 #include <iosfwd>
48 #include <memory>
49 #include <string>
54 
55 
56 #include <google/protobuf/port_def.inc>
57 
58 namespace google {
59 namespace protobuf {
60 namespace io {
61 
62 // ===================================================================
63 
64 // A ZeroCopyInputStream backed by an in-memory array of bytes.
65 class PROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream {
66  public:
67  // Create an InputStream that returns the bytes pointed to by "data".
68  // "data" remains the property of the caller but must remain valid until
69  // the stream is destroyed. If a block_size is given, calls to Next()
70  // will return data blocks no larger than the given size. Otherwise, the
71  // first call to Next() returns the entire array. block_size is mainly
72  // useful for testing; in production you would probably never want to set
73  // it.
74  ArrayInputStream(const void* data, int size, int block_size = -1);
75  ~ArrayInputStream() override = default;
76 
77  // implements ZeroCopyInputStream ----------------------------------
78  bool Next(const void** data, int* size) override;
79  void BackUp(int count) override;
80  bool Skip(int count) override;
81  int64 ByteCount() const override;
82 
83 
84  private:
85  const uint8* const data_; // The byte array.
86  const int size_; // Total size of the array.
87  const int block_size_; // How many bytes to return at a time.
88 
89  int position_;
90  int last_returned_size_; // How many bytes we returned last time Next()
91  // was called (used for error checking only).
92 
94 };
95 
96 // ===================================================================
97 
98 // A ZeroCopyOutputStream backed by an in-memory array of bytes.
99 class PROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
100  public:
101  // Create an OutputStream that writes to the bytes pointed to by "data".
102  // "data" remains the property of the caller but must remain valid until
103  // the stream is destroyed. If a block_size is given, calls to Next()
104  // will return data blocks no larger than the given size. Otherwise, the
105  // first call to Next() returns the entire array. block_size is mainly
106  // useful for testing; in production you would probably never want to set
107  // it.
108  ArrayOutputStream(void* data, int size, int block_size = -1);
109  ~ArrayOutputStream() override = default;
110 
111  // implements ZeroCopyOutputStream ---------------------------------
112  bool Next(void** data, int* size) override;
113  void BackUp(int count) override;
114  int64 ByteCount() const override;
115 
116  private:
117  uint8* const data_; // The byte array.
118  const int size_; // Total size of the array.
119  const int block_size_; // How many bytes to return at a time.
120 
122  int last_returned_size_; // How many bytes we returned last time Next()
123  // was called (used for error checking only).
124 
126 };
127 
128 // ===================================================================
129 
130 // A ZeroCopyOutputStream which appends bytes to a string.
131 class PROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
132  public:
133  // Create a StringOutputStream which appends bytes to the given string.
134  // The string remains property of the caller, but it is mutated in arbitrary
135  // ways and MUST NOT be accessed in any way until you're done with the
136  // stream. Either be sure there's no further usage, or (safest) destroy the
137  // stream before using the contents.
138  //
139  // Hint: If you call target->reserve(n) before creating the stream,
140  // the first call to Next() will return at least n bytes of buffer
141  // space.
143  ~StringOutputStream() override = default;
144 
145  // implements ZeroCopyOutputStream ---------------------------------
146  bool Next(void** data, int* size) override;
147  void BackUp(int count) override;
148  int64 ByteCount() const override;
149 
150  private:
151  static const int kMinimumSize = 16;
152 
154 
156 };
157 
158 // Note: There is no StringInputStream. Instead, just create an
159 // ArrayInputStream as follows:
160 // ArrayInputStream input(str.data(), str.size());
161 
162 // ===================================================================
163 
164 // A generic traditional input stream interface.
165 //
166 // Lots of traditional input streams (e.g. file descriptors, C stdio
167 // streams, and C++ iostreams) expose an interface where every read
168 // involves copying bytes into a buffer. If you want to take such an
169 // interface and make a ZeroCopyInputStream based on it, simply implement
170 // CopyingInputStream and then use CopyingInputStreamAdaptor.
171 //
172 // CopyingInputStream implementations should avoid buffering if possible.
173 // CopyingInputStreamAdaptor does its own buffering and will read data
174 // in large blocks.
175 class PROTOBUF_EXPORT CopyingInputStream {
176  public:
177  virtual ~CopyingInputStream() {}
178 
179  // Reads up to "size" bytes into the given buffer. Returns the number of
180  // bytes read. Read() waits until at least one byte is available, or
181  // returns zero if no bytes will ever become available (EOF), or -1 if a
182  // permanent read error occurred.
183  virtual int Read(void* buffer, int size) = 0;
184 
185  // Skips the next "count" bytes of input. Returns the number of bytes
186  // actually skipped. This will always be exactly equal to "count" unless
187  // EOF was reached or a permanent read error occurred.
188  //
189  // The default implementation just repeatedly calls Read() into a scratch
190  // buffer.
191  virtual int Skip(int count);
192 };
193 
194 // A ZeroCopyInputStream which reads from a CopyingInputStream. This is
195 // useful for implementing ZeroCopyInputStreams that read from traditional
196 // streams. Note that this class is not really zero-copy.
197 //
198 // If you want to read from file descriptors or C++ istreams, this is
199 // already implemented for you: use FileInputStream or IstreamInputStream
200 // respectively.
201 class PROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream {
202  public:
203  // Creates a stream that reads from the given CopyingInputStream.
204  // If a block_size is given, it specifies the number of bytes that
205  // should be read and returned with each call to Next(). Otherwise,
206  // a reasonable default is used. The caller retains ownership of
207  // copying_stream unless SetOwnsCopyingStream(true) is called.
208  explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream,
209  int block_size = -1);
210  ~CopyingInputStreamAdaptor() override;
211 
212  // Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to
213  // delete the underlying CopyingInputStream when it is destroyed.
214  void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
215 
216  // implements ZeroCopyInputStream ----------------------------------
217  bool Next(const void** data, int* size) override;
218  void BackUp(int count) override;
219  bool Skip(int count) override;
220  int64 ByteCount() const override;
221 
222  private:
223  // Insures that buffer_ is not NULL.
224  void AllocateBufferIfNeeded();
225  // Frees the buffer and resets buffer_used_.
226  void FreeBuffer();
227 
228  // The underlying copying stream.
231 
232  // True if we have seen a permenant error from the underlying stream.
233  bool failed_;
234 
235  // The current position of copying_stream_, relative to the point where
236  // we started reading.
238 
239  // Data is read into this buffer. It may be NULL if no buffer is currently
240  // in use. Otherwise, it points to an array of size buffer_size_.
241  std::unique_ptr<uint8[]> buffer_;
242  const int buffer_size_;
243 
244  // Number of valid bytes currently in the buffer (i.e. the size last
245  // returned by Next()). 0 <= buffer_used_ <= buffer_size_.
247 
248  // Number of bytes in the buffer which were backed up over by a call to
249  // BackUp(). These need to be returned again.
250  // 0 <= backup_bytes_ <= buffer_used_
252 
254 };
255 
256 // ===================================================================
257 
258 // A generic traditional output stream interface.
259 //
260 // Lots of traditional output streams (e.g. file descriptors, C stdio
261 // streams, and C++ iostreams) expose an interface where every write
262 // involves copying bytes from a buffer. If you want to take such an
263 // interface and make a ZeroCopyOutputStream based on it, simply implement
264 // CopyingOutputStream and then use CopyingOutputStreamAdaptor.
265 //
266 // CopyingOutputStream implementations should avoid buffering if possible.
267 // CopyingOutputStreamAdaptor does its own buffering and will write data
268 // in large blocks.
269 class PROTOBUF_EXPORT CopyingOutputStream {
270  public:
271  virtual ~CopyingOutputStream() {}
272 
273  // Writes "size" bytes from the given buffer to the output. Returns true
274  // if successful, false on a write error.
275  virtual bool Write(const void* buffer, int size) = 0;
276 };
277 
278 // A ZeroCopyOutputStream which writes to a CopyingOutputStream. This is
279 // useful for implementing ZeroCopyOutputStreams that write to traditional
280 // streams. Note that this class is not really zero-copy.
281 //
282 // If you want to write to file descriptors or C++ ostreams, this is
283 // already implemented for you: use FileOutputStream or OstreamOutputStream
284 // respectively.
285 class PROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream {
286  public:
287  // Creates a stream that writes to the given Unix file descriptor.
288  // If a block_size is given, it specifies the size of the buffers
289  // that should be returned by Next(). Otherwise, a reasonable default
290  // is used.
291  explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream,
292  int block_size = -1);
293  ~CopyingOutputStreamAdaptor() override;
294 
295  // Writes all pending data to the underlying stream. Returns false if a
296  // write error occurred on the underlying stream. (The underlying
297  // stream itself is not necessarily flushed.)
298  bool Flush();
299 
300  // Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to
301  // delete the underlying CopyingOutputStream when it is destroyed.
302  void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
303 
304  // implements ZeroCopyOutputStream ---------------------------------
305  bool Next(void** data, int* size) override;
306  void BackUp(int count) override;
307  int64 ByteCount() const override;
308 
309  private:
310  // Write the current buffer, if it is present.
311  bool WriteBuffer();
312  // Insures that buffer_ is not NULL.
313  void AllocateBufferIfNeeded();
314  // Frees the buffer.
315  void FreeBuffer();
316 
317  // The underlying copying stream.
320 
321  // True if we have seen a permenant error from the underlying stream.
322  bool failed_;
323 
324  // The current position of copying_stream_, relative to the point where
325  // we started writing.
327 
328  // Data is written from this buffer. It may be NULL if no buffer is
329  // currently in use. Otherwise, it points to an array of size buffer_size_.
330  std::unique_ptr<uint8[]> buffer_;
331  const int buffer_size_;
332 
333  // Number of valid bytes currently in the buffer (i.e. the size last
334  // returned by Next()). When BackUp() is called, we just reduce this.
335  // 0 <= buffer_used_ <= buffer_size_.
337 
339 };
340 
341 // ===================================================================
342 
343 // A ZeroCopyInputStream which wraps some other stream and limits it to
344 // a particular byte count.
345 class PROTOBUF_EXPORT LimitingInputStream : public ZeroCopyInputStream {
346  public:
348  ~LimitingInputStream() override;
349 
350  // implements ZeroCopyInputStream ----------------------------------
351  bool Next(const void** data, int* size) override;
352  void BackUp(int count) override;
353  bool Skip(int count) override;
354  int64 ByteCount() const override;
355 
356 
357  private:
359  int64 limit_; // Decreases as we go, becomes negative if we overshoot.
360  int64 prior_bytes_read_; // Bytes read on underlying stream at construction
361 
363 };
364 
365 
366 // ===================================================================
367 
368 // mutable_string_data() and as_string_data() are workarounds to improve
369 // the performance of writing new data to an existing string. Unfortunately
370 // the methods provided by the string class are suboptimal, and using memcpy()
371 // is mildly annoying because it requires its pointer args to be non-NULL even
372 // if we ask it to copy 0 bytes. Furthermore, string_as_array() has the
373 // property that it always returns NULL if its arg is the empty string, exactly
374 // what we want to avoid if we're using it in conjunction with memcpy()!
375 // With C++11, the desired memcpy() boils down to memcpy(..., &(*s)[0], size),
376 // where s is a string*. Without C++11, &(*s)[0] is not guaranteed to be safe,
377 // so we use string_as_array(), and live with the extra logic that tests whether
378 // *s is empty.
379 
380 // Return a pointer to mutable characters underlying the given string. The
381 // return value is valid until the next time the string is resized. We
382 // trust the caller to treat the return value as an array of length s->size().
384  // This should be simpler & faster than string_as_array() because the latter
385  // is guaranteed to return NULL when *s is empty, so it has to check for that.
386  return &(*s)[0];
387 }
388 
389 // as_string_data(s) is equivalent to
390 // ({ char* p = mutable_string_data(s); make_pair(p, p != NULL); })
391 // Sometimes it's faster: in some scenarios p cannot be NULL, and then the
392 // code can avoid that check.
393 inline std::pair<char*, bool> as_string_data(std::string* s) {
394  char* p = mutable_string_data(s);
395  return std::make_pair(p, true);
396 }
397 
398 } // namespace io
399 } // namespace protobuf
400 } // namespace google
401 
402 #include <google/protobuf/port_undef.inc>
403 
404 #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
zero_copy_stream.h
google::protobuf::io::CopyingOutputStream
Definition: zero_copy_stream_impl_lite.h:269
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
google::protobuf::io::CopyingInputStreamAdaptor::buffer_
std::unique_ptr< uint8[]> buffer_
Definition: zero_copy_stream_impl_lite.h:241
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf::io::CopyingInputStreamAdaptor::failed_
bool failed_
Definition: zero_copy_stream_impl_lite.h:233
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
google::protobuf::io::ArrayInputStream::position_
int position_
Definition: zero_copy_stream_impl_lite.h:89
input
std::string input
Definition: tokenizer_unittest.cc:197
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf::io::ArrayInputStream::data_
const uint8 *const data_
Definition: zero_copy_stream_impl_lite.h:85
google::protobuf::io::mutable_string_data
char * mutable_string_data(std::string *s)
Definition: zero_copy_stream_impl_lite.h:383
google::protobuf::io::as_string_data
std::pair< char *, bool > as_string_data(std::string *s)
Definition: zero_copy_stream_impl_lite.h:393
s
XmlRpcServer s
google::protobuf::io::StringOutputStream::target_
std::string * target_
Definition: zero_copy_stream_impl_lite.h:153
google::protobuf::io::CopyingOutputStreamAdaptor::position_
int64 position_
Definition: zero_copy_stream_impl_lite.h:326
google::protobuf::io::LimitingInputStream::limit_
int64 limit_
Definition: zero_copy_stream_impl_lite.h:359
google::protobuf::io::ArrayInputStream::block_size_
const int block_size_
Definition: zero_copy_stream_impl_lite.h:87
google::protobuf::io::CopyingOutputStreamAdaptor::owns_copying_stream_
bool owns_copying_stream_
Definition: zero_copy_stream_impl_lite.h:319
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
target
GLenum target
Definition: glcorearb.h:3739
google::protobuf::io::LimitingInputStream::prior_bytes_read_
int64 prior_bytes_read_
Definition: zero_copy_stream_impl_lite.h:360
callback.h
google::protobuf::io::CopyingInputStreamAdaptor::owns_copying_stream_
bool owns_copying_stream_
Definition: zero_copy_stream_impl_lite.h:230
google::protobuf::io::CopyingInputStreamAdaptor::SetOwnsCopyingStream
void SetOwnsCopyingStream(bool value)
Definition: zero_copy_stream_impl_lite.h:214
google::protobuf::io::ArrayOutputStream::data_
uint8 *const data_
Definition: zero_copy_stream_impl_lite.h:117
testing::internal::posix::Read
int Read(int fd, void *buf, unsigned int count)
Definition: gtest-port.h:2126
google::protobuf::io::ArrayOutputStream::position_
int position_
Definition: zero_copy_stream_impl_lite.h:121
google::protobuf::io::CopyingInputStreamAdaptor::buffer_size_
const int buffer_size_
Definition: zero_copy_stream_impl_lite.h:242
google::protobuf::io::CopyingOutputStreamAdaptor::failed_
bool failed_
Definition: zero_copy_stream_impl_lite.h:322
google::protobuf::io::CopyingOutputStreamAdaptor::SetOwnsCopyingStream
void SetOwnsCopyingStream(bool value)
Definition: zero_copy_stream_impl_lite.h:302
p
const char * p
Definition: gmock-matchers_test.cc:3863
google::protobuf::io::ArrayOutputStream
Definition: zero_copy_stream_impl_lite.h:99
google::protobuf::io::ArrayOutputStream::last_returned_size_
int last_returned_size_
Definition: zero_copy_stream_impl_lite.h:122
google::protobuf::io::ArrayInputStream
Definition: zero_copy_stream_impl_lite.h:65
google::protobuf::io::CopyingOutputStreamAdaptor::copying_stream_
CopyingOutputStream * copying_stream_
Definition: zero_copy_stream_impl_lite.h:318
buffer
Definition: buffer_processor.h:43
google::protobuf::io::CopyingOutputStreamAdaptor
Definition: zero_copy_stream_impl_lite.h:285
google::protobuf::io::ArrayInputStream::size_
const int size_
Definition: zero_copy_stream_impl_lite.h:86
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
google::protobuf::io::CopyingOutputStream::~CopyingOutputStream
virtual ~CopyingOutputStream()
Definition: zero_copy_stream_impl_lite.h:271
google::protobuf::io::CopyingOutputStreamAdaptor::buffer_used_
int buffer_used_
Definition: zero_copy_stream_impl_lite.h:336
testing::internal::posix::Write
int Write(int fd, const void *buf, unsigned int count)
Definition: gtest-port.h:2129
google::protobuf::io::StringOutputStream
Definition: zero_copy_stream_impl_lite.h:131
common.h
pump.Skip
def Skip(lines, pos, regex)
Definition: pump.py:261
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf::io::ArrayOutputStream::size_
const int size_
Definition: zero_copy_stream_impl_lite.h:118
stl_util.h
google::protobuf::io::LimitingInputStream::input_
ZeroCopyInputStream * input_
Definition: zero_copy_stream_impl_lite.h:358
google::protobuf::io::CopyingInputStreamAdaptor::buffer_used_
int buffer_used_
Definition: zero_copy_stream_impl_lite.h:246
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
google::protobuf::io::LimitingInputStream
Definition: zero_copy_stream_impl_lite.h:345
google::protobuf::io::CopyingInputStreamAdaptor::copying_stream_
CopyingInputStream * copying_stream_
Definition: zero_copy_stream_impl_lite.h:229
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
google::protobuf::io::CopyingInputStream
Definition: zero_copy_stream_impl_lite.h:175
google::protobuf::io::CopyingInputStreamAdaptor::position_
int64 position_
Definition: zero_copy_stream_impl_lite.h:237
google::protobuf::io::CopyingInputStream::~CopyingInputStream
virtual ~CopyingInputStream()
Definition: zero_copy_stream_impl_lite.h:177
google::protobuf::io::CopyingOutputStreamAdaptor::buffer_
std::unique_ptr< uint8[]> buffer_
Definition: zero_copy_stream_impl_lite.h:330
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::io::ArrayInputStream::last_returned_size_
int last_returned_size_
Definition: zero_copy_stream_impl_lite.h:90
count
GLint GLsizei count
Definition: glcorearb.h:2830
google::protobuf::io::CopyingInputStreamAdaptor::backup_bytes_
int backup_bytes_
Definition: zero_copy_stream_impl_lite.h:251
google::protobuf::io::CopyingOutputStreamAdaptor::buffer_size_
const int buffer_size_
Definition: zero_copy_stream_impl_lite.h:331
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::io::ArrayOutputStream::block_size_
const int block_size_
Definition: zero_copy_stream_impl_lite.h:119
google::protobuf::io::CopyingInputStreamAdaptor
Definition: zero_copy_stream_impl_lite.h:201


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:02