1 // Copyright 2015 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 
17 #include "android/base/StringView.h"
18 
19 #include <string>
20 
21 #include <inttypes.h>
22 #include <sys/types.h>
23 
24 namespace android {
25 namespace base {
26 
27 // Abstract interface to byte streams of all kind.
28 // This is mainly used to implement disk serialization.
29 class Stream {
30 public:
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 
79     // Write a string |str| into the stream. Ignore errors.
80     void putString(StringView str);
81 
82     // Write a 0-terminated C string |str| into the stream. Ignore error.
83     void putString(const char* str);
84 
85     // Write a string |str| of |strlen| bytes into the stream.
86     // Ignore errors.
87     void putString(const char* str, size_t strlen);
88 
89     // Read a string from the stream. Return a new string instance,
90     // which will be empty on error. Note that this can only be used
91     // to read strings that were written with putString().
92     std::string getString();
93 
94     // Put/gen an integer number into the stream, making it use as little space
95     // there as possible.
96     // It uses a simple byte-by-byte encoding scheme, putting 7 bits of the
97     // number with the 8th bit set when there's more data to read, until the
98     // whole number is read.
99     // The compression is efficient if the number range is small, but it starts
100     // wasting space when values approach 14 bits for int16 (16K), 28 bits for
101     // int32 (268M) or 56 bits for int64 (still a lot).
102     void putPackedNum(uint64_t num);
103     uint64_t getPackedNum();
104 
105     // Same thing, but encode negative numbers efficiently as well (single sign
106     // bit + packed unsigned representation)
107     void putPackedSignedNum(int64_t num);
108     int64_t getPackedSignedNum();
109 };
110 
111 }  // namespace base
112 }  // namespace android
113