All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
memorystream.h
1 #ifndef RAPIDJSON_MEMORYSTREAM_H_
2 #define RAPIDJSON_MEMORYSTREAM_H_
3 
4 #include "rapidjson.h"
5 
6 namespace rapidjson {
7 
8 //! Represents an in-memory input byte stream.
9 /*!
10  This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream.
11 
12  It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file.
13 
14  Differences between MemoryStream and StringStream:
15  1. StringStream has encoding but MemoryStream is a byte stream.
16  2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source.
17  3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4().
18  \note implements Stream concept
19 */
20 struct MemoryStream {
21  typedef char Ch; // byte
22 
23  MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
24 
25  Ch Peek() const { return *src_; }
26  Ch Take() { return (src_ == end_) ? '\0' : *src_++; }
27  size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
28 
29  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
30  void Put(Ch) { RAPIDJSON_ASSERT(false); }
31  void Flush() { RAPIDJSON_ASSERT(false); }
32  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
33 
34  // For encoding detection only.
35  const Ch* Peek4() const {
36  return Tell() + 4 <= size_ ? src_ : 0;
37  }
38 
39  const Ch* src_; //!< Current read position.
40  const Ch* begin_; //!< Original head of the string.
41  const Ch* end_; //!< End of stream.
42  size_t size_; //!< Size of the stream.
43 };
44 
45 } // namespace rapidjson
46 
47 #endif // RAPIDJSON_MEMORYBUFFER_H_