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
21 #include "nocopyable.h"
22 #include "securec.h"
23
24 #include "config_multimodal.h"
25 #include "define_multimodal.h"
26 #include "error_multimodal.h"
27 #include "mmi_log.h"
28
29 namespace OHOS {
30 namespace MMI {
31 class StreamBuffer {
32 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "StreamBuffer"};
33 public:
34 StreamBuffer() = default;
35 virtual ~StreamBuffer() = default;
36 explicit StreamBuffer(const StreamBuffer& buf);
37 virtual StreamBuffer& operator=(const StreamBuffer& other);
38 DISALLOW_MOVE(StreamBuffer);
39
40 void Reset();
41 void Clean();
42 bool SeekReadPos(int32_t n);
43
44 bool Read(std::string& buf);
45 bool Write(const std::string& buf);
46
47 bool Read(StreamBuffer& buf);
48 bool Write(const StreamBuffer& buf);
49
50 bool Read(char *buf, size_t size);
51 virtual bool Write(const char *buf, size_t size);
52
53 bool IsEmpty() const;
54 size_t Size() const;
55 int32_t UnreadSize() const;
56 int32_t GetAvailableBufSize() const;
57
58 bool ChkRWError() const;
59 const std::string& GetErrorStatusRemark() const;
60 const char *Data() const;
61
62 template<typename T>
63 bool Read(T& data);
64 template<typename T>
65 bool Write(const T& data);
66
67 const char *ReadBuf() const;
68 const char *WriteBuf() const;
69
70 template<typename T>
71 StreamBuffer& operator >> (T& data);
72 template<typename T>
73 StreamBuffer& operator << (const T& data);
74
75 protected:
76 bool Clone(const StreamBuffer& buf);
77
78 protected:
79 enum class ErrorStatus {
80 ERROR_STATUS_OK,
81 ERROR_STATUS_READ,
82 ERROR_STATUS_WRITE,
83 };
84 ErrorStatus rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK;
85 int32_t rCount_ = 0;
86 int32_t wCount_ = 0;
87
88 int32_t rPos_ = 0;
89 int32_t wPos_ = 0;
90 char szBuff_[MAX_STREAM_BUF_SIZE+1] = {};
91 };
92
93 template<typename T>
Read(T & data)94 bool StreamBuffer::Read(T &data)
95 {
96 if (!Read(reinterpret_cast<char *>(&data), sizeof(data))) {
97 MMI_LOGE("[%{public}s] size:%{public}zu count:%{public}d,errCode:%{public}d",
98 GetErrorStatusRemark().c_str(), sizeof(data), rCount_ + 1, STREAM_BUF_READ_FAIL);
99 return false;
100 }
101 return true;
102 }
103
104 template<typename T>
Write(const T & data)105 bool StreamBuffer::Write(const T &data)
106 {
107 if (!Write(reinterpret_cast<char *>(const_cast<T *>(&data)), sizeof(data))) {
108 MMI_LOGE("[%{public}s] size:%{public}zu,count:%{public}d,errCode:%{public}d",
109 GetErrorStatusRemark().c_str(), sizeof(data), wCount_ + 1, STREAM_BUF_WRITE_FAIL);
110 return false;
111 }
112 return true;
113 }
114
115 template<typename T>
116 StreamBuffer &StreamBuffer::operator>>(T &data)
117 {
118 if (!Read(data)) {
119 MMI_LOGW("Read data failed");
120 }
121 return *this;
122 }
123
124 template<typename T>
125 StreamBuffer &StreamBuffer::operator<<(const T &data)
126 {
127 if (!Write(data)) {
128 MMI_LOGW("Write data failed");
129 }
130 return *this;
131 }
132 } // namespace MMI
133 } // namespace OHOS
134 #endif // STREAM_BUFFER_H