1 // Copyright 2017 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 SRC_FILE_STREAM_H_ 6 #define SRC_FILE_STREAM_H_ 7 8 #include <string> 9 #include <utility> 10 11 #include "puffin/common.h" 12 #include "puffin/stream.h" 13 14 namespace puffin { 15 16 // A very simple class for reading and writing data into a file descriptor. 17 class FileStream : public StreamInterface { 18 public: FileStream(int fd)19 explicit FileStream(int fd) : fd_(fd) { Seek(0); } 20 ~FileStream() override = default; 21 22 static UniqueStreamPtr Open(const std::string& path, bool read, bool write); 23 24 bool GetSize(uint64_t* size) const override; 25 bool GetOffset(uint64_t* offset) const override; 26 bool Seek(uint64_t offset) override; 27 bool Read(void* buffer, size_t length) override; 28 bool Write(const void* buffer, size_t length) override; 29 bool Close() override; 30 31 protected: 32 FileStream() = default; 33 34 private: 35 // The file descriptor. 36 int fd_; 37 38 DISALLOW_COPY_AND_ASSIGN(FileStream); 39 }; 40 41 } // namespace puffin 42 43 #endif // SRC_FILE_STREAM_H_ 44