Home | History | Annotate | Download | only in puffin
      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_INCLUDE_PUFFIN_STREAM_H_
      6 #define SRC_INCLUDE_PUFFIN_STREAM_H_
      7 
      8 #include <memory>
      9 
     10 #include "puffin/common.h"
     11 
     12 namespace puffin {
     13 
     14 // The base stream interface used by puffin for all operations. This interface
     15 // is designed to be as simple as possible.
     16 class StreamInterface {
     17  public:
     18   virtual ~StreamInterface() = default;
     19 
     20   // Returns the size of the stream.
     21   virtual bool GetSize(uint64_t* size) const = 0;
     22 
     23   // Returns the current offset in the stream where next read or write will
     24   // happen.
     25   virtual bool GetOffset(uint64_t* offset) const = 0;
     26 
     27   // Sets the offset in the stream for the next read or write. On error
     28   // returns |false|.
     29   virtual bool Seek(uint64_t offset) = 0;
     30 
     31   // Reads |length| bytes of data into |buffer|. On error, returns |false|.
     32   virtual bool Read(void* buffer, size_t length) = 0;
     33 
     34   // Writes |length| bytes of data into |buffer|. On error, returns |false|.
     35   virtual bool Write(const void* buffer, size_t length) = 0;
     36 
     37   // Closes the stream and cleans up all associated resources. On error, returns
     38   // |false|.
     39   virtual bool Close() = 0;
     40 };
     41 
     42 using UniqueStreamPtr = std::unique_ptr<StreamInterface>;
     43 using SharedStreamPtr = std::shared_ptr<StreamInterface>;
     44 
     45 }  // namespace puffin
     46 
     47 #endif  // SRC_INCLUDE_PUFFIN_STREAM_H_
     48