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; |
| 20 |
19 bool isAtEnd() const override; | 21 bool isAtEnd() const override; |
20 | 22 |
21 bool rewind() override; | 23 bool rewind() override; |
22 | 24 |
23 bool hasPosition() const override { return true; } | 25 bool hasPosition() const override { return true; } |
24 | 26 |
25 size_t getPosition() const override { return fOffset; } | 27 size_t getPosition() const override { return fOffset; } |
26 | 28 |
27 bool hasLength() const override { return fHasLength; } | 29 bool hasLength() const override { return fHasLength; } |
28 | 30 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 150 |
149 // 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 |
150 // supported, so we can go ahead and free the memory. | 152 // supported, so we can go ahead and free the memory. |
151 if (bytesReadDirectly > 0) { | 153 if (bytesReadDirectly > 0) { |
152 sk_free(fBuffer.detach()); | 154 sk_free(fBuffer.detach()); |
153 } | 155 } |
154 | 156 |
155 return bytesReadDirectly; | 157 return bytesReadDirectly; |
156 } | 158 } |
157 | 159 |
| 160 bool FrontBufferedStream::peek(void* dst, size_t size) const { |
| 161 // Keep track of the offset so we can return to it. |
| 162 const size_t start = fOffset; |
| 163 if (start + size > fBufferSize) { |
| 164 // This stream is not able to buffer enough. |
| 165 return false; |
| 166 } |
| 167 FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this); |
| 168 SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(dst, size); |
| 169 SkASSERT(bytesRead == size); |
| 170 nonConstThis->fOffset = start; |
| 171 return true; |
| 172 } |
| 173 |
158 size_t FrontBufferedStream::read(void* voidDst, size_t size) { | 174 size_t FrontBufferedStream::read(void* voidDst, size_t size) { |
159 // Cast voidDst to a char* for easy addition. | 175 // Cast voidDst to a char* for easy addition. |
160 char* dst = reinterpret_cast<char*>(voidDst); | 176 char* dst = reinterpret_cast<char*>(voidDst); |
161 SkDEBUGCODE(const size_t totalSize = size;) | 177 SkDEBUGCODE(const size_t totalSize = size;) |
162 const size_t start = fOffset; | 178 const size_t start = fOffset; |
163 | 179 |
164 // First, read any data that was previously buffered. | 180 // First, read any data that was previously buffered. |
165 if (fOffset < fBufferedSoFar) { | 181 if (fOffset < fBufferedSoFar) { |
166 const size_t bytesCopied = this->readFromBuffer(dst, size); | 182 const size_t bytesCopied = this->readFromBuffer(dst, size); |
167 | 183 |
(...skipping 21 matching lines...) Expand all Loading... |
189 } | 205 } |
190 | 206 |
191 if (size > 0 && !fStream->isAtEnd()) { | 207 if (size > 0 && !fStream->isAtEnd()) { |
192 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); | 208 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); |
193 SkDEBUGCODE(size -= bytesReadDirectly;) | 209 SkDEBUGCODE(size -= bytesReadDirectly;) |
194 SkASSERT(size + (fOffset - start) == totalSize); | 210 SkASSERT(size + (fOffset - start) == totalSize); |
195 } | 211 } |
196 | 212 |
197 return fOffset - start; | 213 return fOffset - start; |
198 } | 214 } |
OLD | NEW |