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