blob: f8eb603084cf8b83e298dd9f2dff9b11dffb51ce [file] [log] [blame]
Lingfeng Yangc5ae0372019-12-13 11:20:29 -08001// Copyright 2019 The Android Open Source Project
2//
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#pragma once
16
Lingfeng Yangc9feed72020-11-05 10:18:12 -080017#include "msvc.h"
18
Lingfeng Yangc5ae0372019-12-13 11:20:29 -080019#include <string>
20
21#include <inttypes.h>
22#include <sys/types.h>
23
24namespace android {
25namespace base {
26
27// Abstract interface to byte streams of all kind.
28// This is mainly used to implement disk serialization.
29class Stream {
30public:
31 // Default constructor.
32 Stream() = default;
33
34 // Destructor.
35 virtual ~Stream() = default;
36
37 // Read up to |size| bytes and copy them to |buffer|. Return the number
38 // of bytes that were actually transferred, or -errno value on error.
39 virtual ssize_t read(void* buffer, size_t size) = 0;
40
41 // Write up to |size| bytes from |buffer| into the stream. Return the
42 // number of bytes that were actually transferred, or -errno value on
43 // error.
44 virtual ssize_t write(const void* buffer, size_t size) = 0;
45
46 // Write a single byte |value| into the stream. Ignore errors.
47 void putByte(uint8_t value);
48
49 // Write a 16-bit |value| as big-endian into the stream. Ignore errors.
50 void putBe16(uint16_t value);
51
52 // Write a 32-bit |value| as big-endian into the stream. Ignore errors.
53 void putBe32(uint32_t value);
54
55 // Write a 64-bit |value| as big-endian into the stream. Ignore errors.
56 void putBe64(uint64_t value);
57
58 // Read a single byte from the stream. Return 0 on error.
59 uint8_t getByte();
60
61 // Read a single big-endian 16-bit value from the stream.
62 // Return 0 on error.
63 uint16_t getBe16();
64
65 // Read a single big-endian 32-bit value from the stream.
66 // Return 0 on error.
67 uint32_t getBe32();
68
69 // Read a single big-endian 64-bit value from the stream.
70 // Return 0 on error.
71 uint64_t getBe64();
72
73 // Write a 32-bit float |value| to the stream.
74 void putFloat(float value);
75
76 // Read a single 32-bit float value from the stream.
77 float getFloat();
78
Lingfeng Yangc5ae0372019-12-13 11:20:29 -080079 // Write a 0-terminated C string |str| into the stream. Ignore error.
80 void putString(const char* str);
Lingfeng Yangbcb607f2020-10-28 15:56:23 -070081 void putString(const std::string& str);
Lingfeng Yangc5ae0372019-12-13 11:20:29 -080082
83 // Write a string |str| of |strlen| bytes into the stream.
84 // Ignore errors.
85 void putString(const char* str, size_t strlen);
86
87 // Read a string from the stream. Return a new string instance,
88 // which will be empty on error. Note that this can only be used
89 // to read strings that were written with putString().
90 std::string getString();
91
92 // Put/gen an integer number into the stream, making it use as little space
93 // there as possible.
94 // It uses a simple byte-by-byte encoding scheme, putting 7 bits of the
95 // number with the 8th bit set when there's more data to read, until the
96 // whole number is read.
97 // The compression is efficient if the number range is small, but it starts
98 // wasting space when values approach 14 bits for int16 (16K), 28 bits for
99 // int32 (268M) or 56 bits for int64 (still a lot).
100 void putPackedNum(uint64_t num);
101 uint64_t getPackedNum();
102
103 // Same thing, but encode negative numbers efficiently as well (single sign
104 // bit + packed unsigned representation)
105 void putPackedSignedNum(int64_t num);
106 int64_t getPackedSignedNum();
Lingfeng Yang6102ae32021-01-08 15:45:27 -0800107
108 // Static big-endian conversions
109 static void toByte(uint8_t*);
110 static void toBe16(uint8_t*);
111 static void toBe32(uint8_t*);
112 static void toBe64(uint8_t*);
113 static void fromByte(uint8_t*);
114 static void fromBe16(uint8_t*);
115 static void fromBe32(uint8_t*);
116 static void fromBe64(uint8_t*);
Lingfeng Yangc5ae0372019-12-13 11:20:29 -0800117};
118
119} // namespace base
120} // namespace android