1 /*
2 * Copyright (c) 2021-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 #ifndef STREAM_BUFFER_H
16 #define STREAM_BUFFER_H
17
18 #include <cstdint>
19 #include <string>
20 #include <vector>
21
22 #include "nocopyable.h"
23 #include "securec.h"
24
25 #include "config_multimodal.h"
26 #include "define_multimodal.h"
27 #include "error_multimodal.h"
28 #include "mmi_log.h"
29
30 namespace OHOS {
31 namespace MMI {
32 class StreamBuffer {
33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "StreamBuffer" };
34 public:
35 StreamBuffer() = default;
36 DISALLOW_MOVE(StreamBuffer);
37 virtual ~StreamBuffer() = default;
38 explicit StreamBuffer(const StreamBuffer &buf);
39 virtual StreamBuffer &operator=(const StreamBuffer &other);
40
41 void Reset();
42 void Clean();
43 bool SeekReadPos(int32_t n);
44
45 bool Read(std::string &buf);
46 bool Write(const std::string &buf);
47
48 bool Read(StreamBuffer &buf);
49 bool Write(const StreamBuffer &buf);
50
51 bool Read(char *buf, size_t size);
52 virtual bool Write(const char *buf, size_t size);
53
54 bool IsEmpty() const;
55 size_t Size() const;
56 int32_t UnreadSize() const;
57 int32_t GetAvailableBufSize() const;
58
59 bool ChkRWError() const;
60 const std::string &GetErrorStatusRemark() const;
61 const char *Data() const;
62
63 template<typename T>
64 bool Read(T &data);
65 template<typename T>
66 bool Write(const T &data);
67 template<typename T>
68 bool Read(std::vector<T> &data);
69 template<typename T>
70 bool Write(const std::vector<T> &data);
71
72 const char *ReadBuf() const;
73 const char *WriteBuf() const;
74
75 template<typename T>
76 StreamBuffer &operator >> (T &data);
77 template<typename T>
78 StreamBuffer &operator << (const T &data);
79
80 protected:
81 bool Clone(const StreamBuffer &buf);
82
83 protected:
84 enum class ErrorStatus {
85 ERROR_STATUS_OK,
86 ERROR_STATUS_READ,
87 ERROR_STATUS_WRITE,
88 };
89 ErrorStatus rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK;
90 int32_t rCount_ { 0 };
91 int32_t wCount_ { 0 };
92
93 int32_t rPos_ { 0 };
94 int32_t wPos_ { 0 };
95 char szBuff_[MAX_STREAM_BUF_SIZE+1] = {};
96 };
97
98 template<typename T>
Read(T & data)99 bool StreamBuffer::Read(T &data)
100 {
101 if (!Read(reinterpret_cast<char *>(&data), sizeof(data))) {
102 MMI_HILOGE("[%{public}s] size:%{public}zu count:%{public}d,errCode:%{public}d",
103 GetErrorStatusRemark().c_str(), sizeof(data), rCount_ + 1, STREAM_BUF_READ_FAIL);
104 return false;
105 }
106 return true;
107 }
108
109 template<typename T>
Write(const T & data)110 bool StreamBuffer::Write(const T &data)
111 {
112 if (!Write(reinterpret_cast<const char *>(&data), sizeof(data))) {
113 MMI_HILOGE("[%{public}s] size:%{public}zu,count:%{public}d,errCode:%{public}d",
114 GetErrorStatusRemark().c_str(), sizeof(data), wCount_ + 1, STREAM_BUF_WRITE_FAIL);
115 return false;
116 }
117 return true;
118 }
119
120 template<typename T>
Read(std::vector<T> & data)121 bool StreamBuffer::Read(std::vector<T> &data)
122 {
123 int32_t size = 0;
124 if (!Read(size)) {
125 MMI_HILOGE("Read vector size error");
126 return false;
127 }
128 if (size < 0 || size > MAX_VECTOR_SIZE) {
129 MMI_HILOGE("Read vector size:%{public}d error", size);
130 return false;
131 }
132 for (int32_t i = 0; i < size; i++) {
133 T val;
134 if (!Read(val)) {
135 MMI_HILOGE("Read vector data error");
136 return false;
137 }
138 data.push_back(val);
139 }
140 return true;
141 }
142
143 template<typename T>
Write(const std::vector<T> & data)144 bool StreamBuffer::Write(const std::vector<T> &data)
145 {
146 if (data.size() > INT32_MAX) {
147 MMI_HILOGE("Vector exceeds the max range");
148 return false;
149 }
150 int32_t size = static_cast<int32_t>(data.size());
151 if (!Write(size)) {
152 MMI_HILOGE("Write vector size error");
153 return false;
154 }
155 for (const auto &item : data) {
156 if (!Write(item)) {
157 MMI_HILOGE("Write vector data error");
158 return false;
159 }
160 }
161 return true;
162 }
163
164 template<typename T>
165 StreamBuffer &StreamBuffer::operator>>(T &data)
166 {
167 if (!Read(data)) {
168 MMI_HILOGW("Read data failed");
169 }
170 return *this;
171 }
172
173 template<typename T>
174 StreamBuffer &StreamBuffer::operator<<(const T &data)
175 {
176 if (!Write(data)) {
177 MMI_HILOGW("Write data failed");
178 }
179 return *this;
180 }
181 } // namespace MMI
182 } // namespace OHOS
183 #endif // STREAM_BUFFER_H