diff options
author | 2015-04-02 13:19:51 -0700 | |
---|---|---|
committer | 2015-04-02 13:19:51 -0700 | |
commit | 028a4135aa6404ccd3a494813befe6e1a35e5e6c (patch) | |
tree | c8fcabf479a597c8117fcad7a6a506d11beed9bd /src/utils | |
parent | f92ace90d89cc99b34162dda26be564e34ca80ef (diff) |
Add a method to read a stream without advancing it.
Add a virtual method on SkStream which will do a "peek" some bytes, so
that those bytes are read, but the next call to read will be
unaffected.
Implement peek for SkMemoryStream, where the implementation is simple
and obvious.
Implement peek on SkFrontBufferedStream.
Add tests.
Motivated by decoding streams which cannot be rewound.
TBR=reed@google.com
BUG=skia:3257
Review URL: https://codereview.chromium.org/1044953002
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/SkFrontBufferedStream.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/utils/SkFrontBufferedStream.cpp b/src/utils/SkFrontBufferedStream.cpp index f23b1f99c4..beead37a95 100644 --- a/src/utils/SkFrontBufferedStream.cpp +++ b/src/utils/SkFrontBufferedStream.cpp @@ -16,6 +16,8 @@ public: size_t read(void* buffer, size_t size) override; + bool peek(void* buffer, size_t size) const override; + bool isAtEnd() const override; bool rewind() override; @@ -155,6 +157,20 @@ size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { return bytesReadDirectly; } +bool 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; + } + FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this); + SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(dst, size); + SkASSERT(bytesRead == size); + nonConstThis->fOffset = start; + return true; +} + size_t FrontBufferedStream::read(void* voidDst, size_t size) { // Cast voidDst to a char* for easy addition. char* dst = reinterpret_cast<char*>(voidDst); |