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 bool all_contents_received) { | |
scroggo_chromium
2017/04/17 20:04:56
It looks like this parameter is unused.
cblume
2017/04/20 03:42:26
Done.
scroggo_chromium
2017/04/21 20:09:31
You've commented out the parameter, but why not re
cblume
2017/04/21 23:47:38
Oops. Thought it was part of an interface we were
| |
17 reader_ = reader; | |
18 if (reader) { | |
19 has_read_all_contents_ = reader->size() == position_; | |
scroggo_chromium
2017/04/17 20:04:56
If the position > reader->size(), should has_read_
cblume
2017/04/20 03:42:26
Done.
| |
20 is_cleared_ = position_ > reader_->size(); | |
21 } else { | |
22 has_read_all_contents_ = true; | |
23 is_cleared_ = true; | |
24 } | |
25 } | |
26 | |
27 size_t SegmentStream::read(void* buffer, size_t size) { | |
28 if (is_cleared_) | |
29 return 0; | |
30 | |
31 size = std::min(size, reader_->size() - position_); | |
32 | |
33 size_t bytes_advanced = 0; | |
34 if (!buffer) { // skipping, not reading | |
35 bytes_advanced = size; | |
36 } else { | |
37 bytes_advanced = peek(buffer, size); | |
38 } | |
39 | |
40 position_ += bytes_advanced; | |
41 has_read_all_contents_ = position_ == reader_->size(); | |
42 | |
43 return bytes_advanced; | |
44 } | |
45 | |
46 size_t SegmentStream::peek(void* buffer, size_t size) const { | |
47 if (is_cleared_) | |
48 return 0; | |
49 | |
50 size = std::min(size, reader_->size() - position_); | |
51 | |
52 size_t peek_position = position_; | |
53 size_t total_bytes_peeked = 0; | |
54 char* buffer_as_char_ptr = reinterpret_cast<char*>(buffer); | |
55 while (size) { | |
56 const char* segment = nullptr; | |
57 size_t bytes_peeked = reader_->GetSomeData(segment, peek_position); | |
58 if (!bytes_peeked) | |
59 break; | |
60 if (bytes_peeked > size) | |
61 bytes_peeked = size; | |
62 | |
63 memcpy(buffer_as_char_ptr, segment, bytes_peeked); | |
64 buffer_as_char_ptr += bytes_peeked; | |
65 size -= bytes_peeked; | |
66 total_bytes_peeked += bytes_peeked; | |
67 peek_position += bytes_peeked; | |
68 } | |
69 | |
70 return total_bytes_peeked; | |
71 } | |
72 | |
73 bool SegmentStream::rewind() { | |
74 position_ = 0; | |
75 has_read_all_contents_ = true; | |
scroggo_chromium
2017/04/17 20:04:56
Why is this set to true? Should this be in an "els
cblume
2017/04/20 03:42:26
Done.
| |
76 if (reader_) | |
77 has_read_all_contents_ = position_ == reader_->size(); | |
78 | |
79 return true; | |
80 } | |
81 | |
82 bool SegmentStream::seek(size_t position) { | |
83 has_read_all_contents_ = true; | |
scroggo_chromium
2017/04/17 20:04:56
Similarly, why did this get set to true?
cblume
2017/04/20 03:42:26
Done.
| |
84 if (reader_) { | |
85 position = std::min(position, reader_->size()); | |
86 has_read_all_contents_ = position == reader_->size(); | |
scroggo_chromium
2017/04/17 20:04:56
This gets repeated a lot. Maybe add an inlined met
cblume
2017/04/20 03:42:26
Done.
| |
87 } | |
88 position_ = position; | |
scroggo_chromium
2017/04/17 20:04:56
It seems odd to me that we correct position if the
cblume
2017/04/20 03:42:26
I agree.
We cannot correct without a reader. So my
scroggo_chromium
2017/04/21 20:09:31
Maybe add a comment?
cblume
2017/04/21 23:47:38
Done.
| |
89 | |
90 return true; | |
91 } | |
92 | |
93 bool SegmentStream::move(long offset) { | |
94 long absolute_position = position_ + offset; | |
95 | |
96 // clamp inside the bounds of the buffer size | |
97 absolute_position = std::max(absolute_position, 0l); | |
98 absolute_position = | |
99 std::min(static_cast<size_t>(absolute_position), reader_->size()); | |
100 | |
101 position_ = absolute_position; | |
102 has_read_all_contents_ = position_ == reader_->size(); | |
103 | |
104 return true; | |
105 } | |
106 | |
107 } // namespace blink | |
OLD | NEW |