libUTL++
MemStream.h
1 #pragma once
2 
4 
5 #include <libutl/Stream.h>
6 #include <libutl/Vector.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
14 class BinaryData;
15 
17 
38 
40 class MemStream : public Stream
41 {
43 
44 public:
50  MemStream(size_t size, size_t increment = size_t_max)
51  {
52  init(nullptr, size, true, increment);
53  }
54 
62  MemStream(byte_t* array, size_t size, bool owner = true, size_t increment = size_t_max)
63  {
64  init(array, size, owner, increment);
65  }
66 
67  virtual void
69  {
70  clear();
71  }
72 
73  virtual size_t read(byte_t* array, size_t maxBytes, size_t minBytes = size_t_max);
74 
75  virtual void write(const byte_t* array, size_t num);
76 
78  const byte_t*
79  get() const
80  {
81  return _array.get();
82  }
83 
85  size_t
86  arraySize() const
87  {
88  return _array.size();
89  }
90 
92  void takeData(BinaryData& data);
93 
95  char* copyString() const;
96 
98  char* takeString();
99 
101  void
102  seekg(size_t pos)
103  {
104  ASSERTD(pos <= _array.size());
105  _readPos = pos;
106  }
107 
109  void
110  seekp(size_t pos)
111  {
112  _writePos = pos;
113  if (_writePos > _array.size())
114  {
115  _array.grow(_writePos);
116  }
117  }
118 
120  size_t
121  tellg() const
122  {
123  return _readPos;
124  }
125 
127  size_t
128  tellp() const
129  {
130  return _writePos;
131  }
132 
138  void
139  reserve(size_t size, size_t increment = size_t_max)
140  {
141  _array.reserve(size, increment);
142  }
143 
148  void truncate(size_t size);
149 
157  void set(byte_t* array, size_t size, bool owner = true, size_t increment = size_t_max);
158 
160  void
161  setOwner(bool owner)
162  {
163  _array.setOwner(owner);
164  }
165 
167  void
168  setIncrement(size_t increment)
169  {
170  _array.setIncrement(increment);
171  }
172 
173 protected:
174  virtual void
175  clear()
176  {
177  set(nullptr, 0);
178  }
179 
180 private:
181  void init(byte_t* array = nullptr,
182  size_t size = KB(4),
183  bool owner = true,
184  size_t increment = size_t_max);
185  void
186  deInit()
187  {
188  }
189 
190 private:
191  Vector<byte_t> _array;
192  size_t _readPos;
193  size_t _writePos;
194 };
195 
197 
198 UTL_NS_END;
MemStream(size_t size, size_t increment=size_t_max)
Constructor.
Definition: MemStream.h:50
virtual void close()
Close the stream.
Definition: MemStream.h:68
Vector of bytes.
Definition: BinaryData.h:22
void deInit()
De-initialize UTL++.
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
unsigned char byte_t
Unsigned character.
Definition: types.h:31
void seekg(size_t pos)
Seek the "get pointer" to the given position.
Definition: MemStream.h:102
void seekp(size_t pos)
Seek the "put pointer" to the given position.
Definition: MemStream.h:110
void setOwner(bool owner)
Set ownership flag.
Definition: MemStream.h:161
const size_t size_t_max
Maximum size_t value.
size_t tellg() const
Get the position of the "get pointer".
Definition: MemStream.h:121
Memory stream.
Definition: MemStream.h:40
MemStream(byte_t *array, size_t size, bool owner=true, size_t increment=size_t_max)
Constructor.
Definition: MemStream.h:62
Stream I/O abstraction.
Definition: Stream.h:68
void reserve(size_t size, size_t increment=size_t_max)
Reserve allocation space.
Definition: MemStream.h:139
constexpr size_t KB(size_t n)
Convert size in kilobytes to size in bytes.
Definition: util_inl.h:1008
void setIncrement(size_t increment)
Set the growth increment.
Definition: MemStream.h:168
size_t tellp() const
Get the position of the "put pointer".
Definition: MemStream.h:128
size_t arraySize() const
Get size of allocated array.
Definition: MemStream.h:86
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.