Index: include/core/SkStream.h |
diff --git a/include/core/SkStream.h b/include/core/SkStream.h |
index c227765b363624afbf7a3f98f39d480c7f05726d..7e89ef94095eafeb31d7b45164defb551e1b84bd 100644 |
--- a/include/core/SkStream.h |
+++ b/include/core/SkStream.h |
@@ -63,6 +63,34 @@ public: |
return this->read(NULL, size); |
} |
+ /** |
+ * Return value for peek() if the stream does not support peeking, or if |
+ * it cannot from this offset. |
+ */ |
+ static const size_t PEEK_UNSUPPORTED = (size_t) -1; |
bungeman-skia
2015/03/31 22:04:48
I think we tend toward enums with 'k' prefixes (th
scroggo
2015/04/01 18:38:31
I'm fine with renaming them to use 'k' prefixes (p
|
+ |
+ /** |
+ * Return value for peek() if the stream encountered a fatal error. The |
+ * stream is no unreliable. |
+ */ |
+ static const size_t PEEK_FAILURE = (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, PEEK_FAILURE 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 PEEK_FAILURE was returned). |
+ * If peeking is unsupported, returns PEEK_UNSUPPORTED |
+ * @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 PEEK_UNSUPPORTED; } |
bungeman-skia
2015/03/31 22:04:48
We already have the pattern of either returning bo
|
+ |
/** 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 +172,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 PEEK_FAILURE; |
+ } |
+ return bytesRead; |
+ } |
}; |
/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ |