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..f9205bdb617b5ed711aa038e49770e24c2c3ac16 |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp |
@@ -0,0 +1,95 @@ |
+// 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; |
+ UpdatePositionState(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); |
+ } |
+ |
+ UpdatePositionState(position_ + bytes_advanced); |
+ |
+ 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() { |
+ UpdatePositionState(0); |
+ return true; |
+} |
+ |
+bool SegmentStream::seek(size_t position) { |
+ UpdatePositionState(position); |
+ return true; |
+} |
+ |
+bool SegmentStream::move(long offset) { |
+ long absolute_position = position_ + offset; |
+ |
+ absolute_position = std::max(absolute_position, 0l); |
+ UpdatePositionState(absolute_position); |
+ return true; |
+} |
+ |
+void SegmentStream::UpdatePositionState(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 |