blob: 6b510b8970146cb4d437db13e28f03df01b896c5 [file] [log] [blame]
Leon Scroggins III932efed2016-12-16 11:39:51 -05001/*
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#include "SkData.h"
9#include "SkStream.h"
10
11#ifndef FakeStreams_DEFINED
12#define FakeStreams_DEFINED
13
14// Stream that is not an asset stream (!hasPosition() or !hasLength())
15class NotAssetMemStream : public SkStream {
16public:
17 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
18
19 bool hasPosition() const override {
20 return false;
21 }
22
23 bool hasLength() const override {
24 return false;
25 }
26
27 size_t peek(void* buf, size_t bytes) const override {
28 return fStream.peek(buf, bytes);
29 }
30 size_t read(void* buf, size_t bytes) override {
31 return fStream.read(buf, bytes);
32 }
33 bool rewind() override {
34 return fStream.rewind();
35 }
36 bool isAtEnd() const override {
37 return fStream.isAtEnd();
38 }
39private:
40 SkMemoryStream fStream;
41};
42
43/*
44 * Represents a stream without all of its data.
45 */
46class HaltingStream : public SkStream {
47public:
48 HaltingStream(sk_sp<SkData> data, size_t initialLimit)
49 : fTotalSize(data->size())
50 , fLimit(initialLimit)
51 , fStream(std::move(data))
52 {}
53
54 void addNewData(size_t extra) {
55 fLimit = SkTMin(fTotalSize, fLimit + extra);
56 }
57
58 size_t read(void* buffer, size_t size) override {
59 if (fStream.getPosition() + size > fLimit) {
60 size = fLimit - fStream.getPosition();
61 }
62
63 return fStream.read(buffer, size);
64 }
65
66 bool isAtEnd() const override {
67 return fStream.isAtEnd();
68 }
69
70 bool hasLength() const override { return true; }
71 size_t getLength() const override { return fLimit; }
72
73 bool hasPosition() const override { return true; }
74 size_t getPosition() const override { return fStream.getPosition(); }
75 bool rewind() override { return fStream.rewind(); }
76 bool move(long offset) override { return fStream.move(offset); }
77 bool seek(size_t position) override { return fStream.seek(position); }
78
79 bool isAllDataReceived() const { return fLimit == fTotalSize; }
80
81private:
82 const size_t fTotalSize;
83 size_t fLimit;
84 SkMemoryStream fStream;
85};
86#endif // FakeStreams_DEFINED