1 #ifndef IMAGE_IO_BASE_ISTREAM_REF_DATA_SOURCE_H_ // NOLINT 2 #define IMAGE_IO_BASE_ISTREAM_REF_DATA_SOURCE_H_ // NOLINT 3 4 #include <iostream> 5 6 #include "image_io/base/data_source.h" 7 8 namespace photos_editing_formats { 9 namespace image_io { 10 11 /// A DataSource that obtains data from an istream held as a reference. 12 class IStreamRefDataSource : public DataSource { 13 public: 14 /// Constructs an IStreamDataSource using the given istream. 15 /// @param istream_ref The istream from which to read. IStreamRefDataSource(std::istream & istream_ref)16 explicit IStreamRefDataSource(std::istream& istream_ref) 17 : istream_ref_(istream_ref) {} 18 IStreamRefDataSource(const IStreamRefDataSource&) = delete; 19 IStreamRefDataSource& operator=(const IStreamRefDataSource&) = delete; 20 21 void Reset() override; 22 std::shared_ptr<DataSegment> GetDataSegment(size_t begin, 23 size_t min_size) override; 24 TransferDataResult TransferData(const DataRange& data_range, size_t best_size, 25 DataDestination* data_destination) override; 26 27 private: 28 /// The worker function to create a DataSegment and fill it with the given 29 /// number of bytes read from the istream, starting at the given location. 30 /// @param begin The location in the istream at which to start reading. 31 /// @param count The number of bytes to read. 32 /// @return A DataSegment pointer, or nullptr if the read failed. 33 std::shared_ptr<DataSegment> Read(size_t begin, size_t count); 34 35 private: 36 /// The istream from which to read. 37 std::istream& istream_ref_; 38 39 /// The current data segment that was read in the GetDataSegment() function. 40 std::shared_ptr<DataSegment> current_data_segment_; 41 }; 42 43 } // namespace image_io 44 } // namespace photos_editing_formats 45 46 #endif // IMAGE_IO_BASE_ISTREAM_REF_DATA_SOURCE_H_ // NOLINT 47