1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
15 #include "pw_stream/memory_stream.h"
20 #include "pw_status/status_with_size.h"
22 namespace pw::stream {
24 Status MemoryWriter::DoWrite(ConstByteSpan data) {
25 if (ConservativeWriteLimit() == 0) {
26 return Status::OutOfRange();
28 if (ConservativeWriteLimit() < data.size_bytes()) {
29 return Status::ResourceExhausted();
32 size_t bytes_to_write = data.size_bytes();
33 std::memcpy(dest_.data() + bytes_written_, data.data(), bytes_to_write);
34 bytes_written_ += bytes_to_write;
39 StatusWithSize MemoryReader::DoRead(ByteSpan dest) {
40 if (source_.size_bytes() == bytes_read_) {
41 return StatusWithSize::OutOfRange();
44 size_t bytes_to_read =
45 std::min(dest.size_bytes(), source_.size_bytes() - bytes_read_);
47 std::memcpy(dest.data(), source_.data() + bytes_read_, bytes_to_read);
48 bytes_read_ += bytes_to_read;
50 return StatusWithSize(bytes_to_read);
53 } // namespace pw::stream