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