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 UpdateInternals(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 position_ += bytes_advanced; | |
34 UpdateInternals(position_); | |
35 | |
36 return bytes_advanced; | |
37 } | |
38 | |
39 size_t SegmentStream::peek(void* buffer, size_t size) const { | |
40 if (is_cleared_) | |
41 return 0; | |
42 | |
43 size = std::min(size, reader_->size() - position_); | |
44 | |
45 size_t peek_position = position_; | |
46 size_t total_bytes_peeked = 0; | |
47 char* buffer_as_char_ptr = reinterpret_cast<char*>(buffer); | |
48 while (size) { | |
49 const char* segment = nullptr; | |
50 size_t bytes_peeked = reader_->GetSomeData(segment, peek_position); | |
51 if (!bytes_peeked) | |
52 break; | |
53 if (bytes_peeked > size) | |
54 bytes_peeked = size; | |
55 | |
56 memcpy(buffer_as_char_ptr, segment, bytes_peeked); | |
57 buffer_as_char_ptr += bytes_peeked; | |
58 size -= bytes_peeked; | |
59 total_bytes_peeked += bytes_peeked; | |
60 peek_position += bytes_peeked; | |
61 } | |
62 | |
63 return total_bytes_peeked; | |
64 } | |
65 | |
66 bool SegmentStream::rewind() { | |
67 UpdateInternals(0); | |
68 return true; | |
69 } | |
70 | |
71 bool SegmentStream::seek(size_t position) { | |
72 // If we do not have a reader, we can either: | |
73 // * ignore this input, or | |
74 // * blindly follow it without checking against bounds. | |
75 // 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
| |
76 if (reader_) { | |
77 position = std::min(position, reader_->size()); | |
78 } | |
79 UpdateInternals(position); | |
80 return true; | |
81 } | |
82 | |
83 bool SegmentStream::move(long offset) { | |
84 long absolute_position = position_ + offset; | |
85 | |
86 // clamp inside the bounds of the buffer size | |
87 absolute_position = std::max(absolute_position, 0l); | |
88 absolute_position = | |
89 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
| |
90 | |
91 UpdateInternals(absolute_position); | |
92 return true; | |
93 } | |
94 | |
95 void SegmentStream::UpdateInternals(size_t new_position) { | |
96 position_ = new_position; | |
97 if (reader_) { | |
98 const size_t reader_size = reader_->size(); | |
99 has_read_all_contents_ = position_ >= reader_size; | |
100 is_cleared_ = position_ > reader_size; | |
101 } else { | |
102 has_read_all_contents_ = true; | |
103 is_cleared_ = true; | |
104 } | |
105 } | |
106 | |
107 } // namespace blink | |
OLD | NEW |