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 #ifdef SK_DEBUG | |
169 const size_t bytesRead = | |
170 #endif | |
171 nonConstThis->read(dst, size); | |
172 SkASSERT(bytesRead == size); | |
173 nonConstThis->fOffset = start; | |
174 return bytesRead; | |
scroggo
2015/04/02 19:49:43
Oops, this should be true. Good thing I tested in
| |
175 } | |
176 | |
158 size_t FrontBufferedStream::read(void* voidDst, size_t size) { | 177 size_t FrontBufferedStream::read(void* voidDst, size_t size) { |
159 // Cast voidDst to a char* for easy addition. | 178 // Cast voidDst to a char* for easy addition. |
160 char* dst = reinterpret_cast<char*>(voidDst); | 179 char* dst = reinterpret_cast<char*>(voidDst); |
161 SkDEBUGCODE(const size_t totalSize = size;) | 180 SkDEBUGCODE(const size_t totalSize = size;) |
162 const size_t start = fOffset; | 181 const size_t start = fOffset; |
163 | 182 |
164 // First, read any data that was previously buffered. | 183 // First, read any data that was previously buffered. |
165 if (fOffset < fBufferedSoFar) { | 184 if (fOffset < fBufferedSoFar) { |
166 const size_t bytesCopied = this->readFromBuffer(dst, size); | 185 const size_t bytesCopied = this->readFromBuffer(dst, size); |
167 | 186 |
(...skipping 21 matching lines...) Expand all Loading... | |
189 } | 208 } |
190 | 209 |
191 if (size > 0 && !fStream->isAtEnd()) { | 210 if (size > 0 && !fStream->isAtEnd()) { |
192 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre am(dst, size); | 211 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre am(dst, size); |
193 SkDEBUGCODE(size -= bytesReadDirectly;) | 212 SkDEBUGCODE(size -= bytesReadDirectly;) |
194 SkASSERT(size + (fOffset - start) == totalSize); | 213 SkASSERT(size + (fOffset - start) == totalSize); |
195 } | 214 } |
196 | 215 |
197 return fOffset - start; | 216 return fOffset - start; |
198 } | 217 } |
OLD | NEW |