Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 17 | #ifndef AAPT_IO_FILESTREAM_H |
| 18 | #define AAPT_IO_FILESTREAM_H |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 19 | |
| 20 | #include "io/Io.h" |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 25 | #include "android-base/file.h" // for O_BINARY |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 26 | #include "android-base/macros.h" |
| 27 | #include "android-base/unique_fd.h" |
| 28 | |
| 29 | namespace aapt { |
| 30 | namespace io { |
| 31 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 32 | constexpr size_t kDefaultBufferCapacity = 4096u; |
| 33 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 34 | class FileInputStream : public InputStream { |
| 35 | public: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 36 | explicit FileInputStream(const std::string& path, |
| 37 | size_t buffer_capacity = kDefaultBufferCapacity); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 39 | // Take ownership of `fd`. |
| 40 | explicit FileInputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 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_; |
| 59 | size_t buffer_offset_; |
| 60 | size_t buffer_size_; |
| 61 | size_t total_byte_count_; |
| 62 | }; |
| 63 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 64 | class FileOutputStream : public OutputStream { |
| 65 | public: |
| 66 | explicit FileOutputStream(const std::string& path, int mode = O_RDWR | O_CREAT | O_BINARY, |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 67 | size_t buffer_capacity = kDefaultBufferCapacity); |
| 68 | |
| 69 | // Does not take ownership of `fd`. |
| 70 | explicit FileOutputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 71 | |
| 72 | // Takes ownership of `fd`. |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 73 | explicit FileOutputStream(android::base::unique_fd fd, |
| 74 | size_t buffer_capacity = kDefaultBufferCapacity); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 75 | |
| 76 | ~FileOutputStream(); |
| 77 | |
| 78 | bool Next(void** data, size_t* size) override; |
| 79 | |
| 80 | // Immediately flushes out the contents of the buffer to disk. |
| 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 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 96 | android::base::unique_fd owned_fd_; |
| 97 | int fd_; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 98 | std::string error_; |
| 99 | std::unique_ptr<uint8_t[]> buffer_; |
| 100 | size_t buffer_capacity_; |
| 101 | size_t buffer_offset_; |
| 102 | size_t total_byte_count_; |
| 103 | }; |
| 104 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 105 | } // namespace io |
| 106 | } // namespace aapt |
| 107 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 108 | #endif // AAPT_IO_FILESTREAM_H |