blob: 2a34d4dd442d2d87d3adb92dd751a4b97b21387d [file] [log] [blame]
Adam Lesinski21efb682016-09-14 17:35:43 -07001/*
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
Adam Lesinski21efb682016-09-14 17:35:43 -070020#include <string>
21
22namespace aapt {
23namespace io {
24
Adam Lesinski06460ef2017-03-14 18:52:13 -070025// InputStream interface that mimics protobuf's ZeroCopyInputStream,
26// with added error handling methods to better report issues.
27class InputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 public:
Adam Lesinski06460ef2017-03-14 18:52:13 -070029 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.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 virtual std::string GetError() const { return {}; }
Adam Lesinski21efb682016-09-14 17:35:43 -070056
Adam Lesinski06460ef2017-03-14 18:52:13 -070057 // Returns true if an error occurred. Errors are permanent.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 virtual bool HadError() const = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070059};
60
Adam Lesinski06460ef2017-03-14 18:52:13 -070061// OutputStream interface that mimics protobuf's ZeroCopyOutputStream,
62// with added error handling methods to better report issues.
63class OutputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 public:
Adam Lesinski06460ef2017-03-14 18:52:13 -070065 virtual ~OutputStream() = default;
66
67 // Returns a buffer to which data can be written to. The data written to this buffer will
68 // eventually be written to the stream. Call BackUp() if the data written doesn't occupy the
69 // entire buffer.
70 // Return false if there was an error.
71 // The stream owns the buffer returned from this method and the buffer is invalidated
72 // anytime another mutable method is called.
73 virtual bool Next(void** data, size_t* size) = 0;
74
75 // Backup count bytes, where count is smaller or equal to the size of the last buffer returned
76 // from Next().
77 // Useful for when the last block returned from Next() wasn't fully written to.
78 virtual void BackUp(size_t count) = 0;
79
80 // Returns the number of bytes that have been written to the stream.
81 virtual size_t ByteCount() const = 0;
82
83 // Returns an error message if HadError() returned true.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 virtual std::string GetError() const { return {}; }
Adam Lesinski21efb682016-09-14 17:35:43 -070085
Adam Lesinski06460ef2017-03-14 18:52:13 -070086 // Returns true if an error occurred. Errors are permanent.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 virtual bool HadError() const = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070088};
89
Adam Lesinski06460ef2017-03-14 18:52:13 -070090// Copies the data from in to out. Returns false if there was an error.
91// If there was an error, check the individual streams' HadError/GetError methods.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092bool Copy(OutputStream* out, InputStream* in);
Adam Lesinski21efb682016-09-14 17:35:43 -070093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094} // namespace io
95} // namespace aapt
Adam Lesinski21efb682016-09-14 17:35:43 -070096
97#endif /* AAPT_IO_IO_H */