Chromium Code Reviews| Index: include/utils/SkBufferedStream.h |
| diff --git a/include/utils/SkBufferedStream.h b/include/utils/SkBufferedStream.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6333d2aad6671b99bf504aacb8f1614f1d870a7f |
| --- /dev/null |
| +++ b/include/utils/SkBufferedStream.h |
| @@ -0,0 +1,61 @@ |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkStream.h" |
| +#include "SkTypes.h" |
| + |
| +class SkBufferedStream : public SkStreamRewindable { |
|
bungeman-skia
2013/09/17 22:19:11
So I'm a bit iffy about the name, simply because t
scroggo
2013/09/18 15:15:44
Good point. How about SkFrontBufferedStream?
|
| +public: |
| + /** |
| + * Creates a new stream that wraps and buffers SkStream. |
| + * @param stream SkStream to buffer. If NULL, NULL is returned. After |
| + * this call, unref stream and do not refer to it. |
| + * SkBufferedStream is expected to be its only owner. |
| + * @param bufferSize Exact size of the buffer to be used. |
| + * @return An SkStream that can buffer up to bufferSize. |
| + */ |
| + static SkBufferedStream* Create(SkStream* stream, size_t bufferSize); |
| + |
| + virtual size_t read(void* buffer, size_t size) SK_OVERRIDE; |
| + |
| + virtual bool isAtEnd() const SK_OVERRIDE; |
| + |
| + virtual bool rewind() SK_OVERRIDE; |
| + |
| + virtual bool hasPosition() const SK_OVERRIDE { return true; } |
| + |
| + virtual size_t getPosition() const SK_OVERRIDE { return fOffset; } |
| + |
| + virtual bool hasLength() const SK_OVERRIDE; |
| + |
| + virtual size_t getLength() const SK_OVERRIDE; |
| + |
| + virtual SkStreamRewindable* duplicate() const SK_OVERRIDE { return NULL; } |
| + |
| +private: |
| + SkAutoTUnref<SkStream> fStream; |
| + // Current offset into the stream. Always >= 0. |
| + size_t fOffset; |
| + // Amount that has been buffered by calls to read. Will always be less than |
| + // fBufferSize. |
| + size_t fBufferedSoFar; |
| + // Total size of the buffer. |
| + const size_t fBufferSize; |
| + SkAutoTMalloc<char> fBuffer; |
| + |
| + // Unimplemented. |
| + SkBufferedStream(); |
| + |
| + // Private. Use Create. |
| + SkBufferedStream(SkStream*, size_t bufferSize); |
| + |
| + char* getBufferAtOffset() { |
| + return fBuffer.get() + fOffset; |
| + } |
| + |
| + typedef SkStream INHERITED; |
| +}; |