Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkData.h" |
| 9 | #include "include/core/SkStream.h" |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 10 | |
| 11 | #ifndef FakeStreams_DEFINED |
| 12 | #define FakeStreams_DEFINED |
| 13 | |
| 14 | // Stream that is not an asset stream (!hasPosition() or !hasLength()) |
| 15 | class NotAssetMemStream : public SkStream { |
| 16 | public: |
| 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 | } |
| 39 | private: |
| 40 | SkMemoryStream fStream; |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * Represents a stream without all of its data. |
| 45 | */ |
| 46 | class HaltingStream : public SkStream { |
| 47 | public: |
| 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) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 55 | fLimit = std::min(fTotalSize, fLimit + extra); |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 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 | |
| 81 | private: |
| 82 | const size_t fTotalSize; |
| 83 | size_t fLimit; |
| 84 | SkMemoryStream fStream; |
| 85 | }; |
| 86 | #endif // FakeStreams_DEFINED |