• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 const size_t DataBuffer::MAX_DATA_LEN = 1024 * 1024 * 5;
23 
24 const int DataBuffer::MAX_TRANSFER_SIZE = 1024 * 1024 * 5 - DataBuffer::HEADER_LEN;
25 
26 const uint32_t DataBuffer::VERSION = 0;
27 
28 const uint32_t DataBuffer::TYPE = 0;
29 
DataBuffer()30 DataBuffer::DataBuffer() : buf_(nullptr), size_(0), used_(0)
31 {}
32 
~DataBuffer()33 DataBuffer::~DataBuffer()
34 {
35     if (buf_ != nullptr) {
36         delete[] buf_;
37         buf_ = nullptr;
38     }
39 }
40 
Init(size_t size)41 bool DataBuffer::Init(size_t size)
42 {
43     if (buf_ != nullptr || size == 0) {
44         return false;
45     }
46     size_ = std::min(size, MAX_DATA_LEN);
47     buf_ = new(std::nothrow) char[size_]();
48     if (buf_ == nullptr) {
49         return false;
50     }
51     used_ = 0;
52     return true;
53 }
54 
GetBufPtr() const55 const char *DataBuffer::GetBufPtr() const
56 {
57     return buf_;
58 }
59 
GetBufSize() const60 size_t DataBuffer::GetBufSize() const
61 {
62     return size_;
63 }
64 
SetBufUsed(size_t used)65 void DataBuffer::SetBufUsed(size_t used)
66 {
67     used_ = used;
68 }
69 
GetBufUsed() const70 size_t DataBuffer::GetBufUsed() const
71 {
72     return used_;
73 }
74 }  // namespace AppDistributedKv
75 }  // namespace OHOS
76