OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 The Chromium 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 #include "platform/image-decoders/SegmentStream.h" |
| 6 |
| 7 namespace blink { |
| 8 |
| 9 SegmentStream::SegmentStream() |
| 10 : reader_(), |
| 11 position_(0), |
| 12 has_read_all_contents_(true), |
| 13 is_cleared_(true) {} |
| 14 |
| 15 void SegmentStream::SetReader(SegmentReader* reader) { |
| 16 reader_ = reader; |
| 17 UpdatePositionState(position_); |
| 18 } |
| 19 |
| 20 size_t SegmentStream::read(void* buffer, size_t size) { |
| 21 if (is_cleared_) |
| 22 return 0; |
| 23 |
| 24 size = std::min(size, reader_->size() - position_); |
| 25 |
| 26 size_t bytes_advanced = 0; |
| 27 if (!buffer) { // skipping, not reading |
| 28 bytes_advanced = size; |
| 29 } else { |
| 30 bytes_advanced = peek(buffer, size); |
| 31 } |
| 32 |
| 33 UpdatePositionState(position_ + bytes_advanced); |
| 34 |
| 35 return bytes_advanced; |
| 36 } |
| 37 |
| 38 size_t SegmentStream::peek(void* buffer, size_t size) const { |
| 39 if (is_cleared_) |
| 40 return 0; |
| 41 |
| 42 size = std::min(size, reader_->size() - position_); |
| 43 |
| 44 size_t peek_position = position_; |
| 45 size_t total_bytes_peeked = 0; |
| 46 char* buffer_as_char_ptr = reinterpret_cast<char*>(buffer); |
| 47 while (size) { |
| 48 const char* segment = nullptr; |
| 49 size_t bytes_peeked = reader_->GetSomeData(segment, peek_position); |
| 50 if (!bytes_peeked) |
| 51 break; |
| 52 if (bytes_peeked > size) |
| 53 bytes_peeked = size; |
| 54 |
| 55 memcpy(buffer_as_char_ptr, segment, bytes_peeked); |
| 56 buffer_as_char_ptr += bytes_peeked; |
| 57 size -= bytes_peeked; |
| 58 total_bytes_peeked += bytes_peeked; |
| 59 peek_position += bytes_peeked; |
| 60 } |
| 61 |
| 62 return total_bytes_peeked; |
| 63 } |
| 64 |
| 65 bool SegmentStream::rewind() { |
| 66 UpdatePositionState(0); |
| 67 return true; |
| 68 } |
| 69 |
| 70 bool SegmentStream::seek(size_t position) { |
| 71 UpdatePositionState(position); |
| 72 return true; |
| 73 } |
| 74 |
| 75 bool SegmentStream::move(long offset) { |
| 76 long absolute_position = position_ + offset; |
| 77 |
| 78 absolute_position = std::max(absolute_position, 0l); |
| 79 UpdatePositionState(absolute_position); |
| 80 return true; |
| 81 } |
| 82 |
| 83 void SegmentStream::UpdatePositionState(size_t new_position) { |
| 84 position_ = new_position; |
| 85 if (reader_) { |
| 86 const size_t reader_size = reader_->size(); |
| 87 has_read_all_contents_ = position_ >= reader_size; |
| 88 is_cleared_ = position_ > reader_size; |
| 89 } else { |
| 90 has_read_all_contents_ = true; |
| 91 is_cleared_ = true; |
| 92 } |
| 93 } |
| 94 |
| 95 } // namespace blink |
OLD | NEW |