1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_BUFFER_READER_H
17 #define OHOS_SHARING_BUFFER_READER_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 namespace OHOS {
25 namespace Sharing {
26 class DataBuffer {
27 public:
28     using Ptr = std::shared_ptr<DataBuffer>;
29 
30     DataBuffer() = default;
31     explicit DataBuffer(int32_t size);
32 
33     DataBuffer(const DataBuffer &other) noexcept;
34     DataBuffer &operator=(const DataBuffer &other) noexcept;
35     DataBuffer(DataBuffer &&other) noexcept;
36     DataBuffer &operator=(DataBuffer &&other) noexcept;
37 
38     virtual ~DataBuffer();
39 
Data()40     virtual uint8_t *Data()
41     {
42         return data_;
43     }
44 
Size() const45     virtual int32_t Size() const
46     {
47         return size_;
48     }
49 
Capacity()50     int32_t Capacity()
51     {
52         return capacity_;
53     }
54 
UpdateSize(int32_t size)55     void UpdateSize(int32_t size)
56     {
57         if (size <= capacity_) {
58             size_ = size;
59         }
60     }
61 
Peek() const62     const char *Peek() const
63     {
64         return (char *)data_;
65     }
66 
SetSize(int32_t size)67     void SetSize(int32_t size)
68     {
69         size_ = size;
70     }
71 
Clear()72     virtual void Clear()
73     {
74         if (data_) {
75             delete[] data_;
76             data_ = nullptr;
77         }
78 
79         size_ = 0;
80         capacity_ = 0;
81     }
82 
Append(const char *data, int32_t dataLen)83     void Append(const char *data, int32_t dataLen)
84     {
85         PushData(data, dataLen);
86     }
87 
Append(const uint8_t *data, int32_t dataLen)88     void Append(const uint8_t *data, int32_t dataLen)
89     {
90         PushData((const char *)data, dataLen);
91     }
92 
Append(uint8_t c)93     void Append(uint8_t c)
94     {
95         PushData((char *)&c, 1);
96     }
97 
Assign(const char *data, int32_t dataLen)98     void Assign(const char *data, int32_t dataLen)
99     {
100         ReplaceData(data, dataLen);
101     }
102 
Assign(uint8_t c)103     void Assign(uint8_t c)
104     {
105         ReplaceData((char *)&c, 1);
106     }
107 
108 public:
109     void Resize(int32_t size);
110     void SetCapacity(int32_t capacity);
111     void PushData(const char *data, int32_t dataLen);
112     void ReplaceData(const char *data, int32_t dataLen);
113 
114 private:
115     int32_t size_ = 0;
116     int32_t capacity_ = 0;
117     uint8_t *data_ = nullptr;
118 };
119 
120 } // namespace Sharing
121 } // namespace OHOS
122 #endif