| 168 | } |
| 169 | |
| 170 | std::int64_t MemoryStream::FetchFromStream(Stream& source) |
| 171 | { |
| 172 | static constexpr std::int32_t BufferSize = 8192; |
| 173 | |
| 174 | std::int64_t bytesReadTotal = 0; |
| 175 | if (_mode == AccessMode::Writable || _mode == AccessMode::Growable) { |
| 176 | bool shouldTrim = false; |
| 177 | while (true) { |
| 178 | std::int32_t bytesToRead = BufferSize; |
| 179 | if (_mode == AccessMode::Growable) { |
| 180 | if (_size < _pos + bytesToRead) { |
| 181 | _size = _pos + bytesToRead; |
| 182 | arrayResize(_data, Containers::NoInit, _size); |
| 183 | shouldTrim = true; |
| 184 | } |
| 185 | } else { |
| 186 | if (_size < _pos + bytesToRead) { |
| 187 | bytesToRead = static_cast<std::int32_t>(_size - _pos); |
| 188 | } |
| 189 | if DEATH_UNLIKELY(bytesToRead <= 0) { |
| 190 | // No more space to read into |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | std::int64_t bytesRead = source.Read(&_data[_pos], bytesToRead); |
| 196 | if DEATH_UNLIKELY(bytesRead <= 0) { |
| 197 | break; |
| 198 | } |
| 199 | _pos += bytesRead; |
| 200 | bytesReadTotal += bytesRead; |
| 201 | bytesToRead -= bytesRead; |
| 202 | } |
| 203 | |
| 204 | if (shouldTrim) { |
| 205 | // Trim the stream to the actual size read, as we might have reserved more space than needed in the last iteration |
| 206 | _size = _pos; |
| 207 | arrayResize(_data, Containers::NoInit, _size); |
| 208 | } |
| 209 | } |
| 210 | return bytesReadTotal; |
| 211 | } |
| 212 |
no test coverage detected