Index: include/core/SkStream.h |
diff --git a/include/core/SkStream.h b/include/core/SkStream.h |
index c227765b363624afbf7a3f98f39d480c7f05726d..82c58267b2e0be04a3a9b93d9fd4cf09d8b951f4 100644 |
--- a/include/core/SkStream.h |
+++ b/include/core/SkStream.h |
@@ -63,6 +63,37 @@ public: |
return this->read(NULL, size); |
} |
+ enum PeekResult : size_t { |
+ /** |
+ * Return value for peek() if the stream does not support peeking, or if |
+ * it cannot from this offset. |
+ */ |
+ kUnsupported_PeekResult = (size_t) -1, |
+ |
+ /** |
+ * Return value for peek() if the stream encountered a fatal error. The |
+ * stream is no unreliable. |
+ */ |
+ kFailed_PeekResult = (size_t) -2, |
+ }; |
+ |
+ |
+ /** |
+ * Attempt to peek at size bytes. |
+ * If this stream supports peeking, copy up to size bytes into buffer. If |
+ * less than size is returned, the stream is less than size bytes from the |
+ * end or the stream has reached its limit for how many bytes it can peek. |
+ * If there is a fatal error, kFailed_PeekResult is returned, and the stream |
+ * cannot be used anymore. |
+ * After this call, the stream will have the same state it did beforehand, |
+ * unless there was an error (and kFailed_PeekResult was returned). |
+ * If peeking is unsupported, returns kUnsupported_PeekResult |
+ * @param buffer Must not be NULL. Destination to copy bytes. |
+ * @param size Number of bytes to copy. |
+ * @return the number of bytes actually read, or an error message. |
+ */ |
+ virtual size_t peek(void* buffer, size_t size) { return kUnsupported_PeekResult; } |
+ |
/** Returns true when all the bytes in the stream have been read. |
* This may return true early (when there are no more bytes to be read) |
* or late (after the first unsuccessful read). |
@@ -144,6 +175,16 @@ public: |
bool seek(size_t position) override = 0; |
bool move(long offset) override = 0; |
SkStreamSeekable* fork() const override = 0; |
+ // Default implementation reads and then resets the position. |
+ size_t peek(void* buffer, size_t size) override { |
+ SkASSERT(buffer != NULL); |
+ const size_t position = this->getPosition(); |
+ const size_t bytesRead = this->read(buffer, size); |
+ if (!this->seek(position)) { |
+ return kFailed_PeekResult; |
+ } |
+ return bytesRead; |
+ } |
}; |
/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ |