• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
CanRewind()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.
Rewind()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.
GetError()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 // OutputStream interface that mimics protobuf's ZeroCopyOutputStream,
62 // with added error handling methods to better report issues.
63 class OutputStream {
64  public:
65   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.
GetError()84   virtual std::string GetError() const { return {}; }
85 
86   // Returns true if an error occurred. Errors are permanent.
87   virtual bool HadError() const = 0;
88 };
89 
90 }  // namespace io
91 }  // namespace aapt
92 
93 #endif /* AAPT_IO_IO_H */
94