• 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 #pragma once
18 
19 #include <string>
20 #include "android-base/off64_t.h"
21 
22 namespace android {
23 
24 // InputStream interface that mimics protobuf's ZeroCopyInputStream,
25 // with added error handling methods to better report issues.
26 class InputStream {
27  public:
28   virtual ~InputStream() = default;
29 
30   // Returns a chunk of data for reading. data and size must not be nullptr.
31   // Returns true so long as there is more data to read, returns false if an error occurred
32   // or no data remains. If an error occurred, check HadError().
33   // The stream owns the buffer returned from this method and the buffer is invalidated
34   // anytime another mutable method is called.
35   virtual bool Next(const void** data, size_t* size) = 0;
36 
37   // Backup count bytes, where count is smaller or equal to the size of the last buffer returned
38   // from Next().
39   // Useful when the last block returned from Next() wasn't fully read.
40   virtual void BackUp(size_t count) = 0;
41 
42   // Returns true if this InputStream can rewind. If so, Rewind() can be called.
CanRewind()43   virtual bool CanRewind() const {
44     return false;
45   };
46 
47   // Rewinds the stream to the beginning so it can be read again.
48   // Returns true if the rewind succeeded.
49   // This does nothing if CanRewind() returns false.
Rewind()50   virtual bool Rewind() {
51     return false;
52   }
53 
54   // Returns the number of bytes that have been read from the stream.
55   virtual size_t ByteCount() const = 0;
56 
57   // Returns an error message if HadError() returned true.
GetError()58   virtual std::string GetError() const {
59     return {};
60   }
61 
62   // Returns true if an error occurred. Errors are permanent.
63   virtual bool HadError() const = 0;
64 
ReadFullyAtOffset(void * data,size_t byte_count,off64_t offset)65   virtual bool ReadFullyAtOffset(void* data, size_t byte_count, off64_t offset) {
66     (void)data;
67     (void)byte_count;
68     (void)offset;
69     return false;
70   }
71 };
72 
73 // A sub-InputStream interface that knows the total size of its stream.
74 class KnownSizeInputStream : public InputStream {
75  public:
76   virtual size_t TotalSize() const = 0;
77 };
78 
79 // OutputStream interface that mimics protobuf's ZeroCopyOutputStream,
80 // with added error handling methods to better report issues.
81 class OutputStream {
82  public:
83   virtual ~OutputStream() = default;
84 
85   // Returns a buffer to which data can be written to. The data written to this buffer will
86   // eventually be written to the stream. Call BackUp() if the data written doesn't occupy the
87   // entire buffer.
88   // Return false if there was an error.
89   // The stream owns the buffer returned from this method and the buffer is invalidated
90   // anytime another mutable method is called.
91   virtual bool Next(void** data, size_t* size) = 0;
92 
93   // Backup count bytes, where count is smaller or equal to the size of the last buffer returned
94   // from Next().
95   // Useful for when the last block returned from Next() wasn't fully written to.
96   virtual void BackUp(size_t count) = 0;
97 
98   // Returns the number of bytes that have been written to the stream.
99   virtual size_t ByteCount() const = 0;
100 
101   // Returns an error message if HadError() returned true.
GetError()102   virtual std::string GetError() const {
103     return {};
104   }
105 
106   // Returns true if an error occurred. Errors are permanent.
107   virtual bool HadError() const = 0;
108 };
109 
110 }  // namespace android