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*/) { | |
17 reader_ = reader; | |
18 UpdateInternals(position_); | |
19 } | |
20 | |
21 size_t SegmentStream::read(void* buffer, size_t size) { | |
22 if (is_cleared_) | |
23 return 0; | |
24 | |
25 size = std::min(size, reader_->size() - position_); | |
26 | |
27 size_t bytes_advanced = 0; | |
28 if (!buffer) { // skipping, not reading | |
29 bytes_advanced = size; | |
30 } else { | |
31 bytes_advanced = peek(buffer, size); | |
32 } | |
33 | |
34 position_ += bytes_advanced; | |
35 UpdateInternals(position_); | |
36 | |
37 return bytes_advanced; | |
38 } | |
39 | |
40 size_t SegmentStream::peek(void* buffer, size_t size) const { | |
41 if (is_cleared_) | |
42 return 0; | |
43 | |
44 size = std::min(size, reader_->size() - position_); | |
45 | |
46 size_t peek_position = position_; | |
47 size_t total_bytes_peeked = 0; | |
48 char* buffer_as_char_ptr = reinterpret_cast<char*>(buffer); | |
49 while (size) { | |
50 const char* segment = nullptr; | |
51 size_t bytes_peeked = reader_->GetSomeData(segment, peek_position); | |
52 if (!bytes_peeked) | |
53 break; | |
54 if (bytes_peeked > size) | |
55 bytes_peeked = size; | |
56 | |
57 memcpy(buffer_as_char_ptr, segment, bytes_peeked); | |
58 buffer_as_char_ptr += bytes_peeked; | |
59 size -= bytes_peeked; | |
60 total_bytes_peeked += bytes_peeked; | |
61 peek_position += bytes_peeked; | |
62 } | |
63 | |
64 return total_bytes_peeked; | |
65 } | |
66 | |
67 bool SegmentStream::rewind() { | |
68 UpdateInternals(0); | |
69 return true; | |
70 } | |
71 | |
72 bool SegmentStream::seek(size_t position) { | |
73 size_t new_position = position; | |
scroggo_chromium
2017/04/21 20:09:32
nit: Why not reuse the variable "position"?
cblume
2017/04/21 23:47:38
Done.
| |
74 if (reader_) { | |
75 new_position = std::min(position, reader_->size()); | |
76 } | |
77 UpdateInternals(new_position); | |
78 return true; | |
79 } | |
80 | |
81 bool SegmentStream::move(long offset) { | |
82 long absolute_position = position_ + offset; | |
83 | |
84 // clamp inside the bounds of the buffer size | |
85 absolute_position = std::max(absolute_position, 0l); | |
86 absolute_position = | |
87 std::min(static_cast<size_t>(absolute_position), reader_->size()); | |
88 | |
89 UpdateInternals(absolute_position); | |
90 return true; | |
91 } | |
92 | |
93 void SegmentStream::UpdateInternals(size_t new_position) { | |
94 position_ = new_position; | |
95 if (reader_) { | |
96 const size_t reader_size = reader_->size(); | |
97 has_read_all_contents_ = position_ >= reader_size; | |
98 is_cleared_ = position_ > reader_size; | |
99 } else { | |
100 has_read_all_contents_ = true; | |
101 is_cleared_ = true; | |
102 } | |
103 } | |
104 | |
105 } // namespace blink | |
OLD | NEW |