11 struct MockStream::Impl {
12 std::vector<char> m_buffer;
23 : m_pImpl(new Impl()) {
25 m_pImpl->m_lPosition = 0;
26 m_pImpl->m_bCanRead = bCanRead;
27 m_pImpl->m_bCanWrite = bCanWrite;
28 m_pImpl->m_bCanSeek = bCanSeek;
29 m_pImpl->m_bThrowOnRead =
false;
30 m_pImpl->m_bThrowOnWrite =
false;
31 m_pImpl->m_bIsDisposed =
false;
41 m_pImpl->m_bThrowOnRead = bThrow;
46 m_pImpl->m_bThrowOnWrite = bThrow;
51 m_pImpl->m_bCanRead = bCanRead;
56 m_pImpl->m_bCanWrite = bCanWrite;
61 m_pImpl->m_bCanSeek = bCanSeek;
66 return m_pImpl->m_bIsDisposed;
71 return m_pImpl->m_bCanRead && !m_pImpl->m_bIsDisposed;
76 return m_pImpl->m_bCanSeek && !m_pImpl->m_bIsDisposed;
81 return m_pImpl->m_bCanWrite && !m_pImpl->m_bIsDisposed;
86 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
88 return static_cast<long>(m_pImpl->m_buffer.size());
93 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
95 return m_pImpl->m_lPosition;
100 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
101 if (!m_pImpl->m_bCanSeek)
throw IOException(
"Stream does not support seeking.");
103 m_pImpl->m_lPosition = lValue;
108 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
113 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
114 if (!m_pImpl->m_bCanRead)
throw IOException(
"Stream does not support reading.");
115 if (m_pImpl->m_bThrowOnRead)
throw IOException(
"Simulated read fault.");
116 if (!pBuffer || iOffset < 0 || iCount < 0)
return 0;
119 long lAvailable =
static_cast<long>(m_pImpl->m_buffer.size()) - m_pImpl->m_lPosition;
120 if (lAvailable <= 0)
return 0;
123 int iToRead =
static_cast<int>(lAvailable < iCount ? lAvailable : iCount);
124 for (
int i = 0; i < iToRead; ++i) {
125 pBuffer[iOffset + i] = m_pImpl->m_buffer[m_pImpl->m_lPosition + i];
127 m_pImpl->m_lPosition += iToRead;
133 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
134 if (!m_pImpl->m_bCanSeek)
throw IOException(
"Stream does not support seeking.");
137 long lNewPos = m_pImpl->m_lPosition;
138 if (iOrigin == 0) lNewPos = lOffset;
139 else if (iOrigin == 1) lNewPos += lOffset;
140 else if (iOrigin == 2) lNewPos =
static_cast<long>(m_pImpl->m_buffer.size()) + lOffset;
143 if (lNewPos < 0) lNewPos = 0;
144 m_pImpl->m_lPosition = lNewPos;
145 return m_pImpl->m_lPosition;
150 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
151 if (!m_pImpl->m_bCanSeek || !m_pImpl->m_bCanWrite)
throw IOException(
"Stream does not support seeking or writing.");
153 m_pImpl->m_buffer.resize(
static_cast<size_t>(lValue), 0);
158 if (m_pImpl->m_bIsDisposed)
throw IOException(
"Stream is closed.");
159 if (!m_pImpl->m_bCanWrite)
throw IOException(
"Stream does not support writing.");
160 if (m_pImpl->m_bThrowOnWrite)
throw IOException(
"Simulated write fault.");
161 if (!pBuffer || iOffset < 0 || iCount <= 0)
return;
164 size_t uRequiredSize =
static_cast<size_t>(m_pImpl->m_lPosition + iCount);
165 if (m_pImpl->m_buffer.size() < uRequiredSize) {
166 m_pImpl->m_buffer.resize(uRequiredSize, 0);
170 for (
int i = 0; i < iCount; ++i) {
171 m_pImpl->m_buffer[m_pImpl->m_lPosition + i] = pBuffer[iOffset + i];
173 m_pImpl->m_lPosition += iCount;
178 m_pImpl->m_bIsDisposed =
true;
The exception that is thrown when an I/O error occurs.
Mock stream implementation for unit testing and diagnostic validation.
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
IOException()
Initializes a new instance of the IOException class with a default message.
void Flush() override
Clears all buffers for this stream.
int Read(char *pBuffer, int iOffset, int iCount) override
Reads a sequence of bytes from the current stream.
virtual ~MockStream() override
Releases resources used by the MockStream.
long Seek(long lOffset, int iOrigin) override
Sets the position within the current stream.
bool CanSeek() const override
Gets a value indicating whether the current stream supports seeking.
void Dispose() override
Closes and releases all resources associated with the stream.
void Write(const char *pBuffer, int iOffset, int iCount) override
Writes a sequence of bytes to the current stream.
void SetCanSeek(bool bCanSeek)
Sets whether seeking is supported.
void SetCanWrite(bool bCanWrite)
Sets whether write operations are supported.
void SetCanRead(bool bCanRead)
Sets whether read operations are supported.
long GetLength() const override
Gets the length in bytes of the stream.
bool IsDisposed() const
Checks whether the stream has been disposed.
void SetThrowOnRead(bool bThrow)
Configures whether future read operations throw an IOException.
void SetPosition(long lValue) override
Sets the current position within the stream.
void SetThrowOnWrite(bool bThrow)
Configures whether future write operations throw an IOException.
bool CanWrite() const override
Gets a value indicating whether the current stream supports writing.
MockStream(bool bCanRead=true, bool bCanWrite=true, bool bCanSeek=true)
Initializes a new instance of MockStream with configurable capabilities.
bool CanRead() const override
Gets a value indicating whether the current stream supports reading.
void SetLength(long lValue) override
Sets the length of the current stream.
long GetPosition() const override
Gets the current position within the stream.