aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/codec/SkStreamBuffer.h
blob: f04ddcbfd8805ad9039d47c8b286cd7c608c074e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkStreamBuffer_DEFINED
#define SkStreamBuffer_DEFINED

#include "SkStream.h"
#include "SkTypes.h"

/**
 *  Helper class for reading from a stream that may not have all its data
 *  available yet.
 *
 *  Used by GIFImageReader, and currently set up for that use case.
 *
 *  Buffers up to 256 * 3 bytes (256 colors, with 3 bytes each) to support GIF.
 *  FIXME (scroggo): Make this more general purpose?
 */
class SkStreamBuffer : SkNoncopyable {
public:
    // Takes ownership of the SkStream.
    SkStreamBuffer(SkStream*);

    /**
     *  Return a pointer the buffered data.
     *
     *  The number of bytes buffered is the sum of values returned by calls to
     *  buffer() since the last call to flush().
     */
    const char* get() const { SkASSERT(fBytesBuffered >= 1); return fBuffer; }

    /**
     *  Bytes in the buffer.
     *
     *  Sum of the values returned by calls to buffer() since the last call to
     *  flush().
     */
    size_t bytesBuffered() const { return fBytesBuffered; }

    /**
     *  Buffer from the stream into our buffer.
     *
     *  Returns the number of bytes successfully buffered.
     */
    size_t buffer(size_t bytes);

    /**
     *  Flush the buffer.
     *
     *  After this call, no bytes are buffered.
     */
    void flush() {
        fBytesBuffered = 0;
    }

private:
    static constexpr size_t kMaxSize = 256 * 3;

    std::unique_ptr<SkStream>   fStream;
    char                        fBuffer[kMaxSize];
    size_t                      fBytesBuffered;
};
#endif // SkStreamBuffer_DEFINED