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 #ifndef DISTRIBUTED_KV_BLOB_H 17 #define DISTRIBUTED_KV_BLOB_H 18 19 #include <string> 20 #include <vector> 21 #include <algorithm> 22 #include "visibility.h" 23 24 namespace OHOS { 25 namespace DistributedKv { 26 // note: Blob derives from Parcelable, so hiding inner a interface using blob is not possible unless Parcelable 27 // declared its interface as visible. 28 class Blob { 29 public: 30 API_EXPORT Blob(); 31 32 API_EXPORT ~Blob() = default; 33 34 // copy constructor for Blob. 35 API_EXPORT Blob(const Blob &blob); 36 API_EXPORT Blob &operator=(const Blob &blob); 37 38 // move constructor for Blob. 39 API_EXPORT Blob(Blob &&blob); 40 API_EXPORT Blob &operator=(Blob &&blob); 41 42 // construct a Blob use std::string. 43 API_EXPORT Blob(const std::string &str); 44 API_EXPORT Blob &operator=(const std::string &str); 45 46 // construct a Blob use char pointer and len. 47 API_EXPORT Blob(const char *str, size_t n); 48 49 // construct a Blob use char pointer. 50 API_EXPORT Blob(const char *str); 51 API_EXPORT Blob &operator=(const char *str); 52 53 // construct a Blob use std::vector<uint8_t> 54 API_EXPORT Blob(const std::vector<uint8_t> &bytes); 55 56 // construct a Blob use std::vector<uint8_t> 57 API_EXPORT Blob(std::vector<uint8_t> &&bytes); 58 59 // Return a reference to the data of the blob. 60 API_EXPORT const std::vector<uint8_t> &Data() const; 61 62 API_EXPORT operator const std::vector<uint8_t> &() const; 63 64 API_EXPORT operator std::vector<uint8_t> &&() noexcept; 65 66 // Return the length (in bytes) of the referenced data 67 API_EXPORT size_t Size() const; 68 69 // Return the occupied length when write this blob to rawdata 70 int RawSize() const; 71 72 // Return true if the length of the referenced data is zero 73 API_EXPORT bool Empty() const; 74 75 // Return the the byte in the referenced data. 76 // REQUIRES: n < size() 77 API_EXPORT uint8_t operator[](size_t n) const; 78 79 API_EXPORT bool operator==(const Blob &) const; 80 81 // Change this blob to refer to an empty array 82 API_EXPORT void Clear(); 83 84 // change vector<uint8_t> to std::string 85 API_EXPORT std::string ToString() const; 86 87 // comparison. Returns value: 88 // < 0 if "*this" < "blob", 89 // == 0 if "*this" == "blob", 90 // > 0 if "*this" > "blob" 91 API_EXPORT int Compare(const Blob &blob) const; 92 93 // Return true if "blob" is a prefix of "*this" 94 API_EXPORT bool StartsWith(const Blob &blob) const; 95 96 /* write blob size and data to memory buffer. return error when bufferLeftSize not enough. */ 97 bool WriteToBuffer(uint8_t *&cursorPtr, int &bufferLeftSize) const; 98 99 /* read a blob from memory buffer. */ 100 bool ReadFromBuffer(const uint8_t *&cursorPtr, int &bufferLeftSize); 101 102 private: 103 std::vector<uint8_t> blob_; 104 }; 105 } // namespace DistributedKv 106 } // namespace OHOS 107 108 #endif // DISTRIBUTED_KV_BLOB_H 109