PLCnext API Documentation  22.9.0.33
MemoryStream.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Commons/Io/Stream.hpp"
9 #include "Arp/System/Commons/Exceptions/Exceptions.h"
10 #include <vector>
11 
12 namespace Arp { namespace System { namespace Commons { namespace Io
13 {
14 
15 class MemoryStream : public Stream
16 {
17 public: // typedefs
18 
19 public: // construction/destruction
21  MemoryStream(void);
25  MemoryStream(size_t capacity);
29  MemoryStream(byte* buffer, size_t bufferSize);
34  MemoryStream(byte* buffer, size_t bufferSize, boolean writable);
42  MemoryStream(byte* buffer, size_t bufferSize, size_t index, size_t count);
51  MemoryStream(byte* buffer, size_t bufferSize, size_t index, size_t count, boolean writable);
61  MemoryStream(byte* buffer, size_t bufferSize, size_t index, size_t count, boolean writable, boolean publiclyVisible);
63  MemoryStream(const MemoryStream& arg) = delete;
65  MemoryStream& operator=(const MemoryStream& arg) = delete;
67  virtual ~MemoryStream(void);
68 
69 public: // operators
70 
71 public: // static operations
72 
73 public: // setter/getter operations
74  boolean CanRead(void) override;
75  boolean CanWrite(void) override;
76  boolean CanSeek(void) override;
77  size_t GetLength(void) override;
78  void SetLength(size_t argLength) override;
79  size_t GetPosition(void) override;
80  void SetPosition(size_t argPostion) override;
81 
82  virtual size_t GetCapacity(void);
83  virtual void SetCapacity(size_t argCapacity);
84 
85 public: // operations
86  void Flush(void) override;
87  size_t Seek(size_t offset, SeekOrigin origin) override;
88  size_t Read(byte* pBuffer, size_t bufferSize, size_t offset, size_t count) override;
89  std::vector<byte> ToArray(void);
90  void Write(const byte* pBuffer, size_t bufferSize, size_t offset, size_t count) override;
91 
92  virtual void WriteTo(Stream& stream);
93 
94 protected: // operations
95  void InternalDispose(void) override;
96 
97 private: // static methods
98 
99 private: // methods
100  void InternalConstructor(byte* buffer, size_t bufferSize, size_t index, size_t count, boolean writable, boolean publicallyVisible);
101  void EnsureSize(size_t newSize);
102 
103 private: // fields
104  // The underlying buffer of this stream.
105  byte* streamBuffer;
106  size_t streamBufferSize;
107  // True if the stream is writeable
108  boolean writeable;
109  // The actual position of this stream
110  size_t position;
111  // The actual length of the stream.
112  size_t length;
113  // Is true, if the stream is expandable
114  boolean expandable;
115  // The capacity of the stream
116  size_t capacity;
117  // The initial start index in the buffer
118  size_t bufferOffset;
119  // Is true if it is allowed to get the underlying buffer
120  boolean allowGetBuffer;
121  // Is true, the buffer created by the memory stream self
122  boolean selfCreated;
123 
124 private: // static fields
125  static size_t minCapacity;
126 };
127 
129 // inline methods of class MemoryStream
131  : MemoryStream(0)
132 {
133 }
134 
135 inline MemoryStream::MemoryStream(size_t capacity)
136  : streamBuffer(nullptr), writeable(true), position(0), length(0), expandable(false), capacity(capacity), bufferOffset(0), allowGetBuffer(true), selfCreated(true)
137 {
138  if (capacity < 0)
139  {
140  throw ArgumentException("{0}: capacity must not be less than 0", __FUNCTION__);
141  }
142 
143  this->streamBuffer = new byte[capacity];
144  this->streamBufferSize = capacity;
145  memset(this->streamBuffer, 0, capacity);
146 }
147 
148 inline MemoryStream::MemoryStream(byte* buffer, size_t bufferSize)
149  : streamBuffer(nullptr), writeable(true), position(0), length(0), expandable(false), capacity(0), bufferOffset(0), allowGetBuffer(true), selfCreated(false)
150 {
151  if (buffer == nullptr)
152  {
153  throw ArgumentNullException("{0}: buffer", __FUNCTION__);
154  }
155 
156  this->InternalConstructor(buffer, bufferSize, 0, bufferSize, true, false);
157 }
158 
159 inline MemoryStream::MemoryStream(byte* buffer, size_t bufferSize, boolean writable)
160  : streamBuffer(nullptr), writeable(true), position(0), length(0), expandable(false), capacity(0), bufferOffset(0), allowGetBuffer(true), selfCreated(false)
161 {
162  if (buffer == nullptr)
163  {
164  throw ArgumentNullException("{0}: buffer", __FUNCTION__);
165  }
166 
167  this->InternalConstructor(buffer, bufferSize, 0, sizeof(buffer), writable, false);
168 }
169 
170 inline MemoryStream::MemoryStream(byte* buffer, size_t bufferSize, size_t index, size_t count)
171  : streamBuffer(nullptr), writeable(true), position(0), length(0), expandable(false), capacity(0), bufferOffset(0), allowGetBuffer(true), selfCreated(false)
172 {
173  if (buffer == nullptr)
174  {
175  throw ArgumentNullException("{0}: buffer", __FUNCTION__);
176  }
177 
178  this->InternalConstructor(buffer, bufferSize, index, count, true, false);
179 }
180 
181 inline MemoryStream::MemoryStream(byte* buffer, size_t bufferSize, size_t index, size_t count, boolean writable)
182  : streamBuffer(nullptr), writeable(true), position(0), length(0), expandable(false), capacity(0), bufferOffset(0), allowGetBuffer(true), selfCreated(false)
183 {
184  this->InternalConstructor(buffer, bufferSize, index, count, writable, false);
185 }
186 
187 inline MemoryStream::MemoryStream(byte* buffer, size_t bufferSize, size_t index, size_t count, boolean writable, boolean publiclyVisible)
188  : streamBuffer(nullptr), writeable(true), position(0), length(0), expandable(false), capacity(0), bufferOffset(0), allowGetBuffer(true), selfCreated(false)
189 {
190  if (buffer == nullptr)
191  {
192  throw ArgumentNullException("{0}: buffer", __FUNCTION__);
193  }
194 
195  this->InternalConstructor(buffer, bufferSize, index, count, writable, publiclyVisible);
196 }
197 
199 {
200  this->Dispose();
201 }
202 
203 }}}} // end of namespace Arp::System::Commons::Io
This exception is used when an invalid argument occurs.
Definition: ArgumentException.hpp:15
This exception is used when an invalid <null> argument occurs.
Definition: ArgumentNullException.hpp:16
Definition: MemoryStream.hpp:16
MemoryStream(const MemoryStream &arg)=delete
Copy constructor.
virtual ~MemoryStream(void)
Destructs this instance and frees all resources.
Definition: MemoryStream.hpp:198
MemoryStream & operator=(const MemoryStream &arg)=delete
Assignment operator.
MemoryStream(void)
Initializes a new instance of the T:System.IO.MemoryStream class with an expandable capacity initiali...
Definition: MemoryStream.hpp:130
Provides a generic view of a sequence of bytes.
Definition: Stream.hpp:20
@ System
System components used by the System, Device, Plc or Io domains.
SeekOrigin
Provides seek reference points. To seek to the end of a stream, call stream.Seek(0,...
Definition: SeekOrigin.hpp:20
Root namespace for the PLCnext API