• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11 
12 #pragma once
13 
14 #include "android/base/StringView.h"
15 
16 #include <string>
17 
18 #include <inttypes.h>
19 #include <sys/types.h>
20 
21 namespace android {
22 namespace base {
23 
24 // Abstract interface to byte streams of all kind.
25 // This is mainly used to implement disk serialization.
26 class Stream {
27 public:
28     // Default constructor.
29     Stream() = default;
30 
31     // Destructor.
32     virtual ~Stream() = default;
33 
34     // Read up to |size| bytes and copy them to |buffer|. Return the number
35     // of bytes that were actually transferred, or -errno value on error.
36     virtual ssize_t read(void* buffer, size_t size) = 0;
37 
38     // Write up to |size| bytes from |buffer| into the stream. Return the
39     // number of bytes that were actually transferred, or -errno value on
40     // error.
41     virtual ssize_t write(const void* buffer, size_t size) = 0;
42 
43     // Write a single byte |value| into the stream. Ignore errors.
44     void putByte(uint8_t value);
45 
46     // Write a 16-bit |value| as big-endian into the stream. Ignore errors.
47     void putBe16(uint16_t value);
48 
49     // Write a 32-bit |value| as big-endian into the stream. Ignore errors.
50     void putBe32(uint32_t value);
51 
52     // Write a 64-bit |value| as big-endian into the stream. Ignore errors.
53     void putBe64(uint64_t value);
54 
55     // Read a single byte from the stream. Return 0 on error.
56     uint8_t getByte();
57 
58     // Read a single big-endian 16-bit value from the stream.
59     // Return 0 on error.
60     uint16_t getBe16();
61 
62     // Read a single big-endian 32-bit value from the stream.
63     // Return 0 on error.
64     uint32_t getBe32();
65 
66     // Read a single big-endian 64-bit value from the stream.
67     // Return 0 on error.
68     uint64_t getBe64();
69 
70     // Write a 32-bit float |value| to the stream.
71     void putFloat(float value);
72 
73     // Read a single 32-bit float value from the stream.
74     float getFloat();
75 
76     // Write a string |str| into the stream. Ignore errors.
77     void putString(StringView str);
78 
79     // Write a 0-terminated C string |str| into the stream. Ignore error.
80     void putString(const char* str);
81 
82     // Write a string |str| of |strlen| bytes into the stream.
83     // Ignore errors.
84     void putString(const char* str, size_t strlen);
85 
86     // Read a string from the stream. Return a new string instance,
87     // which will be empty on error. Note that this can only be used
88     // to read strings that were written with putString().
89     std::string getString();
90 
91     // Put/gen an integer number into the stream, making it use as little space
92     // there as possible.
93     // It uses a simple byte-by-byte encoding scheme, putting 7 bits of the
94     // number with the 8th bit set when there's more data to read, until the
95     // whole number is read.
96     // The compression is efficient if the number range is small, but it starts
97     // wasting space when values approach 14 bits for int16 (16K), 28 bits for
98     // int32 (268M) or 56 bits for int64 (still a lot).
99     void putPackedNum(uint64_t num);
100     uint64_t getPackedNum();
101 
102     // Same thing, but encode negative numbers efficiently as well (single sign
103     // bit + packed unsigned representation)
104     void putPackedSignedNum(int64_t num);
105     int64_t getPackedSignedNum();
106 };
107 
108 }  // namespace base
109 }  // namespace android
110