Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Unified Diff: src/core/SkStream.cpp

Issue 1490923005: Allow SkStream::peek() to partially succeed (Closed) Base URL: https://skia.googlesource.com/skia.git@wbmp
Patch Set: Use the correct return type Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/core/SkStream.h ('k') | src/utils/SkFrontBufferedStream.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkStream.cpp
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 05867d91bfe53191ce65e15de1dc8d23d49a9c5a..fff8f3382209b9fd9509d55a916b711022d7721e 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 {
« no previous file with comments | « include/core/SkStream.h ('k') | src/utils/SkFrontBufferedStream.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698