OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 #ifndef SkStreamBuffer_DEFINED |
| 9 #define SkStreamBuffer_DEFINED |
| 10 |
| 11 #include "SkStream.h" |
| 12 #include "SkTypes.h" |
| 13 |
| 14 /** |
| 15 * Helper class for reading from a stream that may not have all its data |
| 16 * available yet. |
| 17 * |
| 18 * Used by GIFImageReader, and currently set up for that use case. |
| 19 * |
| 20 * Buffers up to 256 * 3 bytes (256 colors, with 3 bytes each) to support GIF. |
| 21 * FIXME (scroggo): Make this more general purpose? |
| 22 */ |
| 23 class SkStreamBuffer : SkNoncopyable { |
| 24 public: |
| 25 // Takes ownership of the SkStream. |
| 26 SkStreamBuffer(SkStream*); |
| 27 |
| 28 /** |
| 29 * Return a pointer the buffered data. |
| 30 * |
| 31 * The number of bytes buffered is the sum of values returned by calls to |
| 32 * buffer() since the last call to flush(). |
| 33 */ |
| 34 const char* get() const { SkASSERT(fBytesBuffered >= 1); return fBuffer; } |
| 35 |
| 36 /** |
| 37 * Bytes in the buffer. |
| 38 * |
| 39 * Sum of the values returned by calls to buffer() since the last call to |
| 40 * flush(). |
| 41 */ |
| 42 size_t bytesBuffered() const { return fBytesBuffered; } |
| 43 |
| 44 /** |
| 45 * Buffer from the stream into our buffer. |
| 46 * |
| 47 * Returns the number of bytes successfully buffered. |
| 48 */ |
| 49 size_t buffer(size_t bytes); |
| 50 |
| 51 /** |
| 52 * Flush the buffer. |
| 53 * |
| 54 * After this call, no bytes are buffered. |
| 55 */ |
| 56 void flush() { |
| 57 fBytesBuffered = 0; |
| 58 } |
| 59 |
| 60 private: |
| 61 static constexpr size_t kMaxSize = 256 * 3; |
| 62 |
| 63 std::unique_ptr<SkStream> fStream; |
| 64 char fBuffer[kMaxSize]; |
| 65 size_t fBytesBuffered; |
| 66 }; |
| 67 #endif // SkStreamBuffer_DEFINED |
| 68 |
OLD | NEW |