Index: third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp |
diff --git a/third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp b/third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9885af1bc1691d485422457887bc1ff2e5e6499f |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp |
@@ -0,0 +1,107 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "platform/image-decoders/SegmentStream.h" |
+ |
+namespace blink { |
+ |
+SegmentStream::SegmentStream() |
+ : reader_(), |
+ position_(0), |
+ has_read_all_contents_(true), |
+ is_cleared_(true) {} |
+ |
+void SegmentStream::SetReader(SegmentReader* reader) { |
+ reader_ = reader; |
+ UpdateInternals(position_); |
+} |
+ |
+size_t SegmentStream::read(void* buffer, size_t size) { |
+ if (is_cleared_) |
+ return 0; |
+ |
+ size = std::min(size, reader_->size() - position_); |
+ |
+ size_t bytes_advanced = 0; |
+ if (!buffer) { // skipping, not reading |
+ bytes_advanced = size; |
+ } else { |
+ bytes_advanced = peek(buffer, size); |
+ } |
+ |
+ position_ += bytes_advanced; |
+ UpdateInternals(position_); |
+ |
+ return bytes_advanced; |
+} |
+ |
+size_t SegmentStream::peek(void* buffer, size_t size) const { |
+ if (is_cleared_) |
+ return 0; |
+ |
+ size = std::min(size, reader_->size() - position_); |
+ |
+ size_t peek_position = position_; |
+ size_t total_bytes_peeked = 0; |
+ char* buffer_as_char_ptr = reinterpret_cast<char*>(buffer); |
+ while (size) { |
+ const char* segment = nullptr; |
+ size_t bytes_peeked = reader_->GetSomeData(segment, peek_position); |
+ if (!bytes_peeked) |
+ break; |
+ if (bytes_peeked > size) |
+ bytes_peeked = size; |
+ |
+ memcpy(buffer_as_char_ptr, segment, bytes_peeked); |
+ buffer_as_char_ptr += bytes_peeked; |
+ size -= bytes_peeked; |
+ total_bytes_peeked += bytes_peeked; |
+ peek_position += bytes_peeked; |
+ } |
+ |
+ return total_bytes_peeked; |
+} |
+ |
+bool SegmentStream::rewind() { |
+ UpdateInternals(0); |
+ return true; |
+} |
+ |
+bool SegmentStream::seek(size_t position) { |
+ // If we do not have a reader, we can either: |
+ // * ignore this input, or |
+ // * blindly follow it without checking against bounds. |
+ // Given those two options, blindly following sounds better. |
scroggo_chromium
2017/04/24 20:25:07
What would happen if we blindly followed in the ca
cblume
2017/04/24 21:35:53
I like your idea. That would simplify things a lot
|
+ if (reader_) { |
+ position = std::min(position, reader_->size()); |
+ } |
+ UpdateInternals(position); |
+ return true; |
+} |
+ |
+bool SegmentStream::move(long offset) { |
+ long absolute_position = position_ + offset; |
+ |
+ // clamp inside the bounds of the buffer size |
+ absolute_position = std::max(absolute_position, 0l); |
+ absolute_position = |
+ std::min(static_cast<size_t>(absolute_position), reader_->size()); |
scroggo_chromium
2017/04/24 20:25:07
Should this one check for a null reader_, too?
Oh
cblume
2017/04/24 21:35:53
I don't think there is a good reason why. It is pr
|
+ |
+ UpdateInternals(absolute_position); |
+ return true; |
+} |
+ |
+void SegmentStream::UpdateInternals(size_t new_position) { |
+ position_ = new_position; |
+ if (reader_) { |
+ const size_t reader_size = reader_->size(); |
+ has_read_all_contents_ = position_ >= reader_size; |
+ is_cleared_ = position_ > reader_size; |
+ } else { |
+ has_read_all_contents_ = true; |
+ is_cleared_ = true; |
+ } |
+} |
+ |
+} // namespace blink |