1 #include "image_io/base/ostream_ref_data_destination.h"
2 
3 #include "image_io/base/data_range.h"
4 #include "image_io/base/data_segment.h"
5 
6 namespace photos_editing_formats {
7 namespace image_io {
8 
9 using std::ostream;
10 
StartTransfer()11 void OStreamRefDataDestination::StartTransfer() {}
12 
Transfer(const DataRange & transfer_range,const DataSegment & data_segment)13 DataDestination::TransferStatus OStreamRefDataDestination::Transfer(
14     const DataRange& transfer_range, const DataSegment& data_segment) {
15   if (transfer_range.IsValid() && !HasError()) {
16     size_t bytes_written = 0;
17     size_t bytes_to_write = transfer_range.GetLength();
18     const Byte* buffer = data_segment.GetBuffer(transfer_range.GetBegin());
19     if (buffer) {
20       ostream::pos_type prewrite_pos = ostream_ref_.tellp();
21       ostream_ref_.write(reinterpret_cast<const char*>(buffer), bytes_to_write);
22       ostream::pos_type postwrite_pos = ostream_ref_.tellp();
23       if (postwrite_pos != EOF) {
24         bytes_written = ostream_ref_.tellp() - prewrite_pos;
25         bytes_transferred_ += bytes_written;
26       }
27     }
28     if (bytes_written != bytes_to_write) {
29       if (message_handler_) {
30         message_handler_->ReportMessage(Message::kStdLibError, name_);
31       }
32       has_error_ = true;
33       return kTransferError;
34     }
35   }
36   return kTransferOk;
37 }
38 
FinishTransfer()39 void OStreamRefDataDestination::FinishTransfer() {
40     ostream_ref_.flush();
41 }
42 
43 }  // namespace image_io
44 }  // namespace photos_editing_formats
45