1 /*
2 * Copyright (c) 2022 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/containers/shared_ptr.h>
23 #include <base/namespace.h>
24 #include <core/io/intf_file.h>
25 #include <core/log.h>
26 #include <core/namespace.h>
27
28 CORE_BEGIN_NAMESPACE()
29 using BASE_NS::CloneData;
30
Write(uint64_t index,const void * buffer,uint64_t count)31 uint64_t MemoryFileStorage::Write(uint64_t index, const void* buffer, uint64_t count)
32 {
33 if (index >= buffer_.size()) {
34 return 0;
35 }
36 if (CloneData(buffer_.data() + index, buffer_.size() - index, buffer, static_cast<size_t>(count))) {
37 return count;
38 }
39 return 0;
40 }
41
MemoryFile(BASE_NS::shared_ptr<MemoryFileStorage> && buffer,Mode mode)42 MemoryFile::MemoryFile(BASE_NS::shared_ptr<MemoryFileStorage>&& buffer, Mode mode)
43 : buffer_(BASE_NS::move(buffer)), mode_(mode)
44 {}
45
GetMode() const46 IFile::Mode MemoryFile::GetMode() const
47 {
48 return mode_;
49 }
50
Close()51 void MemoryFile::Close() {}
52
Read(void * buffer,uint64_t count)53 uint64_t MemoryFile::Read(void* buffer, uint64_t count)
54 {
55 if (mode_ == Mode::INVALID) {
56 return {};
57 }
58 uint64_t toRead = count;
59 if ((index_ + toRead) > buffer_->GetStorage().size()) {
60 toRead = buffer_->GetStorage().size() - index_;
61 }
62
63 if (toRead > 0) {
64 if (toRead <= SIZE_MAX) {
65 if (CloneData(buffer, static_cast<size_t>(count), &(buffer_->GetStorage().data()[index_]),
66 static_cast<size_t>(toRead))) {
67 index_ += toRead;
68 }
69 } else {
70 CORE_ASSERT_MSG(false, "Unable to read chunks bigger than (SIZE_MAX) bytes.");
71 toRead = 0;
72 }
73 }
74
75 return toRead;
76 }
77
Write(const void * buffer,uint64_t count)78 uint64_t MemoryFile::Write(const void* buffer, uint64_t count)
79 {
80 if (mode_ == Mode::READ_WRITE) {
81 buffer_->Resize(size_t(count));
82 return buffer_->Write(0, buffer, count);
83 }
84 return {};
85 }
86
Append(const void * buffer,uint64_t count,uint64_t)87 uint64_t MemoryFile::Append(const void* buffer, uint64_t count, uint64_t /*chunkSize*/)
88 {
89 if (mode_ == Mode::READ_WRITE) {
90 auto exSize = buffer_->Size();
91 buffer_->Resize(exSize + count);
92 return buffer_->Write(exSize, buffer, count);
93 }
94 return {};
95 }
96
GetLength() const97 uint64_t MemoryFile::GetLength() const
98 {
99 return buffer_->GetStorage().size();
100 }
101
Seek(uint64_t aOffset)102 bool MemoryFile::Seek(uint64_t aOffset)
103 {
104 if (aOffset < buffer_->GetStorage().size()) {
105 index_ = aOffset;
106 return true;
107 }
108
109 return false;
110 }
111
GetPosition() const112 uint64_t MemoryFile::GetPosition() const
113 {
114 return index_;
115 }
116 CORE_END_NAMESPACE()
117