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; | |
scroggo_chromium
2017/04/25 15:31:40
UpdateInternals also updates the position. Why not
cblume
2017/04/26 10:09:42
Done.
| |
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 UpdateInternals(position); | |
73 return true; | |
74 } | |
75 | |
76 bool SegmentStream::move(long offset) { | |
77 long absolute_position = position_ + offset; | |
78 | |
79 // clamp inside the bounds of the buffer size | |
scroggo_chromium
2017/04/25 15:31:40
This comment looks to be out of date. Might be unn
cblume
2017/04/26 10:09:41
Done.
| |
80 absolute_position = std::max(absolute_position, 0l); | |
81 UpdateInternals(absolute_position); | |
82 return true; | |
83 } | |
84 | |
85 void SegmentStream::UpdateInternals(size_t new_position) { | |
scroggo_chromium
2017/04/25 15:31:40
"UpdateInternals" sounds vague to me. What if we c
cblume
2017/04/26 10:09:42
I agree that there could be a better name.
I am t
scroggo_chromium
2017/04/26 19:02:22
That sounds better. What do you think of SetPositi
cblume
2017/04/26 19:16:06
SetPositionState sounds great. I'll go with that.
| |
86 position_ = new_position; | |
87 if (reader_) { | |
88 const size_t reader_size = reader_->size(); | |
89 has_read_all_contents_ = position_ >= reader_size; | |
90 is_cleared_ = position_ > reader_size; | |
91 } else { | |
92 has_read_all_contents_ = true; | |
93 is_cleared_ = true; | |
94 } | |
95 } | |
96 | |
97 } // namespace blink | |
OLD | NEW |