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