Home | History | Annotate | Download | only in bsdiff
      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_MEMORY_FILE_H_
      6 #define _BSDIFF_MEMORY_FILE_H_
      7 
      8 #include <memory>
      9 #include <vector>
     10 
     11 #include "file_interface.h"
     12 
     13 namespace bsdiff {
     14 
     15 class MemoryFile : public FileInterface {
     16  public:
     17   // Creates a MemoryFile based on the underlying |file| passed. The MemoryFile
     18   // will cache all the write in memory and write it to to |file| when it's
     19   // closed. MemoryFile does not support read and seek.
     20   // |size| should be the estimated total file size, it is used to reserve
     21   // buffer space.
     22   MemoryFile(std::unique_ptr<FileInterface> file, size_t size);
     23 
     24   ~MemoryFile() 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_;
     36 
     37   std::vector<uint8_t> buffer_;
     38 };
     39 
     40 }  // namespace bsdiff
     41 
     42 #endif  // _BSDIFF_MEMORY_FILE_H_
     43