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