aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkStream.h
diff options
context:
space:
mode:
authorGravatar scroggo <scroggo@google.com>2015-12-07 11:37:13 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-12-07 11:37:13 -0800
commitd61c384342aaf7facf31b948db98acc32d81d21b (patch)
tree76ed2ef565c4786b83b368974f77b695d99376ea /include/core/SkStream.h
parent3660d534516e4687546a43ac80d0ae40bc68dae7 (diff)
Allow SkStream::peek() to partially succeed
If the stream can peek less than requested, peek that amount. Return the number of bytes peeked. This simplifies crrev.com/1472123002. For a stream that is smaller than 14 bytes, it can successfully peek, meaning the client will not need to fall back to read() + rewind(), which may fail if the stream can peek but not rewind. This CL revives code from patch set 3 of crrev.com/1044953002, where I initially introduced peek() (including tests). Add a test for SkFrontBufferedStream that verifies that peeking does not make rewind() fail (i.e. by reading past the internal buffer). BUG=skia:3257 Review URL: https://codereview.chromium.org/1490923005
Diffstat (limited to 'include/core/SkStream.h')
-rw-r--r--include/core/SkStream.h17
1 files changed, 9 insertions, 8 deletions
diff --git a/include/core/SkStream.h b/include/core/SkStream.h
index 2279f9ff10..44de6e87e1 100644
--- a/include/core/SkStream.h
+++ b/include/core/SkStream.h
@@ -65,17 +65,18 @@ public:
/**
* Attempt to peek at size bytes.
- * If this stream supports peeking, and it can peek size bytes, copy size
- * bytes into buffer, and return true.
- * If the stream does not support peeking, or cannot peek size bytes,
- * return false and leave buffer unchanged.
+ * If this stream supports peeking, copy min(size, peekable bytes) into
+ * buffer, and return the number of bytes copied.
+ * If the stream does not support peeking, or cannot peek any bytes,
+ * return 0 and leave buffer unchanged.
* The stream is guaranteed to be in the same visible state after this
* call, regardless of success or failure.
- * @param buffer Must not be NULL. Destination to copy bytes.
+ * @param buffer Must not be NULL, and must be at least size bytes. Destination
+ * to copy bytes.
* @param size Number of bytes to copy.
- * @return Whether the peek was performed.
+ * @return The number of bytes peeked/copied.
*/
- virtual bool peek(void* /* buffer */, size_t /* size */) const { return false; }
+ virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
/** 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)
@@ -325,7 +326,7 @@ public:
size_t read(void* buffer, size_t size) override;
bool isAtEnd() const override;
- bool peek(void* buffer, size_t size) const override;
+ size_t peek(void* buffer, size_t size) const override;
bool rewind() override;
SkMemoryStream* duplicate() const override;