8 #ifndef SkStream_DEFINED
9 #define SkStream_DEFINED
15 #include "include/private/base/SkCPUTypes.h"
16 #include "include/private/base/SkTo.h"
37 static std::unique_ptr<SkStreamAsset>
MakeFromFile(
const char path[]);
46 virtual size_t read(
void* buffer,
size_t size) = 0;
52 return this->read(
nullptr, size);
68 virtual size_t peek(
void* ,
size_t )
const {
return 0; }
83 [[nodiscard]]
bool readU8(uint8_t* i) {
return this->readS8((int8_t*)i); }
84 [[nodiscard]]
bool readU16(uint16_t* i) {
return this->readS16((int16_t*)i); }
85 [[nodiscard]]
bool readU32(uint32_t* i) {
return this->readS32((int32_t*)i); }
89 if (!this->readU8(&i)) {
return false; }
106 return std::unique_ptr<SkStream>(this->onDuplicate());
111 std::unique_ptr<SkStream>
fork()
const {
112 return std::unique_ptr<SkStream>(this->onFork());
125 virtual bool seek(
size_t ) {
return false; }
131 virtual bool move(
long ) {
return false; }
145 virtual SkStream* onDuplicate()
const {
return nullptr; }
146 virtual SkStream* onFork()
const {
return nullptr; }
159 return std::unique_ptr<SkStreamRewindable>(this->onDuplicate());
169 return std::unique_ptr<SkStreamSeekable>(this->onDuplicate());
174 bool seek(
size_t position)
override = 0;
175 bool move(
long offset)
override = 0;
177 std::unique_ptr<SkStreamSeekable>
fork()
const {
178 return std::unique_ptr<SkStreamSeekable>(this->onFork());
192 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
194 std::unique_ptr<SkStreamAsset>
fork()
const {
195 return std::unique_ptr<SkStreamAsset>(this->onFork());
208 return std::unique_ptr<SkStreamMemory>(this->onDuplicate());
210 std::unique_ptr<SkStreamMemory>
fork()
const {
211 return std::unique_ptr<SkStreamMemory>(this->onFork());
228 virtual bool write(
const void* buffer,
size_t size) = 0;
236 uint8_t v = SkToU8(value);
237 return this->write(&v, 1);
240 uint16_t v = SkToU16(value);
241 return this->write(&v, 2);
244 return this->write(&v, 4);
249 return this->write(text, std::strlen(text));
252 bool newline() {
return this->write(
"\n", std::strlen(
"\n")); }
280 bool write(
const void* ,
size_t n)
override { fBytesWritten += n;
return true; }
285 size_t fBytesWritten;
314 static std::unique_ptr<SkFILEStream>
Make(
const char path[]) {
315 std::unique_ptr<SkFILEStream> stream(
new SkFILEStream(path));
316 return stream->isValid() ? std::move(stream) :
nullptr;
320 bool isValid()
const {
return fFILE !=
nullptr; }
325 size_t read(
void* buffer,
size_t size)
override;
330 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
334 bool seek(
size_t position)
override;
335 bool move(
long offset)
override;
337 std::unique_ptr<SkStreamAsset>
fork()
const {
338 return std::unique_ptr<SkStreamAsset>(this->onFork());
344 explicit SkFILEStream(FILE*,
size_t size,
size_t start);
345 explicit SkFILEStream(std::shared_ptr<FILE>,
size_t end,
size_t start);
346 explicit SkFILEStream(std::shared_ptr<FILE>,
size_t end,
size_t start,
size_t current);
351 std::shared_ptr<FILE> fFILE;
375 static std::unique_ptr<SkMemoryStream>
MakeCopy(
const void* data,
size_t length);
378 static std::unique_ptr<SkMemoryStream>
MakeDirect(
const void* data,
size_t length);
388 bool copyData =
false);
401 size_t read(
void* buffer,
size_t size)
override;
404 size_t peek(
void* buffer,
size_t size)
const override;
409 return std::unique_ptr<SkMemoryStream>(this->onDuplicate());
413 bool seek(
size_t position)
override;
414 bool move(
long offset)
override;
416 std::unique_ptr<SkMemoryStream>
fork()
const {
417 return std::unique_ptr<SkMemoryStream>(this->onFork());
443 bool isValid()
const {
return fFILE !=
nullptr; }
445 bool write(
const void* buffer,
size_t size)
override;
463 bool write(
const void* buffer,
size_t size)
override;
466 bool read(
void* buffer,
size_t offset,
size_t size);
496 Block* fHead =
nullptr;
497 Block* fTail =
nullptr;
498 size_t fBytesWrittenBeforeTail = 0;
501 void validate()
const;
503 void validate()
const {}
507 friend class SkBlockMemoryStream;
508 friend class SkBlockMemoryRefCnt;
float SkScalar
Definition: SkScalar.h:14
Definition: SkStream.h:456
void reset()
Reset the stream to its original, empty, state.
size_t bytesWritten() const override
SkDynamicMemoryWStream(SkDynamicMemoryWStream &&)
SkDynamicMemoryWStream & operator=(SkDynamicMemoryWStream &&)
sk_sp< SkData > detachAsData()
Return the contents as SkData, and then reset the stream.
~SkDynamicMemoryWStream() override
bool writeToStream(SkWStream *dst) const
std::unique_ptr< SkStreamAsset > detachAsStream()
Reset, returning a reader stream with the current content.
bool writeToAndReset(SkWStream *dst)
Equivalent to writeToStream() followed by reset(), but may save memory use.
void copyToAndReset(void *dst)
Equivalent to copyTo() followed by reset(), but may save memory use.
void copyTo(void *dst) const
More efficient version of read(dst, 0, bytesWritten()).
SkDynamicMemoryWStream()=default
bool writeToAndReset(SkDynamicMemoryWStream *dst)
Equivalent to writeToStream() followed by reset(), but may save memory use.
bool write(const void *buffer, size_t size) override
Called to write bytes to a SkWStream.
bool read(void *buffer, size_t offset, size_t size)
void prependToAndReset(SkDynamicMemoryWStream *dst)
Prepend this stream to dst, resetting this.
A stream that wraps a C FILE* file stream.
Definition: SkStream.h:291
static std::unique_ptr< SkFILEStream > Make(const char path[])
Definition: SkStream.h:314
bool move(long offset) override
Seeks to an relative offset in the stream.
bool seek(size_t position) override
Seeks to an absolute position in the stream.
size_t read(void *buffer, size_t size) override
Reads or skips size number of bytes.
std::unique_ptr< SkStreamAsset > fork() const
Definition: SkStream.h:337
bool isAtEnd() const override
Returns true when all the bytes in the stream have been read.
bool isValid() const
Returns true if the current path could be opened.
Definition: SkStream.h:320
size_t getLength() const override
Returns the total length of the stream.
bool rewind() override
Rewinds to the beginning of the stream.
SkFILEStream(FILE *file)
Initialize the stream with an existing C FILE stream.
void close()
Close this SkFILEStream.
size_t getPosition() const override
Returns the current position in the stream.
SkFILEStream(FILE *file, size_t size)
Initialize the stream with an existing C FILE stream.
SkFILEStream(const char path[]=nullptr)
Initialize the stream by calling sk_fopen on the specified path.
std::unique_ptr< SkStreamAsset > duplicate() const
Definition: SkStream.h:329
Definition: SkStream.h:436
size_t bytesWritten() const override
bool write(const void *buffer, size_t size) override
Called to write bytes to a SkWStream.
~SkFILEWStream() override
SkFILEWStream(const char path[])
bool isValid() const
Returns true if the current path could be opened.
Definition: SkStream.h:443
Definition: SkStream.h:361
bool isAtEnd() const override
Returns true when all the bytes in the stream have been read.
bool move(long offset) override
Seeks to an relative offset in the stream.
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
Returns a stream with a shared reference to the input data.
bool seek(size_t position) override
Seeks to an absolute position in the stream.
static std::unique_ptr< SkMemoryStream > MakeCopy(const void *data, size_t length)
Returns a stream with a copy of the input data.
size_t read(void *buffer, size_t size) override
Reads or skips size number of bytes.
void setMemoryOwned(const void *data, size_t length)
Replace any memory buffer with the specified buffer.
std::unique_ptr< SkMemoryStream > duplicate() const
Definition: SkStream.h:408
sk_sp< SkData > asData() const
Definition: SkStream.h:395
SkMemoryStream(sk_sp< SkData > data)
Creates the stream to read from the specified data.
size_t getLength() const override
Returns the total length of the stream.
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 peek(void *buffer, size_t size) const override
Attempt to peek at size bytes.
bool rewind() override
Rewinds to the beginning of the stream.
std::unique_ptr< SkMemoryStream > fork() const
Definition: SkStream.h:416
SkMemoryStream(const void *data, size_t length, bool copyData=false)
If copyData is true, the stream makes a private copy of the data.
void setData(sk_sp< SkData > data)
const void * getMemoryBase() override
Returns the starting address for the data.
SkMemoryStream(size_t length)
We allocate (and free) the memory.
size_t getPosition() const override
Returns the current position in the stream.
static std::unique_ptr< SkMemoryStream > MakeDirect(const void *data, size_t length)
Returns a stream with a bare pointer reference to the input data.
Definition: SkStream.h:276
size_t bytesWritten() const override
Definition: SkStream.h:282
SkNullWStream()
Definition: SkStream.h:278
void flush() override
Definition: SkStream.h:281
bool write(const void *, size_t n) override
Called to write bytes to a SkWStream.
Definition: SkStream.h:280
SkStreamAsset is a SkStreamSeekable for which getLength is required.
Definition: SkStream.h:186
bool hasLength() const override
Returns true if this stream can report its total length.
Definition: SkStream.h:188
size_t getLength() const override=0
Returns the total length of the stream.
std::unique_ptr< SkStreamAsset > duplicate() const
Definition: SkStream.h:191
std::unique_ptr< SkStreamAsset > fork() const
Definition: SkStream.h:194
SkStreamMemory is a SkStreamAsset for which getMemoryBase is required.
Definition: SkStream.h:203
const void * getMemoryBase() override=0
Returns the starting address for the data.
std::unique_ptr< SkStreamMemory > duplicate() const
Definition: SkStream.h:207
std::unique_ptr< SkStreamMemory > fork() const
Definition: SkStream.h:210
SkStreamRewindable is a SkStream for which rewind and duplicate are required.
Definition: SkStream.h:155
bool rewind() override=0
Rewinds to the beginning of the stream.
std::unique_ptr< SkStreamRewindable > duplicate() const
Definition: SkStream.h:158
SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required.
Definition: SkStream.h:166
size_t getPosition() const override=0
Returns the current position in the stream.
std::unique_ptr< SkStreamSeekable > fork() const
Definition: SkStream.h:177
std::unique_ptr< SkStreamSeekable > duplicate() const
Definition: SkStream.h:168
bool move(long offset) override=0
Seeks to an relative offset in the stream.
bool hasPosition() const override
Returns true if this stream can report its current position.
Definition: SkStream.h:172
bool seek(size_t position) override=0
Seeks to an absolute position in the stream.
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:29
bool readU8(uint8_t *i)
Definition: SkStream.h:83
virtual bool move(long)
Seeks to an relative offset in the stream.
Definition: SkStream.h:131
bool readScalar(SkScalar *)
SkStream()
Definition: SkStream.h:32
virtual bool rewind()
Rewinds to the beginning of the stream.
Definition: SkStream.h:100
virtual const void * getMemoryBase()
Returns the starting address for the data.
Definition: SkStream.h:142
static std::unique_ptr< SkStreamAsset > MakeFromFile(const char path[])
Attempts to open the specified file as a stream, returns nullptr on failure.
bool readBool(bool *b)
Definition: SkStream.h:87
virtual size_t peek(void *, size_t) const
Attempt to peek at size bytes.
Definition: SkStream.h:68
bool readPackedUInt(size_t *)
virtual bool hasPosition() const
Returns true if this stream can report its current position.
Definition: SkStream.h:117
virtual size_t getPosition() const
Returns the current position in the stream.
Definition: SkStream.h:119
virtual ~SkStream()
Definition: SkStream.h:31
bool readU16(uint16_t *i)
Definition: SkStream.h:84
std::unique_ptr< SkStream > duplicate() const
Duplicates this stream.
Definition: SkStream.h:105
size_t skip(size_t size)
Skip size number of bytes.
Definition: SkStream.h:51
bool readU32(uint32_t *i)
Definition: SkStream.h:85
virtual bool isAtEnd() const =0
Returns true when all the bytes in the stream have been read.
virtual bool seek(size_t)
Seeks to an absolute position in the stream.
Definition: SkStream.h:125
std::unique_ptr< SkStream > fork() const
Duplicates this stream.
Definition: SkStream.h:111
virtual size_t getLength() const
Returns the total length of the stream.
Definition: SkStream.h:137
virtual bool hasLength() const
Returns true if this stream can report its total length.
Definition: SkStream.h:135
virtual size_t read(void *buffer, size_t size)=0
Reads or skips size number of bytes.
Definition: SkStream.h:218
bool writePackedUInt(size_t)
virtual bool write(const void *buffer, size_t size)=0
Called to write bytes to a SkWStream.
bool writeScalarAsText(SkScalar)
bool write32(uint32_t v)
Definition: SkStream.h:243
bool write8(U8CPU value)
Definition: SkStream.h:235
bool write16(U16CPU value)
Definition: SkStream.h:239
bool writeHexAsText(uint32_t, int minDigits=0)
SkWStream()
Definition: SkStream.h:221
static int SizeOfPackedUInt(size_t value)
This returns the number of bytes in the stream required to store 'value'.
bool writeStream(SkStream *input, size_t length)
bool writeDecAsText(int32_t)
bool writeBigDecAsText(int64_t, int minDigits=0)
virtual size_t bytesWritten() const =0
bool writeBool(bool v)
Definition: SkStream.h:259
bool writeScalar(SkScalar)
bool writeText(const char text[])
Definition: SkStream.h:247
bool newline()
Definition: SkStream.h:252