blob: 0b9261c18679122238d696e9b2f2ffdcebf13d2f [file] [log] [blame]
Sen Jiang716d5692016-05-09 16:43:34 -07001// 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 Deymoddf9db52017-03-02 16:10:41 -080011#include "bsdiff/file_interface.h"
Sen Jiang716d5692016-05-09 16:43:34 -070012
13namespace bsdiff {
14
15class 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_