 |
Skia
2D Graphics Library
|
Go to the documentation of this file.
8 #ifndef SkStream_DEFINED
9 #define SkStream_DEFINED
14 #include "include/private/SkTo.h"
49 static std::unique_ptr<SkStreamAsset>
MakeFromFile(
const char path[]);
58 virtual size_t read(
void* buffer,
size_t size) = 0;
64 return this->read(
nullptr, size);
80 virtual size_t peek(
void* ,
size_t )
const {
return 0; }
98 if (!this->readU8(&i)) {
return false; }
115 return std::unique_ptr<SkStream>(this->onDuplicate());
120 std::unique_ptr<SkStream>
fork()
const {
121 return std::unique_ptr<SkStream>(this->onFork());
134 virtual bool seek(
size_t ) {
return false; }
140 virtual bool move(
long ) {
return false; }
154 virtual SkStream* onDuplicate()
const {
return nullptr; }
155 virtual SkStream* onFork()
const {
return nullptr; }
168 return std::unique_ptr<SkStreamRewindable>(this->onDuplicate());
178 return std::unique_ptr<SkStreamSeekable>(this->onDuplicate());
183 bool seek(
size_t position)
override = 0;
184 bool move(
long offset)
override = 0;
186 std::unique_ptr<SkStreamSeekable>
fork()
const {
187 return std::unique_ptr<SkStreamSeekable>(this->onFork());
201 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
203 std::unique_ptr<SkStreamAsset>
fork()
const {
204 return std::unique_ptr<SkStreamAsset>(this->onFork());
217 return std::unique_ptr<SkStreamMemory>(this->onDuplicate());
219 std::unique_ptr<SkStreamMemory>
fork()
const {
220 return std::unique_ptr<SkStreamMemory>(this->onFork());
237 virtual bool write(
const void* buffer,
size_t size) = 0;
245 uint8_t v = SkToU8(value);
246 return this->write(&v, 1);
249 uint16_t v = SkToU16(value);
250 return this->write(&v, 2);
253 return this->write(&v, 4);
258 return this->write(text, strlen(text));
261 bool newline() {
return this->write(
"\n", strlen(
"\n")); }
289 bool write(
const void* ,
size_t n)
override { fBytesWritten += n;
return true; }
294 size_t fBytesWritten;
325 static std::unique_ptr<SkFILEStream>
Make(
const char path[]) {
326 std::unique_ptr<SkFILEStream> stream(
new SkFILEStream(path));
327 return stream->isValid() ? std::move(stream) :
nullptr;
331 bool isValid()
const {
return fFILE !=
nullptr; }
336 size_t read(
void* buffer,
size_t size)
override;
341 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
345 bool seek(
size_t position)
override;
346 bool move(
long offset)
override;
348 std::unique_ptr<SkStreamAsset>
fork()
const {
349 return std::unique_ptr<SkStreamAsset>(this->onFork());
355 explicit SkFILEStream(FILE*,
size_t size,
size_t start);
356 explicit SkFILEStream(std::shared_ptr<FILE>,
size_t end,
size_t start);
357 explicit SkFILEStream(std::shared_ptr<FILE>,
size_t end,
size_t start,
size_t current);
362 std::shared_ptr<FILE> fFILE;
386 static std::unique_ptr<SkMemoryStream>
MakeCopy(
const void* data,
size_t length);
389 static std::unique_ptr<SkMemoryStream>
MakeDirect(
const void* data,
size_t length);
399 bool copyData =
false);
412 size_t read(
void* buffer,
size_t size)
override;
415 size_t peek(
void* buffer,
size_t size)
const override;
420 return std::unique_ptr<SkMemoryStream>(this->onDuplicate());
424 bool seek(
size_t position)
override;
425 bool move(
long offset)
override;
427 std::unique_ptr<SkMemoryStream>
fork()
const {
428 return std::unique_ptr<SkMemoryStream>(this->onFork());
454 bool isValid()
const {
return fFILE !=
nullptr; }
456 bool write(
const void* buffer,
size_t size)
override;
474 bool write(
const void* buffer,
size_t size)
override;
477 bool read(
void* buffer,
size_t offset,
size_t size);
507 Block* fHead =
nullptr;
508 Block* fTail =
nullptr;
509 size_t fBytesWrittenBeforeTail = 0;
512 void validate()
const;
514 void validate()
const {}
518 friend class SkBlockMemoryStream;
519 friend class SkBlockMemoryRefCnt;
virtual bool seek(size_t)
Seeks to an absolute position in the stream.
Definition: SkStream.h:134
bool SK_WARN_UNUSED_RESULT readS8(int8_t *)
std::unique_ptr< SkStreamSeekable > fork() const
Definition: SkStream.h:186
virtual size_t read(void *buffer, size_t size)=0
Reads or skips size number of bytes.
SkMemoryStream(size_t length)
We allocate (and free) the memory.
bool isValid() const
Returns true if the current path could be opened.
Definition: SkStream.h:331
size_t getPosition() const override=0
Returns the current position in the stream.
Definition: SkStream.h:285
bool writeDecAsText(int32_t)
bool writeText(const char text[])
Definition: SkStream.h:256
SkDynamicMemoryWStream(SkDynamicMemoryWStream &&)
Definition: SkStream.h:372
bool SK_WARN_UNUSED_RESULT readBool(bool *b)
Definition: SkStream.h:96
bool SK_WARN_UNUSED_RESULT readU8(uint8_t *i)
Definition: SkStream.h:92
std::unique_ptr< SkMemoryStream > duplicate() const
Definition: SkStream.h:419
void setData(sk_sp< SkData > data)
sk_sp< SkData > detachAsData()
Return the contents as SkData, and then reset the stream.
SkFILEStream(FILE *file, size_t size)
Initialize the stream with an existing C FILE stream.
SkDynamicMemoryWStream & operator=(SkDynamicMemoryWStream &&)
bool move(long offset) override=0
Seeks to an relative offset in the stream.
bool move(long offset) override
Seeks to an relative offset in the stream.
bool SK_WARN_UNUSED_RESULT readU16(uint16_t *i)
Definition: SkStream.h:93
std::unique_ptr< SkStreamAsset > detachAsStream()
Reset, returning a reader stream with the current content.
void close()
Close this SkFILEStream.
bool isAtEnd() const override
Returns true when all the bytes in the stream have been read.
void reset()
Reset the stream to its original, empty, state.
SkWStream()
Definition: SkStream.h:230
bool writeScalarAsText(SkScalar)
void copyToAndReset(void *dst)
Equivalent to copyTo() followed by reset(), but may save memory use.
bool hasLength() const override
Returns true if this stream can report it's total length.
Definition: SkStream.h:197
SkNullWStream()
Definition: SkStream.h:287
virtual size_t peek(void *, size_t) const
Attempt to peek at size bytes.
Definition: SkStream.h:80
size_t getLength() const override
Returns the total length of the stream.
bool write(const void *buffer, size_t size) override
Called to write bytes to a SkWStream.
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:41
SkDynamicMemoryWStream()=default
bool rewind() override
Rewinds to the beginning of the stream.
bool rewind() override=0
Rewinds to the beginning of the stream.
bool hasPosition() const override
Returns true if this stream can report it's current position.
Definition: SkStream.h:181
bool write32(uint32_t v)
Definition: SkStream.h:252
static std::unique_ptr< SkStreamAsset > MakeFromFile(const char path[])
Attempts to open the specified file as a stream, returns nullptr on failure.
size_t bytesWritten() const override
Definition: SkStream.h:291
bool writePackedUInt(size_t)
size_t bytesWritten() const override
~SkDynamicMemoryWStream() override
bool write(const void *, size_t n) override
Called to write bytes to a SkWStream.
Definition: SkStream.h:289
virtual size_t bytesWritten() const =0
bool write(const void *buffer, size_t size) override
Called to write bytes to a SkWStream.
~SkFILEWStream() override
#define SK_API
Definition: SkTypes.h:181
A stream that wraps a C FILE* file stream.
Definition: SkStream.h:302
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkFILEStream(FILE *file)
Initialize the stream with an existing C FILE stream.
std::unique_ptr< SkMemoryStream > fork() const
Definition: SkStream.h:427
void flush() override
Definition: SkStream.h:290
size_t getLength() const override=0
Returns the total length of the stream.
size_t read(void *buffer, size_t size) override
Reads or skips size number of bytes.
bool isAtEnd() const override
Returns true when all the bytes in the stream have been read.
bool writeToAndReset(SkWStream *dst)
Equivalent to writeToStream() followed by reset(), but may save memory use.
SkStream()
Definition: SkStream.h:44
virtual bool hasLength() const
Returns true if this stream can report it's total length.
Definition: SkStream.h:144
Definition: SkStream.h:467
bool SK_WARN_UNUSED_RESULT readPackedUInt(size_t *)
std::unique_ptr< SkStream > duplicate() const
Duplicates this stream.
Definition: SkStream.h:114
virtual bool move(long)
Seeks to an relative offset in the stream.
Definition: SkStream.h:140
static std::unique_ptr< SkMemoryStream > MakeCopy(const void *data, size_t length)
Returns a stream with a copy of the input data.
Definition: SkStream.h:447
bool seek(size_t position) override
Seeks to an absolute position in the stream.
bool SK_WARN_UNUSED_RESULT readS32(int32_t *)
virtual size_t getLength() const
Returns the total length of the stream.
Definition: SkStream.h:146
virtual size_t getPosition() const
Returns the current position in the stream.
Definition: SkStream.h:128
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
Returns a stream with a shared reference to the input data.
sk_sp< SkData > asData() const
Definition: SkStream.h:406
unsigned U16CPU
Fast type for unsigned 16 bits.
Definition: SkTypes.h:493
virtual const void * getMemoryBase()
Returns the starting address for the data.
Definition: SkStream.h:151
const void * getMemoryBase() override=0
Returns the starting address for the data.
size_t getPosition() const override
Returns the current position in the stream.
bool writeToAndReset(SkDynamicMemoryWStream *dst)
Equivalent to writeToStream() followed by reset(), but may save memory use.
virtual ~SkStream()
Definition: SkStream.h:43
float SkScalar
Definition: SkScalar.h:16
virtual void setMemory(const void *data, size_t length, bool copyData=false)
Resets the stream to the specified data and length, just like the constructor.
size_t getLength() const override
Returns the total length of the stream.
bool rewind() override
Rewinds to the beginning of the stream.
std::unique_ptr< SkStreamAsset > fork() const
Definition: SkStream.h:348
bool writeBigDecAsText(int64_t, int minDigits=0)
bool writeBool(bool v)
Definition: SkStream.h:268
SkStreamAsset is a SkStreamSeekable for which getLength is required.
Definition: SkStream.h:195
bool SK_WARN_UNUSED_RESULT readS16(int16_t *)
std::unique_ptr< SkStreamAsset > duplicate() const
Definition: SkStream.h:340
void prependToAndReset(SkDynamicMemoryWStream *dst)
Prepend this stream to dst, resetting this.
bool SK_WARN_UNUSED_RESULT readU32(uint32_t *i)
Definition: SkStream.h:94
SkMemoryStream(const void *data, size_t length, bool copyData=false)
If copyData is true, the stream makes a private copy of the data.
bool writeHexAsText(uint32_t, int minDigits=0)
std::unique_ptr< SkStreamAsset > fork() const
Definition: SkStream.h:203
bool seek(size_t position) override
Seeks to an absolute position in the stream.
SkFILEStream(const char path[]=nullptr)
Initialize the stream by calling sk_fopen on the specified path.
virtual bool rewind()
Rewinds to the beginning of the stream.
Definition: SkStream.h:109
std::unique_ptr< SkStreamRewindable > duplicate() const
Definition: SkStream.h:167
bool read(void *buffer, size_t offset, size_t size)
SkStreamMemory is a SkStreamAsset for which getMemoryBase is required.
Definition: SkStream.h:212
SkFILEWStream(const char path[])
void setMemoryOwned(const void *data, size_t length)
Replace any memory buffer with the specified buffer.
static std::unique_ptr< SkMemoryStream > MakeDirect(const void *data, size_t length)
Returns a stream with a bare pointer reference to the input data.
virtual bool hasPosition() const
Returns true if this stream can report it's current position.
Definition: SkStream.h:126
#define SK_WARN_UNUSED_RESULT
Definition: SkTypes.h:60
size_t getPosition() const override
Returns the current position in the stream.
std::unique_ptr< SkStream > fork() const
Duplicates this stream.
Definition: SkStream.h:120
std::unique_ptr< SkStreamMemory > duplicate() const
Definition: SkStream.h:216
bool isValid() const
Returns true if the current path could be opened.
Definition: SkStream.h:454
SkStreamRewindable is a SkStream for which rewind and duplicate are required.
Definition: SkStream.h:164
bool writeStream(SkStream *input, size_t length)
bool seek(size_t position) override=0
Seeks to an absolute position in the stream.
Definition: SkStream.h:227
size_t peek(void *buffer, size_t size) const override
Attempt to peek at size bytes.
bool move(long offset) override
Seeks to an relative offset in the stream.
std::unique_ptr< SkStreamMemory > fork() const
Definition: SkStream.h:219
static std::unique_ptr< SkFILEStream > Make(const char path[])
Definition: SkStream.h:325
virtual bool isAtEnd() const =0
Returns true when all the bytes in the stream have been read.
std::unique_ptr< SkStreamAsset > duplicate() const
Definition: SkStream.h:200
SkMemoryStream(sk_sp< SkData > data)
Creates the stream to read from the specified data.
bool write16(U16CPU value)
Definition: SkStream.h:248
SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required.
Definition: SkStream.h:175
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:488
bool writeScalar(SkScalar)
size_t skip(size_t size)
Skip size number of bytes.
Definition: SkStream.h:63
bool newline()
Definition: SkStream.h:261
size_t bytesWritten() const override
bool SK_WARN_UNUSED_RESULT readScalar(SkScalar *)
bool write8(U8CPU value)
Definition: SkStream.h:244
virtual bool write(const void *buffer, size_t size)=0
Called to write bytes to a SkWStream.
static int SizeOfPackedUInt(size_t value)
This returns the number of bytes in the stream required to store 'value'.
bool writeToStream(SkWStream *dst) const
void copyTo(void *dst) const
More efficient version of read(dst, 0, bytesWritten()).
size_t read(void *buffer, size_t size) override
Reads or skips size number of bytes.
const void * getMemoryBase() override
Returns the starting address for the data.
std::unique_ptr< SkStreamSeekable > duplicate() const
Definition: SkStream.h:177