OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFrontBufferedStream.h" | 8 #include "SkFrontBufferedStream.h" |
9 #include "SkStream.h" | 9 #include "SkStream.h" |
10 #include "SkTemplates.h" | 10 #include "SkTemplates.h" |
11 | 11 |
12 class FrontBufferedStream : public SkStreamRewindable { | 12 class FrontBufferedStream : public SkStreamRewindable { |
13 public: | 13 public: |
14 // Called by Create. | 14 // Called by Create. |
15 FrontBufferedStream(SkStream*, size_t bufferSize); | 15 FrontBufferedStream(SkStream*, size_t bufferSize); |
16 | 16 |
17 size_t read(void* buffer, size_t size) override; | 17 size_t read(void* buffer, size_t size) override; |
18 | 18 |
19 bool peek(void* buffer, size_t size) const override; | 19 size_t peek(void* buffer, size_t size) const override; |
20 | 20 |
21 bool isAtEnd() const override; | 21 bool isAtEnd() const override; |
22 | 22 |
23 bool rewind() override; | 23 bool rewind() override; |
24 | 24 |
25 bool hasPosition() const override { return true; } | 25 bool hasPosition() const override { return true; } |
26 | 26 |
27 size_t getPosition() const override { return fOffset; } | 27 size_t getPosition() const override { return fOffset; } |
28 | 28 |
29 bool hasLength() const override { return fHasLength; } | 29 bool hasLength() const override { return fHasLength; } |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 150 |
151 // If we have read past the end of the buffer, rewinding is no longer | 151 // If we have read past the end of the buffer, rewinding is no longer |
152 // supported, so we can go ahead and free the memory. | 152 // supported, so we can go ahead and free the memory. |
153 if (bytesReadDirectly > 0) { | 153 if (bytesReadDirectly > 0) { |
154 sk_free(fBuffer.detach()); | 154 sk_free(fBuffer.detach()); |
155 } | 155 } |
156 | 156 |
157 return bytesReadDirectly; | 157 return bytesReadDirectly; |
158 } | 158 } |
159 | 159 |
160 bool FrontBufferedStream::peek(void* dst, size_t size) const { | 160 size_t FrontBufferedStream::peek(void* dst, size_t size) const { |
161 // Keep track of the offset so we can return to it. | 161 // Keep track of the offset so we can return to it. |
162 const size_t start = fOffset; | 162 const size_t start = fOffset; |
163 if (start + size > fBufferSize) { | 163 |
164 // This stream is not able to buffer enough. | 164 if (start >= fBufferSize) { |
165 return false; | 165 // This stream is not able to buffer. |
| 166 return 0; |
166 } | 167 } |
| 168 |
| 169 size = SkTMin(size, fBufferSize - start); |
167 FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this); | 170 FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this); |
168 SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(dst, size); | 171 const size_t bytesRead = nonConstThis->read(dst, size); |
169 SkASSERT(bytesRead == size); | |
170 nonConstThis->fOffset = start; | 172 nonConstThis->fOffset = start; |
171 return true; | 173 return bytesRead; |
172 } | 174 } |
173 | 175 |
174 size_t FrontBufferedStream::read(void* voidDst, size_t size) { | 176 size_t FrontBufferedStream::read(void* voidDst, size_t size) { |
175 // Cast voidDst to a char* for easy addition. | 177 // Cast voidDst to a char* for easy addition. |
176 char* dst = reinterpret_cast<char*>(voidDst); | 178 char* dst = reinterpret_cast<char*>(voidDst); |
177 SkDEBUGCODE(const size_t totalSize = size;) | 179 SkDEBUGCODE(const size_t totalSize = size;) |
178 const size_t start = fOffset; | 180 const size_t start = fOffset; |
179 | 181 |
180 // First, read any data that was previously buffered. | 182 // First, read any data that was previously buffered. |
181 if (fOffset < fBufferedSoFar) { | 183 if (fOffset < fBufferedSoFar) { |
(...skipping 23 matching lines...) Expand all Loading... |
205 } | 207 } |
206 | 208 |
207 if (size > 0 && !fStream->isAtEnd()) { | 209 if (size > 0 && !fStream->isAtEnd()) { |
208 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); | 210 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); |
209 SkDEBUGCODE(size -= bytesReadDirectly;) | 211 SkDEBUGCODE(size -= bytesReadDirectly;) |
210 SkASSERT(size + (fOffset - start) == totalSize); | 212 SkASSERT(size + (fOffset - start) == totalSize); |
211 } | 213 } |
212 | 214 |
213 return fOffset - start; | 215 return fOffset - start; |
214 } | 216 } |
OLD | NEW |