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