aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkStream.cpp
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 /src/core/SkStream.cpp
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 'src/core/SkStream.cpp')
-rw-r--r--src/core/SkStream.cpp33
1 files changed, 15 insertions, 18 deletions
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 05867d91bf..fff8f33822 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -367,18 +367,14 @@ size_t SkMemoryStream::read(void* buffer, size_t size) {
return size;
}
-bool SkMemoryStream::peek(void* buffer, size_t size) const {
+size_t SkMemoryStream::peek(void* buffer, size_t size) const {
SkASSERT(buffer != nullptr);
- const size_t position = fOffset;
- if (size > fData->size() - position) {
- // The stream is not large enough to satisfy this request.
- return false;
- }
+
+ const size_t currentOffset = fOffset;
SkMemoryStream* nonConstThis = const_cast<SkMemoryStream*>(this);
- SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(buffer, size);
- SkASSERT(bytesRead == size);
- nonConstThis->fOffset = position;
- return true;
+ const size_t bytesRead = nonConstThis->read(buffer, size);
+ nonConstThis->fOffset = currentOffset;
+ return bytesRead;
}
bool SkMemoryStream::isAtEnd() const {
@@ -725,25 +721,26 @@ public:
return fOffset == fSize;
}
- bool peek(void* buff, size_t size) const override {
+ size_t peek(void* buff, size_t bytesToPeek) const override {
SkASSERT(buff != nullptr);
- if (fOffset + size > fSize) {
- return false;
- }
+
+ bytesToPeek = SkTMin(bytesToPeek, fSize - fOffset);
+
+ size_t bytesLeftToPeek = bytesToPeek;
char* buffer = static_cast<char*>(buff);
const SkDynamicMemoryWStream::Block* current = fCurrent;
size_t currentOffset = fCurrentOffset;
- while (size) {
+ while (bytesLeftToPeek) {
SkASSERT(current);
size_t bytesFromCurrent =
- SkTMin(current->written() - currentOffset, size);
+ SkTMin(current->written() - currentOffset, bytesLeftToPeek);
memcpy(buffer, current->start() + currentOffset, bytesFromCurrent);
- size -= bytesFromCurrent;
+ bytesLeftToPeek -= bytesFromCurrent;
buffer += bytesFromCurrent;
current = current->fNext;
currentOffset = 0;
}
- return true;
+ return bytesToPeek;
}
bool rewind() override {