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