Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef _BSDIFF_BUFFER_FILE_H_ |
| 6 | #define _BSDIFF_BUFFER_FILE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 11 | #include "bsdiff/file_interface.h" |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 12 | |
| 13 | namespace bsdiff { |
| 14 | |
| 15 | class BufferFile : public FileInterface { |
| 16 | public: |
| 17 | // Creates a write only BufferFile based on the underlying |file| passed. |
| 18 | // The BufferFile will cache all the write in a buffer and write everything |
| 19 | // to |file| at once upon closing. Read and Seek are not supported. |
| 20 | // |size| should be the estimated total file size, it is used to reserve |
| 21 | // buffer space. |
| 22 | BufferFile(std::unique_ptr<FileInterface> file, size_t size); |
| 23 | |
| 24 | ~BufferFile() override; |
| 25 | |
| 26 | // FileInterface overrides. |
| 27 | bool Read(void* buf, size_t count, size_t* bytes_read) override; |
| 28 | bool Write(const void* buf, size_t count, size_t* bytes_written) override; |
| 29 | bool Seek(off_t pos) override; |
| 30 | bool Close() override; |
| 31 | bool GetSize(uint64_t* size) override; |
| 32 | |
| 33 | private: |
| 34 | // The underlying FileInterace instance. |
| 35 | std::unique_ptr<FileInterface> file_ = nullptr; |
| 36 | std::vector<uint8_t> buffer_; |
| 37 | }; |
| 38 | |
| 39 | } // namespace bsdiff |
| 40 | |
| 41 | #endif // _BSDIFF_BUFFER_FILE_H_ |