You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bytestream.h 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. // This file declares the ByteSink and ByteSource abstract interfaces. These
  31. // interfaces represent objects that consume (ByteSink) or produce (ByteSource)
  32. // a sequence of bytes. Using these abstract interfaces in your APIs can help
  33. // make your code work with a variety of input and output types.
  34. //
  35. // This file also declares the following commonly used implementations of these
  36. // interfaces.
  37. //
  38. // ByteSink:
  39. // UncheckedArrayByteSink Writes to an array, without bounds checking
  40. // CheckedArrayByteSink Writes to an array, with bounds checking
  41. // GrowingArrayByteSink Allocates and writes to a growable buffer
  42. // StringByteSink Writes to an STL string
  43. // NullByteSink Consumes a never-ending stream of bytes
  44. //
  45. // ByteSource:
  46. // ArrayByteSource Reads from an array or string/StringPiece
  47. // LimitedByteSource Limits the number of bytes read from an
  48. #ifndef GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_
  49. #define GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_
  50. #include <stddef.h>
  51. #include <string>
  52. #include <google/protobuf/stubs/common.h>
  53. #include <google/protobuf/stubs/stringpiece.h>
  54. #include <google/protobuf/port_def.inc>
  55. class CordByteSink;
  56. namespace google
  57. {
  58. namespace protobuf
  59. {
  60. namespace strings
  61. {
  62. // An abstract interface for an object that consumes a sequence of bytes. This
  63. // interface offers a way to append data as well as a Flush() function.
  64. //
  65. // Example:
  66. //
  67. // string my_data;
  68. // ...
  69. // ByteSink* sink = ...
  70. // sink->Append(my_data.data(), my_data.size());
  71. // sink->Flush();
  72. //
  73. class PROTOBUF_EXPORT ByteSink
  74. {
  75. public:
  76. ByteSink()
  77. {
  78. }
  79. virtual ~ByteSink()
  80. {
  81. }
  82. // Appends the "n" bytes starting at "bytes".
  83. virtual void Append(const char* bytes, size_t n) = 0;
  84. // Flushes internal buffers. The default implementation does nothing. ByteSink
  85. // subclasses may use internal buffers that require calling Flush() at the end
  86. // of the stream.
  87. virtual void Flush();
  88. private:
  89. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ByteSink);
  90. };
  91. // An abstract interface for an object that produces a fixed-size sequence of
  92. // bytes.
  93. //
  94. // Example:
  95. //
  96. // ByteSource* source = ...
  97. // while (source->Available() > 0) {
  98. // StringPiece data = source->Peek();
  99. // ... do something with "data" ...
  100. // source->Skip(data.length());
  101. // }
  102. //
  103. class PROTOBUF_EXPORT ByteSource
  104. {
  105. public:
  106. ByteSource()
  107. {
  108. }
  109. virtual ~ByteSource()
  110. {
  111. }
  112. // Returns the number of bytes left to read from the source. Available()
  113. // should decrease by N each time Skip(N) is called. Available() may not
  114. // increase. Available() returning 0 indicates that the ByteSource is
  115. // exhausted.
  116. //
  117. // Note: Size() may have been a more appropriate name as it's more
  118. // indicative of the fixed-size nature of a ByteSource.
  119. virtual size_t Available() const = 0;
  120. // Returns a StringPiece of the next contiguous region of the source. Does not
  121. // reposition the source. The returned region is empty iff Available() == 0.
  122. //
  123. // The returned region is valid until the next call to Skip() or until this
  124. // object is destroyed, whichever occurs first.
  125. //
  126. // The length of the returned StringPiece will be <= Available().
  127. virtual StringPiece Peek() = 0;
  128. // Skips the next n bytes. Invalidates any StringPiece returned by a previous
  129. // call to Peek().
  130. //
  131. // REQUIRES: Available() >= n
  132. virtual void Skip(size_t n) = 0;
  133. // Writes the next n bytes in this ByteSource to the given ByteSink, and
  134. // advances this ByteSource past the copied bytes. The default implementation
  135. // of this method just copies the bytes normally, but subclasses might
  136. // override CopyTo to optimize certain cases.
  137. //
  138. // REQUIRES: Available() >= n
  139. virtual void CopyTo(ByteSink* sink, size_t n);
  140. private:
  141. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ByteSource);
  142. };
  143. //
  144. // Some commonly used implementations of ByteSink
  145. //
  146. // Implementation of ByteSink that writes to an unsized byte array. No
  147. // bounds-checking is performed--it is the caller's responsibility to ensure
  148. // that the destination array is large enough.
  149. //
  150. // Example:
  151. //
  152. // char buf[10];
  153. // UncheckedArrayByteSink sink(buf);
  154. // sink.Append("hi", 2); // OK
  155. // sink.Append(data, 100); // WOOPS! Overflows buf[10].
  156. //
  157. class PROTOBUF_EXPORT UncheckedArrayByteSink : public ByteSink
  158. {
  159. public:
  160. explicit UncheckedArrayByteSink(char* dest) :
  161. dest_(dest)
  162. {
  163. }
  164. virtual void Append(const char* data, size_t n) override;
  165. // Returns the current output pointer so that a caller can see how many bytes
  166. // were produced.
  167. //
  168. // Note: this method is not part of the ByteSink interface.
  169. char* CurrentDestination() const
  170. {
  171. return dest_;
  172. }
  173. private:
  174. char* dest_;
  175. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UncheckedArrayByteSink);
  176. };
  177. // Implementation of ByteSink that writes to a sized byte array. This sink will
  178. // not write more than "capacity" bytes to outbuf. Once "capacity" bytes are
  179. // appended, subsequent bytes will be ignored and Overflowed() will return true.
  180. // Overflowed() does not cause a runtime error (i.e., it does not CHECK fail).
  181. //
  182. // Example:
  183. //
  184. // char buf[10];
  185. // CheckedArrayByteSink sink(buf, 10);
  186. // sink.Append("hi", 2); // OK
  187. // sink.Append(data, 100); // Will only write 8 more bytes
  188. //
  189. class PROTOBUF_EXPORT CheckedArrayByteSink : public ByteSink
  190. {
  191. public:
  192. CheckedArrayByteSink(char* outbuf, size_t capacity);
  193. virtual void Append(const char* bytes, size_t n) override;
  194. // Returns the number of bytes actually written to the sink.
  195. size_t NumberOfBytesWritten() const
  196. {
  197. return size_;
  198. }
  199. // Returns true if any bytes were discarded, i.e., if there was an
  200. // attempt to write more than 'capacity' bytes.
  201. bool Overflowed() const
  202. {
  203. return overflowed_;
  204. }
  205. private:
  206. char* outbuf_;
  207. const size_t capacity_;
  208. size_t size_;
  209. bool overflowed_;
  210. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CheckedArrayByteSink);
  211. };
  212. // Implementation of ByteSink that allocates an internal buffer (a char array)
  213. // and expands it as needed to accommodate appended data (similar to a string),
  214. // and allows the caller to take ownership of the internal buffer via the
  215. // GetBuffer() method. The buffer returned from GetBuffer() must be deleted by
  216. // the caller with delete[]. GetBuffer() also sets the internal buffer to be
  217. // empty, and subsequent appends to the sink will create a new buffer. The
  218. // destructor will free the internal buffer if GetBuffer() was not called.
  219. //
  220. // Example:
  221. //
  222. // GrowingArrayByteSink sink(10);
  223. // sink.Append("hi", 2);
  224. // sink.Append(data, n);
  225. // const char* buf = sink.GetBuffer(); // Ownership transferred
  226. // delete[] buf;
  227. //
  228. class PROTOBUF_EXPORT GrowingArrayByteSink : public strings::ByteSink
  229. {
  230. public:
  231. explicit GrowingArrayByteSink(size_t estimated_size);
  232. virtual ~GrowingArrayByteSink();
  233. virtual void Append(const char* bytes, size_t n) override;
  234. // Returns the allocated buffer, and sets nbytes to its size. The caller takes
  235. // ownership of the buffer and must delete it with delete[].
  236. char* GetBuffer(size_t* nbytes);
  237. private:
  238. void Expand(size_t amount);
  239. void ShrinkToFit();
  240. size_t capacity_;
  241. char* buf_;
  242. size_t size_;
  243. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GrowingArrayByteSink);
  244. };
  245. // Implementation of ByteSink that appends to the given string.
  246. // Existing contents of "dest" are not modified; new data is appended.
  247. //
  248. // Example:
  249. //
  250. // string dest = "Hello ";
  251. // StringByteSink sink(&dest);
  252. // sink.Append("World", 5);
  253. // assert(dest == "Hello World");
  254. //
  255. class PROTOBUF_EXPORT StringByteSink : public ByteSink
  256. {
  257. public:
  258. explicit StringByteSink(std::string* dest) :
  259. dest_(dest)
  260. {
  261. }
  262. virtual void Append(const char* data, size_t n) override;
  263. private:
  264. std::string* dest_;
  265. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringByteSink);
  266. };
  267. // Implementation of ByteSink that discards all data.
  268. //
  269. // Example:
  270. //
  271. // NullByteSink sink;
  272. // sink.Append(data, data.size()); // All data ignored.
  273. //
  274. class PROTOBUF_EXPORT NullByteSink : public ByteSink
  275. {
  276. public:
  277. NullByteSink()
  278. {
  279. }
  280. void Append(const char* /*data*/, size_t /*n*/) override
  281. {
  282. }
  283. private:
  284. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(NullByteSink);
  285. };
  286. //
  287. // Some commonly used implementations of ByteSource
  288. //
  289. // Implementation of ByteSource that reads from a StringPiece.
  290. //
  291. // Example:
  292. //
  293. // string data = "Hello";
  294. // ArrayByteSource source(data);
  295. // assert(source.Available() == 5);
  296. // assert(source.Peek() == "Hello");
  297. //
  298. class PROTOBUF_EXPORT ArrayByteSource : public ByteSource
  299. {
  300. public:
  301. explicit ArrayByteSource(StringPiece s) :
  302. input_(s)
  303. {
  304. }
  305. virtual size_t Available() const override;
  306. virtual StringPiece Peek() override;
  307. virtual void Skip(size_t n) override;
  308. private:
  309. StringPiece input_;
  310. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayByteSource);
  311. };
  312. // Implementation of ByteSource that wraps another ByteSource, limiting the
  313. // number of bytes returned.
  314. //
  315. // The caller maintains ownership of the underlying source, and may not use the
  316. // underlying source while using the LimitByteSource object. The underlying
  317. // source's pointer is advanced by n bytes every time this LimitByteSource
  318. // object is advanced by n.
  319. //
  320. // Example:
  321. //
  322. // string data = "Hello World";
  323. // ArrayByteSource abs(data);
  324. // assert(abs.Available() == data.size());
  325. //
  326. // LimitByteSource limit(abs, 5);
  327. // assert(limit.Available() == 5);
  328. // assert(limit.Peek() == "Hello");
  329. //
  330. class PROTOBUF_EXPORT LimitByteSource : public ByteSource
  331. {
  332. public:
  333. // Returns at most "limit" bytes from "source".
  334. LimitByteSource(ByteSource* source, size_t limit);
  335. virtual size_t Available() const override;
  336. virtual StringPiece Peek() override;
  337. virtual void Skip(size_t n) override;
  338. // We override CopyTo so that we can forward to the underlying source, in
  339. // case it has an efficient implementation of CopyTo.
  340. virtual void CopyTo(ByteSink* sink, size_t n) override;
  341. private:
  342. ByteSource* source_;
  343. size_t limit_;
  344. };
  345. } // namespace strings
  346. } // namespace protobuf
  347. } // namespace google
  348. #include <google/protobuf/port_undef.inc>
  349. #endif // GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_