1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "io/memory_file.h"
17
18 #include <cstdint>
19 #include <memory>
20
21 #include <base/containers/allocator.h>
22 #include <base/namespace.h>
23 #include <core/io/intf_file.h>
24 #include <core/log.h>
25 #include <core/namespace.h>
26
27 CORE_BEGIN_NAMESPACE()
28 using BASE_NS::CloneData;
29
Write(uint64_t index,const void * buffer,uint64_t count)30 uint64_t MemoryFileStorage::Write(uint64_t index, const void* buffer, uint64_t count)
31 {
32 if (index >= buffer_.size()) {
33 return 0;
34 }
35 if (CloneData(buffer_.data() + index, buffer_.size() - index, buffer, static_cast<size_t>(count))) {
36 return count;
37 }
38 return 0;
39 }
40
MemoryFile(std::shared_ptr<MemoryFileStorage> && buffer,Mode mode)41 MemoryFile::MemoryFile(std::shared_ptr<MemoryFileStorage>&& buffer, Mode mode)
42 : buffer_(BASE_NS::move(buffer)), mode_(mode)
43 {}
44
GetMode() const45 IFile::Mode MemoryFile::GetMode() const
46 {
47 return mode_;
48 }
49
Close()50 void MemoryFile::Close() {}
51
Read(void * buffer,uint64_t count)52 uint64_t MemoryFile::Read(void* buffer, uint64_t count)
53 {
54 if (mode_ == Mode::INVALID) {
55 return {};
56 }
57 uint64_t toRead = count;
58 if ((index_ + toRead) > buffer_->GetStorage().size()) {
59 toRead = buffer_->GetStorage().size() - index_;
60 }
61
62 if (toRead > 0) {
63 if (toRead <= SIZE_MAX) {
64 if (CloneData(buffer, static_cast<size_t>(count), &(buffer_->GetStorage().data()[index_]),
65 static_cast<size_t>(toRead))) {
66 index_ += toRead;
67 }
68 } else {
69 CORE_ASSERT_MSG(false, "Unable to read chunks bigger than (SIZE_MAX) bytes.");
70 toRead = 0;
71 }
72 }
73
74 return toRead;
75 }
76
Write(const void * buffer,uint64_t count)77 uint64_t MemoryFile::Write(const void* buffer, uint64_t count)
78 {
79 if (mode_ == Mode::READ_WRITE) {
80 buffer_->Resize(size_t(count));
81 return buffer_->Write(0, buffer, count);
82 }
83 return {};
84 }
85
Append(const void * buffer,uint64_t count,uint64_t)86 uint64_t MemoryFile::Append(const void* buffer, uint64_t count, uint64_t /*chunkSize*/)
87 {
88 if (mode_ == Mode::READ_WRITE) {
89 auto exSize = buffer_->Size();
90 buffer_->Resize(exSize + count);
91 return buffer_->Write(exSize, buffer, count);
92 }
93 return {};
94 }
95
GetLength() const96 uint64_t MemoryFile::GetLength() const
97 {
98 return buffer_->GetStorage().size();
99 }
100
Seek(uint64_t aOffset)101 bool MemoryFile::Seek(uint64_t aOffset)
102 {
103 if (aOffset < buffer_->GetStorage().size()) {
104 index_ = aOffset;
105 return true;
106 }
107
108 return false;
109 }
110
GetPosition() const111 uint64_t MemoryFile::GetPosition() const
112 {
113 return index_;
114 }
115 CORE_END_NAMESPACE()
116