/root/doris/be/src/geo/ByteOrderDataInStream.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <cstddef> |
21 | | |
22 | | #include "ByteOrderValues.h" |
23 | | #include "geo/geo_common.h" |
24 | | #include "machine.h" |
25 | | namespace doris { |
26 | | class ByteOrderDataInStream { |
27 | | public: |
28 | 0 | ByteOrderDataInStream() : ByteOrderDataInStream(nullptr, 0) {}; |
29 | | |
30 | | ByteOrderDataInStream(const unsigned char* buff, size_t buffsz) |
31 | 0 | : byteOrder(getMachineByteOrder()), buf(buff), end(buff + buffsz) {}; |
32 | | |
33 | | ~ByteOrderDataInStream() = default; |
34 | | |
35 | 0 | void setOrder(int order) { byteOrder = order; }; |
36 | | |
37 | 0 | unsigned char readByte() { |
38 | 0 | if (size() < 1) { |
39 | 0 | return GEO_PARSE_WKB_SYNTAX_ERROR; |
40 | 0 | } |
41 | 0 | auto ret = buf[0]; |
42 | 0 | buf++; |
43 | 0 | return ret; |
44 | 0 | }; |
45 | | |
46 | 0 | int32_t readInt() { |
47 | 0 | if (size() < 4) { |
48 | 0 | return GEO_PARSE_WKB_SYNTAX_ERROR; |
49 | 0 | } |
50 | 0 | auto ret = ByteOrderValues::getInt(buf, byteOrder); |
51 | 0 | buf += 4; |
52 | 0 | return ret; |
53 | 0 | }; |
54 | | |
55 | 0 | uint32_t readUnsigned() { |
56 | 0 | if (size() < 4) { |
57 | 0 | return GEO_PARSE_WKB_SYNTAX_ERROR; |
58 | 0 | } |
59 | 0 | auto ret = ByteOrderValues::getUnsigned(buf, byteOrder); |
60 | 0 | buf += 4; |
61 | 0 | return ret; |
62 | 0 | }; |
63 | | |
64 | 0 | int64_t readLong() { |
65 | 0 | if (size() < 8) { |
66 | 0 | return GEO_PARSE_WKB_SYNTAX_ERROR; |
67 | 0 | } |
68 | 0 |
|
69 | 0 | auto ret = ByteOrderValues::getLong(buf, byteOrder); |
70 | 0 | buf += 8; |
71 | 0 | return ret; |
72 | 0 | }; |
73 | | |
74 | 0 | double readDouble() { |
75 | 0 | if (size() < 8) { |
76 | 0 | return GEO_PARSE_WKB_SYNTAX_ERROR; |
77 | 0 | } |
78 | 0 | auto ret = ByteOrderValues::getDouble(buf, byteOrder); |
79 | 0 | buf += 8; |
80 | 0 | return ret; |
81 | 0 | }; |
82 | | |
83 | 0 | size_t size() const { return static_cast<size_t>(end - buf); }; |
84 | | |
85 | | private: |
86 | | int byteOrder; |
87 | | const unsigned char* buf = nullptr; |
88 | | const unsigned char* end = nullptr; |
89 | | }; |
90 | | |
91 | | } // namespace doris |