Debian Codegraph @

tools/aapt2/io/Io.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef AAPT_IO_IO_H
18 #define AAPT_IO_IO_H
19
20 #include <string>
21
22 namespace aapt {
23 namespace io {
24
25 // InputStream interface that mimics protobuf's ZeroCopyInputStream,
26 // with added error handling methods to better report issues.
27 class InputStream {
28 public:
29 virtual ~InputStream() = default;
30
31 // Returns a chunk of data for reading. data and size must not be nullptr.
32 // Returns true so long as there is more data to read, returns false if an error occurred
33 // or no data remains. If an error occurred, check HadError().
34 // The stream owns the buffer returned from this method and the buffer is invalidated
35 // anytime another mutable method is called.
36 virtual bool Next(const void** data, size_t* size) = 0;
37
38 // Backup count bytes, where count is smaller or equal to the size of the last buffer returned
39 // from Next().
40 // Useful when the last block returned from Next() wasn't fully read.
41 virtual void BackUp(size_t count) = 0;
42
43 // Returns true if this InputStream can rewind. If so, Rewind() can be called.
44 virtual bool CanRewind() const { return false; };
45
46 // Rewinds the stream to the beginning so it can be read again.
47 // Returns true if the rewind succeeded.
48 // This does nothing if CanRewind() returns false.
49 virtual bool Rewind() { return false; }
50
51 // Returns the number of bytes that have been read from the stream.
52 virtual size_t ByteCount() const = 0;
53
54 // Returns an error message if HadError() returned true.
55 virtual std::string GetError() const { return {}; }
56
57 // Returns true if an error occurred. Errors are permanent.
58 virtual bool HadError() const = 0;
59 };
60
61 // A sub-InputStream interface that knows the total size of its stream.
62 class KnownSizeInputStream : public InputStream {
63 public:
64 virtual size_t TotalSize() const = 0;
65 };
66
67 // OutputStream interface that mimics protobuf's ZeroCopyOutputStream,
68 // with added error handling methods to better report issues.
69 class OutputStream {
70 public:
71 virtual ~OutputStream() = default;
72
73 // Returns a buffer to which data can be written to. The data written to this buffer will
74 // eventually be written to the stream. Call BackUp() if the data written doesn't occupy the
75 // entire buffer.
76 // Return false if there was an error.
77 // The stream owns the buffer returned from this method and the buffer is invalidated
78 // anytime another mutable method is called.
79 virtual bool Next(void** data, size_t* size) = 0;
80
81 // Backup count bytes, where count is smaller or equal to the size of the last buffer returned
82 // from Next().
83 // Useful for when the last block returned from Next() wasn't fully written to.
84 virtual void BackUp(size_t count) = 0;
85
86 // Returns the number of bytes that have been written to the stream.
87 virtual size_t ByteCount() const = 0;
88
89 // Returns an error message if HadError() returned true.
90 virtual std::string GetError() const { return {}; }
91
92 // Returns true if an error occurred. Errors are permanent.
93 virtual bool HadError() const = 0;
94 };
95
96 } // namespace io
97 } // namespace aapt
98
99 #endif /* AAPT_IO_IO_H */