1 /*
2  * Copyright (c) 2021 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 #include "data_buffer.h"
17 
18 namespace OHOS {
19 namespace AppDistributedKv {
20 int DataBuffer::sequence_ = 0;
21 
DataBuffer()22 DataBuffer::DataBuffer() : buf_(nullptr), size_(0), used_(0)
23 {}
24 
~DataBuffer()25 DataBuffer::~DataBuffer()
26 {
27     if (buf_ != nullptr) {
28         delete[] buf_;
29         buf_ = nullptr;
30     }
31 }
32 
Init(size_t size)33 bool DataBuffer::Init(size_t size)
34 {
35     if (buf_ != nullptr || size == 0) {
36         return false;
37     }
38     size_ = std::min(size, MAX_DATA_LEN);
39     buf_ = new(std::nothrow) char[size_]();
40     if (buf_ == nullptr) {
41         return false;
42     }
43     used_ = 0;
44     return true;
45 }
46 
GetBufPtr() const47 const char *DataBuffer::GetBufPtr() const
48 {
49     return buf_;
50 }
51 
GetBufSize() const52 size_t DataBuffer::GetBufSize() const
53 {
54     return size_;
55 }
56 
SetBufUsed(size_t used)57 void DataBuffer::SetBufUsed(size_t used)
58 {
59     used_ = used;
60 }
61 
GetBufUsed() const62 size_t DataBuffer::GetBufUsed() const
63 {
64     return used_;
65 }
66 }  // namespace AppDistributedKv
67 }  // namespace OHOS
68