aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/SkStream.cpp33
-rw-r--r--src/utils/SkFrontBufferedStream.cpp18
2 files changed, 25 insertions, 26 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 {
diff --git a/src/utils/SkFrontBufferedStream.cpp b/src/utils/SkFrontBufferedStream.cpp
index 0955cfaf71..e0b586a29d 100644
--- a/src/utils/SkFrontBufferedStream.cpp
+++ b/src/utils/SkFrontBufferedStream.cpp
@@ -16,7 +16,7 @@ public:
size_t read(void* buffer, size_t size) override;
- bool peek(void* buffer, size_t size) const override;
+ size_t peek(void* buffer, size_t size) const override;
bool isAtEnd() const override;
@@ -157,18 +157,20 @@ size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) {
return bytesReadDirectly;
}
-bool FrontBufferedStream::peek(void* dst, size_t size) const {
+size_t FrontBufferedStream::peek(void* dst, size_t size) const {
// Keep track of the offset so we can return to it.
const size_t start = fOffset;
- if (start + size > fBufferSize) {
- // This stream is not able to buffer enough.
- return false;
+
+ if (start >= fBufferSize) {
+ // This stream is not able to buffer.
+ return 0;
}
+
+ size = SkTMin(size, fBufferSize - start);
FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this);
- SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(dst, size);
- SkASSERT(bytesRead == size);
+ const size_t bytesRead = nonConstThis->read(dst, size);
nonConstThis->fOffset = start;
- return true;
+ return bytesRead;
}
size_t FrontBufferedStream::read(void* voidDst, size_t size) {