scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkStream.h" |
| 9 | #include "SkTemplates.h" |
| 10 | |
| 11 | /** |
| 12 | * Specialized stream that only buffers the first X bytes of a stream, |
| 13 | * where X is passed in by the user. Note that unlike some buffered |
| 14 | * stream APIs, once more than those bytes are read, no more buffering |
| 15 | * is done. This stream is designed for a use case where the caller |
| 16 | * knows that rewind will only be called from within X bytes (inclusive), |
| 17 | * and the wrapped stream is not necessarily able to rewind at all. |
| 18 | */ |
| 19 | class SkFrontBufferedStream : public SkStreamRewindable { |
| 20 | public: |
| 21 | /** |
| 22 | * Creates a new stream that wraps and buffers SkStream. |
| 23 | * @param stream SkStream to buffer. If NULL, NULL is returned. After |
| 24 | * this call, unref stream and do not refer to it. |
| 25 | * SkFrontBufferedStream is expected to be its only owner. |
| 26 | * @param bufferSize Exact size of the buffer to be used. |
| 27 | * @return An SkStream that can buffer up to bufferSize. |
| 28 | */ |
| 29 | static SkStreamRewindable* Create(SkStream* stream, size_t bufferSize); |
| 30 | |
| 31 | virtual size_t read(void* buffer, size_t size) SK_OVERRIDE; |
| 32 | |
| 33 | virtual bool isAtEnd() const SK_OVERRIDE; |
| 34 | |
| 35 | virtual bool rewind() SK_OVERRIDE; |
| 36 | |
| 37 | virtual bool hasPosition() const SK_OVERRIDE { return true; } |
| 38 | |
| 39 | virtual size_t getPosition() const SK_OVERRIDE { return fOffset; } |
| 40 | |
| 41 | virtual bool hasLength() const SK_OVERRIDE; |
| 42 | |
| 43 | virtual size_t getLength() const SK_OVERRIDE; |
| 44 | |
| 45 | virtual SkStreamRewindable* duplicate() const SK_OVERRIDE { return NULL; } |
| 46 | |
| 47 | private: |
| 48 | SkAutoTUnref<SkStream> fStream; |
| 49 | // Current offset into the stream. Always >= 0. |
| 50 | size_t fOffset; |
| 51 | // Amount that has been buffered by calls to read. Will always be less than |
| 52 | // fBufferSize. |
| 53 | size_t fBufferedSoFar; |
| 54 | // Total size of the buffer. |
| 55 | const size_t fBufferSize; |
| 56 | SkAutoTMalloc<char> fBuffer; |
| 57 | |
| 58 | // Private. Use Create. |
| 59 | SkFrontBufferedStream(SkStream*, size_t bufferSize); |
| 60 | |
| 61 | // Read up to size bytes from already buffered data, and copy to |
| 62 | // dst, if non-NULL. Updates fOffset. Assumes that fOffset is less |
| 63 | // than fBufferedSoFar. |
| 64 | size_t readFromBuffer(char* dst, size_t size); |
| 65 | |
| 66 | // Buffer up to size bytes from the stream, and copy to dst if non- |
| 67 | // NULL. Updates fOffset and fBufferedSoFar. Assumes that fOffset is |
| 68 | // less than fBufferedSoFar, and size is greater than 0. |
| 69 | size_t bufferAndWriteTo(char* dst, size_t size); |
| 70 | |
| 71 | // Read up to size bytes directly from the stream and into dst if non- |
| 72 | // NULL. Updates fOffset. Assumes fOffset is at or beyond the buffered |
| 73 | // data, and size is greater than 0. |
| 74 | size_t readDirectlyFromStream(char* dst, size_t size); |
| 75 | |
| 76 | typedef SkStream INHERITED; |
| 77 | }; |