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 #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()) 15 class NotAssetMemStream : public SkStream { 16 public: NotAssetMemStream(sk_sp<SkData> data)17 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {} 18 hasPosition()19 bool hasPosition() const override { 20 return false; 21 } 22 hasLength()23 bool hasLength() const override { 24 return false; 25 } 26 peek(void * buf,size_t bytes)27 size_t peek(void* buf, size_t bytes) const override { 28 return fStream.peek(buf, bytes); 29 } read(void * buf,size_t bytes)30 size_t read(void* buf, size_t bytes) override { 31 return fStream.read(buf, bytes); 32 } rewind()33 bool rewind() override { 34 return fStream.rewind(); 35 } isAtEnd()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: HaltingStream(sk_sp<SkData> data,size_t initialLimit)48 HaltingStream(sk_sp<SkData> data, size_t initialLimit) 49 : fTotalSize(data->size()) 50 , fLimit(initialLimit) 51 , fStream(std::move(data)) 52 {} 53 addNewData(size_t extra)54 void addNewData(size_t extra) { 55 fLimit = SkTMin(fTotalSize, fLimit + extra); 56 } 57 read(void * buffer,size_t size)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 isAtEnd()66 bool isAtEnd() const override { 67 return fStream.isAtEnd(); 68 } 69 hasLength()70 bool hasLength() const override { return true; } getLength()71 size_t getLength() const override { return fLimit; } 72 hasPosition()73 bool hasPosition() const override { return true; } getPosition()74 size_t getPosition() const override { return fStream.getPosition(); } rewind()75 bool rewind() override { return fStream.rewind(); } move(long offset)76 bool move(long offset) override { return fStream.move(offset); } seek(size_t position)77 bool seek(size_t position) override { return fStream.seek(position); } 78 isAllDataReceived()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 87