1 /*
2  * Copyright (c) 2021-2022 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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CODEC_STANDARD_CODEC_BUFFER_OPERATOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CODEC_STANDARD_CODEC_BUFFER_OPERATOR_H
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include "base/utils/macros.h"
23 #include "base/utils/noncopyable.h"
24 #include "frameworks/bridge/codec/byte_buffer_operator.h"
25 #include "frameworks/bridge/codec/codec_data.h"
26 
27 namespace OHOS::Ace::Framework {
28 
29 class ACE_EXPORT StandardCodecBufferReader final {
30 public:
StandardCodecBufferReader(const std::vector<uint8_t> & buffer)31     explicit StandardCodecBufferReader(const std::vector<uint8_t>& buffer) : byteBufferReader_(buffer) {}
32     ~StandardCodecBufferReader() = default;
33 
34     bool ReadData(CodecData& resultData);
35     bool ReadDataList(std::vector<CodecData>& resultDataList);
36     bool ReadMapSize(int32_t& size);
37 
38 private:
39     bool ReadType(BufferDataType& type);
40 
41     ByteBufferReader byteBufferReader_;
42 
43     ACE_DISALLOW_COPY_AND_MOVE(StandardCodecBufferReader);
44 };
45 
46 class StandardCodecBufferWriter final {
47 public:
StandardCodecBufferWriter(std::vector<uint8_t> & buffer)48     explicit StandardCodecBufferWriter(std::vector<uint8_t>& buffer) : byteBufferWriter_(buffer) {}
49     ~StandardCodecBufferWriter() = default;
50 
51     void WriteData(const CodecData& data);
52     void WriteDataList(const std::vector<CodecData>& dataList);
53 
54 private:
55     void WriteType(BufferDataType type);
56 
57     ByteBufferWriter byteBufferWriter_;
58 
59     ACE_DISALLOW_COPY_AND_MOVE(StandardCodecBufferWriter);
60 };
61 
62 } // namespace OHOS::Ace::Framework
63 
64 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CODEC_STANDARD_CODEC_BUFFER_OPERATOR_H
65