| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT_IO_FILESTREAM_H |
| 18 |
#define AAPT_IO_FILESTREAM_H |
| 19 |
|
| 20 |
#include "io/Io.h" |
| 21 |
|
| 22 |
#include <memory> |
| 23 |
#include <string> |
| •24 |
#include <stdint.h> |
| 25 |
|
| 26 |
#include "android-base/macros.h" |
| 27 |
#include "android-base/unique_fd.h" |
| 28 |
|
| 29 |
namespace aapt { |
| 30 |
namespace io { |
| 31 |
|
| 32 |
constexpr size_t kDefaultBufferCapacity = 4096u; |
| 33 |
|
| 34 |
class FileInputStream : public InputStream { |
| 35 |
public: |
| 36 |
explicit FileInputStream(const std::string& path, |
| 37 |
size_t buffer_capacity = kDefaultBufferCapacity); |
| 38 |
|
| 39 |
|
| 40 |
explicit FileInputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity); |
| 41 |
|
| 42 |
bool Next(const void** data, size_t* size) override; |
| 43 |
|
| 44 |
void BackUp(size_t count) override; |
| 45 |
|
| 46 |
size_t ByteCount() const override; |
| 47 |
|
| 48 |
bool HadError() const override; |
| 49 |
|
| 50 |
std::string GetError() const override; |
| 51 |
|
| 52 |
private: |
| 53 |
DISALLOW_COPY_AND_ASSIGN(FileInputStream); |
| 54 |
|
| 55 |
android::base::unique_fd fd_; |
| 56 |
std::string error_; |
| 57 |
std::unique_ptr<uint8_t[]> buffer_; |
| 58 |
size_t buffer_capacity_ = 0u; |
| 59 |
size_t buffer_offset_ = 0u; |
| 60 |
size_t buffer_size_ = 0u; |
| 61 |
size_t total_byte_count_ = 0u; |
| 62 |
}; |
| 63 |
|
| 64 |
class FileOutputStream : public OutputStream { |
| 65 |
public: |
| 66 |
explicit FileOutputStream(const std::string& path, |
| 67 |
size_t buffer_capacity = kDefaultBufferCapacity); |
| 68 |
|
| 69 |
|
| 70 |
explicit FileOutputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity); |
| 71 |
|
| 72 |
|
| 73 |
explicit FileOutputStream(android::base::unique_fd fd, |
| 74 |
size_t buffer_capacity = kDefaultBufferCapacity); |
| 75 |
|
| 76 |
~FileOutputStream(); |
| 77 |
|
| 78 |
bool Next(void** data, size_t* size) override; |
| 79 |
|
| 80 |
|
| 81 |
bool Flush(); |
| 82 |
|
| 83 |
void BackUp(size_t count) override; |
| 84 |
|
| 85 |
size_t ByteCount() const override; |
| 86 |
|
| 87 |
bool HadError() const override; |
| 88 |
|
| 89 |
std::string GetError() const override; |
| 90 |
|
| 91 |
private: |
| 92 |
DISALLOW_COPY_AND_ASSIGN(FileOutputStream); |
| 93 |
|
| 94 |
bool FlushImpl(); |
| 95 |
|
| 96 |
android::base::unique_fd owned_fd_; |
| 97 |
int fd_; |
| 98 |
std::string error_; |
| 99 |
std::unique_ptr<uint8_t[]> buffer_; |
| 100 |
size_t buffer_capacity_ = 0u; |
| 101 |
size_t buffer_offset_ = 0u; |
| 102 |
size_t total_byte_count_ = 0u; |
| 103 |
}; |
| 104 |
|
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#endif |