14 struct MemoryStream::MemoryStreamImpl {
15 std::vector<char> m_buffer;
20 MemoryStreamImpl() : m_position(0), m_writable(true), m_disposed(false) {}
21 MemoryStreamImpl(
bool writable) : m_position(0), m_writable(writable), m_disposed(false) {}
43 return !m_pImpl->m_disposed;
47 return !m_pImpl->m_disposed;
51 return m_pImpl->m_writable && !m_pImpl->m_disposed;
56 if (m_pImpl->m_disposed)
throw IOException(
"Stream is closed.");
57 return static_cast<long>(m_pImpl->m_buffer.size());
62 if (m_pImpl->m_disposed)
throw IOException(
"Stream is closed.");
63 return m_pImpl->m_position;
68 if (m_pImpl->m_disposed)
throw IOException(
"Stream is closed.");
71 m_pImpl->m_position = value;
80 if (m_pImpl->m_disposed)
throw IOException(
"Stream is closed.");
85 long remaining =
GetLength() - m_pImpl->m_position;
86 if (remaining <= 0)
return 0;
89 int toRead =
static_cast<int>((std::min)(
static_cast<long>(count), remaining));
90 std::memcpy(buffer + offset, m_pImpl->m_buffer.data() + m_pImpl->m_position, toRead);
91 m_pImpl->m_position += toRead;
97 if (origin == 0)
return offset;
98 if (origin == 1)
return currentPos + offset;
99 if (origin == 2)
return length + offset;
105 if (m_pImpl->m_disposed)
throw IOException(
"Stream is closed.");
109 if (newPosition < 0)
throw IOException(
"An attempt was made to move the file pointer before the beginning of the file.");
111 m_pImpl->m_position = newPosition;
112 return m_pImpl->m_position;
117 if (m_pImpl->m_disposed)
throw IOException(
"Stream is closed.");
118 if (!m_pImpl->m_writable)
throw IOException(
"Stream is not writable.");
122 m_pImpl->m_buffer.resize(
static_cast<size_t>(value), 0);
123 if (m_pImpl->m_position > value) {
124 m_pImpl->m_position = value;
130 if (m_pImpl->m_disposed)
throw IOException(
"Stream is closed.");
131 if (!m_pImpl->m_writable)
throw IOException(
"Stream is not writable.");
136 long requiredSize = m_pImpl->m_position + count;
138 m_pImpl->m_buffer.resize(
static_cast<size_t>(requiredSize));
142 std::memcpy(m_pImpl->m_buffer.data() + m_pImpl->m_position, buffer + offset, count);
143 m_pImpl->m_position += count;
148 m_pImpl->m_disposed =
true;
153 int size =
static_cast<int>(m_pImpl->m_buffer.size());
156 std::memcpy(arr.
GetData(), m_pImpl->m_buffer.data(), size);
Defines the exception thrown when a null reference is passed to a method that does not accept it.
Defines the exception thrown when an argument value is outside the acceptable range of values.
The exception that is thrown when an I/O error occurs.
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
ArgumentNullException(const String &sMessage)
Initializes a new instance of the ArgumentNullException class with a specified error message.
ArgumentOutOfRangeException(const String &sMessage)
Initializes a new instance of the ArgumentOutOfRangeException class with a specified error message.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
T * GetData()
Gets a pointer to the contiguous internal element buffer.
IOException()
Initializes a new instance of the IOException class with a default message.
void SetLength(long value) override
Sets the length of the current stream to the specified value.
~MemoryStream()
Destructor releasing encapsulated memory resources.
int Read(char *buffer, int offset, int count) override
Reads a block of bytes from the current stream and writes the data to a buffer.
long GetPosition() const override
Gets the current position within the stream.
bool CanSeek() const override
Gets a value indicating whether the current stream supports seeking.
void Flush() override
Overrides the Flush method so that no action is performed.
void Dispose() override
Releases the unmanaged resources used by the MemoryStream.
void SetPosition(long value) override
Sets the current position within the stream.
bool CanRead() const override
Gets a value indicating whether the current stream supports reading.
long Seek(long offset, int origin) override
Sets the position within the current stream to the specified value.
long GetLength() const override
Returns the length of the stream in bytes.
Array< char > ToArray() const
Writes the stream contents to a byte array, regardless of the Position property.
void Write(const char *buffer, int offset, int count) override
Writes a block of bytes to the current stream using data read from a buffer.
MemoryStream()
Initializes a new non-resizable instance of the MemoryStream class with an expandable capacity initia...
bool CanWrite() const override
Gets a value indicating whether the current stream supports writing.
static long CalculateSeekPosition(long currentPos, long length, long offset, int origin)
Calculate new absolute position from origin and offset.