blob: 4bfaeda58793d7b25aa58319a52c8cc92503a14e [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkStream_DEFINED
9#define SkStream_DEFINED
10
reedfde05112016-03-11 13:02:28 -080011#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkRefCnt.h"
13#include "SkScalar.h"
14
bungeman@google.com6cab1a42013-05-29 13:43:31 +000015class SkStream;
16class SkStreamRewindable;
17class SkStreamSeekable;
18class SkStreamAsset;
19class SkStreamMemory;
20
reed@google.com3b345052013-05-07 15:59:16 +000021/**
22 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
23 * memory, or a file, or something else.
24 *
25 * NOTE:
26 *
27 * Classic "streams" APIs are sort of async, in that on a request for N
28 * bytes, they may return fewer than N bytes on a given call, in which case
29 * the caller can "try again" to get more bytes, eventually (modulo an error)
30 * receiving their total N bytes.
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000031 *
reed@google.com3b345052013-05-07 15:59:16 +000032 * Skia streams behave differently. They are effectively synchronous, and will
33 * always return all N bytes of the request if possible. If they return fewer
34 * (the read() call returns the number of bytes read) then that means there is
35 * no more data (at EOF or hit an error). The caller should *not* call again
36 * in hopes of fulfilling more of the request.
37 */
scroggoa1193e42015-01-21 12:09:53 -080038class SK_API SkStream : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039public:
scroggoa1193e42015-01-21 12:09:53 -080040 virtual ~SkStream() {}
41
42 /**
reed@google.come1575aa2013-03-18 21:08:46 +000043 * Attempts to open the specified file, and return a stream to it (using
scroggoa1193e42015-01-21 12:09:53 -080044 * mmap if available). On success, the caller is responsible for deleting.
45 * On failure, returns NULL.
reed@google.come1575aa2013-03-18 21:08:46 +000046 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +000047 static SkStreamAsset* NewFromFile(const char path[]);
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +000048
bungeman@google.com6cab1a42013-05-29 13:43:31 +000049 /** Reads or skips size number of bytes.
50 * If buffer == NULL, skip size bytes, return how many were skipped.
51 * If buffer != NULL, copy size bytes into buffer, return how many were copied.
52 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
53 * @param size the number of bytes to skip or copy
bungeman@google.com88682b72013-07-19 13:55:41 +000054 * @return the number of bytes actually read.
bungeman@google.com6cab1a42013-05-29 13:43:31 +000055 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual size_t read(void* buffer, size_t size) = 0;
57
bungeman@google.com6cab1a42013-05-29 13:43:31 +000058 /** Skip size number of bytes.
59 * @return the actual number bytes that could be skipped.
60 */
61 size_t skip(size_t size) {
bungeman@google.com04306922013-11-13 19:53:46 +000062 return this->read(NULL, size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000063 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064
scroggo028a4132015-04-02 13:19:51 -070065 /**
66 * Attempt to peek at size bytes.
scroggod61c3842015-12-07 11:37:13 -080067 * If this stream supports peeking, copy min(size, peekable bytes) into
68 * buffer, and return the number of bytes copied.
69 * If the stream does not support peeking, or cannot peek any bytes,
70 * return 0 and leave buffer unchanged.
scroggo028a4132015-04-02 13:19:51 -070071 * The stream is guaranteed to be in the same visible state after this
72 * call, regardless of success or failure.
scroggod61c3842015-12-07 11:37:13 -080073 * @param buffer Must not be NULL, and must be at least size bytes. Destination
74 * to copy bytes.
scroggo028a4132015-04-02 13:19:51 -070075 * @param size Number of bytes to copy.
scroggod61c3842015-12-07 11:37:13 -080076 * @return The number of bytes peeked/copied.
scroggo028a4132015-04-02 13:19:51 -070077 */
scroggod61c3842015-12-07 11:37:13 -080078 virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
scroggo028a4132015-04-02 13:19:51 -070079
bungeman@google.com88682b72013-07-19 13:55:41 +000080 /** Returns true when all the bytes in the stream have been read.
81 * This may return true early (when there are no more bytes to be read)
82 * or late (after the first unsuccessful read).
bungeman@google.com6cab1a42013-05-29 13:43:31 +000083 */
bungeman@google.com04306922013-11-13 19:53:46 +000084 virtual bool isAtEnd() const = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86 int8_t readS8();
87 int16_t readS16();
88 int32_t readS32();
89
90 uint8_t readU8() { return (uint8_t)this->readS8(); }
91 uint16_t readU16() { return (uint16_t)this->readS16(); }
92 uint32_t readU32() { return (uint32_t)this->readS32(); }
93
94 bool readBool() { return this->readU8() != 0; }
95 SkScalar readScalar();
96 size_t readPackedUInt();
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000097
bungeman@google.com6cab1a42013-05-29 13:43:31 +000098//SkStreamRewindable
scroggo@google.com4d213ab2013-08-28 13:08:54 +000099 /** Rewinds to the beginning of the stream. Returns true if the stream is known
100 * to be at the beginning after this call returns.
101 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000102 virtual bool rewind() { return false; }
103
104 /** Duplicates this stream. If this cannot be done, returns NULL.
105 * The returned stream will be positioned at the beginning of its data.
106 */
107 virtual SkStreamRewindable* duplicate() const { return NULL; }
108
109//SkStreamSeekable
110 /** Returns true if this stream can report it's current position. */
111 virtual bool hasPosition() const { return false; }
112 /** Returns the current position in the stream. If this cannot be done, returns 0. */
113 virtual size_t getPosition() const { return 0; }
114
115 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
116 * If an attempt is made to seek past the end of the stream, the position will be set
117 * to the end of the stream.
118 */
djsollenc87dd2c2014-11-14 11:11:46 -0800119 virtual bool seek(size_t /*position*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000120
121 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
122 * If an attempt is made to move to a position outside the stream, the position will be set
123 * to the closest point within the stream (beginning or end).
124 */
djsollenc87dd2c2014-11-14 11:11:46 -0800125 virtual bool move(long /*offset*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000126
127 /** Duplicates this stream. If this cannot be done, returns NULL.
128 * The returned stream will be positioned the same as this stream.
129 */
130 virtual SkStreamSeekable* fork() const { return NULL; }
131
132//SkStreamAsset
133 /** Returns true if this stream can report it's total length. */
134 virtual bool hasLength() const { return false; }
135 /** Returns the total length of the stream. If this cannot be done, returns 0. */
bungeman@google.com04306922013-11-13 19:53:46 +0000136 virtual size_t getLength() const { return 0; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000137
138//SkStreamMemory
139 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
140 //TODO: replace with virtual const SkData* getData()
141 virtual const void* getMemoryBase() { return NULL; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142};
143
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000144/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
145class SK_API SkStreamRewindable : public SkStream {
146public:
mtklein36352bf2015-03-25 18:17:31 -0700147 bool rewind() override = 0;
148 SkStreamRewindable* duplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000149};
150
151/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
152class SK_API SkStreamSeekable : public SkStreamRewindable {
153public:
mtklein36352bf2015-03-25 18:17:31 -0700154 SkStreamSeekable* duplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000155
mtklein36352bf2015-03-25 18:17:31 -0700156 bool hasPosition() const override { return true; }
157 size_t getPosition() const override = 0;
158 bool seek(size_t position) override = 0;
159 bool move(long offset) override = 0;
160 SkStreamSeekable* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000161};
162
163/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
164class SK_API SkStreamAsset : public SkStreamSeekable {
165public:
mtklein36352bf2015-03-25 18:17:31 -0700166 SkStreamAsset* duplicate() const override = 0;
167 SkStreamAsset* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000168
mtklein36352bf2015-03-25 18:17:31 -0700169 bool hasLength() const override { return true; }
170 size_t getLength() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000171};
172
173/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
174class SK_API SkStreamMemory : public SkStreamAsset {
175public:
mtklein36352bf2015-03-25 18:17:31 -0700176 SkStreamMemory* duplicate() const override = 0;
177 SkStreamMemory* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000178
mtklein36352bf2015-03-25 18:17:31 -0700179 const void* getMemoryBase() override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000180};
181
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000182class SK_API SkWStream : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183public:
184 virtual ~SkWStream();
185
186 /** Called to write bytes to a SkWStream. Returns true on success
187 @param buffer the address of at least size bytes to be written to the stream
188 @param size The number of bytes in buffer to write to the stream
189 @return true on success
190 */
191 virtual bool write(const void* buffer, size_t size) = 0;
192 virtual void newline();
193 virtual void flush();
194
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000195 virtual size_t bytesWritten() const = 0;
196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 // helpers
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 bool write8(U8CPU);
200 bool write16(U16CPU);
201 bool write32(uint32_t);
202
halcanarycbc060a2016-04-11 19:41:48 -0700203 bool writeText(const char text[]) {
204 SkASSERT(text);
205 return this->write(text, strlen(text));
206 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 bool writeDecAsText(int32_t);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000208 bool writeBigDecAsText(int64_t, int minDigits = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 bool writeHexAsText(uint32_t, int minDigits = 0);
210 bool writeScalarAsText(SkScalar);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 bool writeBool(bool v) { return this->write8(v); }
213 bool writeScalar(SkScalar);
214 bool writePackedUInt(size_t);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000215
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000216 bool writeStream(SkStream* input, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000217
epoger@google.com8e3fb2d2013-03-20 15:56:03 +0000218 /**
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000219 * This returns the number of bytes in the stream required to store
220 * 'value'.
221 */
222 static int SizeOfPackedUInt(size_t value);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223};
224
225////////////////////////////////////////////////////////////////////////////////////////
226
227#include "SkString.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +0000228#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000230/** A stream that wraps a C FILE* file stream. */
231class SK_API SkFILEStream : public SkStreamAsset {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232public:
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000233 /** Initialize the stream by calling sk_fopen on the specified path.
234 * This internal stream will be closed in the destructor.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 */
236 explicit SkFILEStream(const char path[] = NULL);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000237
238 enum Ownership {
239 kCallerPasses_Ownership,
240 kCallerRetains_Ownership
241 };
242 /** Initialize the stream with an existing C file stream.
243 * While this stream exists, it assumes exclusive access to the C file stream.
244 * The C file stream will be closed in the destructor unless the caller specifies
245 * kCallerRetains_Ownership.
246 */
247 explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership);
248
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 virtual ~SkFILEStream();
250
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000251 /** Returns true if the current path could be opened. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 bool isValid() const { return fFILE != NULL; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000253
254 /** Close the current file, and open a new file with the specified path.
255 * If path is NULL, just close the current file.
256 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 void setPath(const char path[]);
258
mtklein36352bf2015-03-25 18:17:31 -0700259 size_t read(void* buffer, size_t size) override;
260 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000261
mtklein36352bf2015-03-25 18:17:31 -0700262 bool rewind() override;
263 SkStreamAsset* duplicate() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000264
mtklein36352bf2015-03-25 18:17:31 -0700265 size_t getPosition() const override;
266 bool seek(size_t position) override;
267 bool move(long offset) override;
268 SkStreamAsset* fork() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000269
mtklein36352bf2015-03-25 18:17:31 -0700270 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000271
mtklein36352bf2015-03-25 18:17:31 -0700272 const void* getMemoryBase() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273
274private:
reedfde05112016-03-11 13:02:28 -0800275 FILE* fFILE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 SkString fName;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000277 Ownership fOwnership;
278 // fData is lazilly initialized when needed.
reedfde05112016-03-11 13:02:28 -0800279 mutable sk_sp<SkData> fData;
reed@google.com3b429982012-06-26 15:30:08 +0000280
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000281 typedef SkStreamAsset INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282};
283
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000284class SK_API SkMemoryStream : public SkStreamMemory {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285public:
286 SkMemoryStream();
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000287
288 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 SkMemoryStream(size_t length);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000290
291 /** If copyData is true, the stream makes a private copy of the data. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 SkMemoryStream(const void* data, size_t length, bool copyData = false);
scroggo@google.come4904202013-01-09 22:02:58 +0000293
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000294 /** Use the specified data as the memory for this stream.
295 * The stream will call ref() on the data (assuming it is not NULL).
reedfde05112016-03-11 13:02:28 -0800296 * DEPRECATED
scroggo@google.come4904202013-01-09 22:02:58 +0000297 */
298 SkMemoryStream(SkData*);
299
reedfde05112016-03-11 13:02:28 -0800300 /** Creates the stream to read from the specified data */
301 SkMemoryStream(sk_sp<SkData>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302
303 /** Resets the stream to the specified data and length,
304 just like the constructor.
305 if copyData is true, the stream makes a private copy of the data
306 */
307 virtual void setMemory(const void* data, size_t length,
308 bool copyData = false);
djsollen@google.com57f49692011-02-23 20:46:31 +0000309 /** Replace any memory buffer with the specified buffer. The caller
310 must have allocated data with sk_malloc or sk_realloc, since it
311 will be freed with sk_free.
312 */
313 void setMemoryOwned(const void* data, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000314
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000315 /** Return the stream's data in a SkData.
316 * The caller must call unref() when it is finished using the data.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000317 */
318 SkData* copyToData() const;
319
320 /**
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000321 * Use the specified data as the memory for this stream.
322 * The stream will call ref() on the data (assuming it is not NULL).
323 * The function returns the data parameter as a convenience.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000324 */
325 SkData* setData(SkData*);
326
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327 void skipToAlign4();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328 const void* getAtPos();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000329
mtklein36352bf2015-03-25 18:17:31 -0700330 size_t read(void* buffer, size_t size) override;
331 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000332
scroggod61c3842015-12-07 11:37:13 -0800333 size_t peek(void* buffer, size_t size) const override;
scroggo028a4132015-04-02 13:19:51 -0700334
mtklein36352bf2015-03-25 18:17:31 -0700335 bool rewind() override;
336 SkMemoryStream* duplicate() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000337
mtklein36352bf2015-03-25 18:17:31 -0700338 size_t getPosition() const override;
339 bool seek(size_t position) override;
340 bool move(long offset) override;
341 SkMemoryStream* fork() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000342
mtklein36352bf2015-03-25 18:17:31 -0700343 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000344
mtklein36352bf2015-03-25 18:17:31 -0700345 const void* getMemoryBase() override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000346
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347private:
reedfde05112016-03-11 13:02:28 -0800348 sk_sp<SkData> fData;
349 size_t fOffset;
reed@google.com3b429982012-06-26 15:30:08 +0000350
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000351 typedef SkStreamMemory INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352};
353
354/////////////////////////////////////////////////////////////////////////////////////////////
355
alokp@chromium.orgf7751ae2012-07-17 19:10:36 +0000356class SK_API SkFILEWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357public:
reed@google.com3b429982012-06-26 15:30:08 +0000358 SkFILEWStream(const char path[]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 virtual ~SkFILEWStream();
360
361 /** Returns true if the current path could be opened.
362 */
363 bool isValid() const { return fFILE != NULL; }
364
mtklein36352bf2015-03-25 18:17:31 -0700365 bool write(const void* buffer, size_t size) override;
366 void flush() override;
caryclark7471fa42015-12-16 13:41:23 -0800367 void fsync();
mtklein36352bf2015-03-25 18:17:31 -0700368 size_t bytesWritten() const override;
reed@google.com3b429982012-06-26 15:30:08 +0000369
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370private:
halcanaryd76be9c2015-11-20 13:47:49 -0800371 FILE* fFILE;
reed@google.com3b429982012-06-26 15:30:08 +0000372
373 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374};
375
caryclark15e2a532016-03-16 06:51:55 -0700376class SK_API SkMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377public:
378 SkMemoryWStream(void* buffer, size_t size);
mtklein36352bf2015-03-25 18:17:31 -0700379 bool write(const void* buffer, size_t size) override;
380 size_t bytesWritten() const override { return fBytesWritten; }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000381
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382private:
383 char* fBuffer;
384 size_t fMaxLength;
385 size_t fBytesWritten;
reed@google.com3b429982012-06-26 15:30:08 +0000386
387 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388};
389
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000390class SK_API SkDynamicMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391public:
392 SkDynamicMemoryWStream();
393 virtual ~SkDynamicMemoryWStream();
reed@google.com8a85d0c2011-06-24 19:12:12 +0000394
mtklein36352bf2015-03-25 18:17:31 -0700395 bool write(const void* buffer, size_t size) override;
396 size_t bytesWritten() const override { return fBytesWritten; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397 // random access write
398 // modifies stream and returns true if offset + size is less than or equal to getOffset()
399 bool write(const void* buffer, size_t offset, size_t size);
400 bool read(void* buffer, size_t offset, size_t size);
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000401 size_t getOffset() const { return fBytesWritten; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402
403 // copy what has been written to the stream into dst
reed@google.com8a85d0c2011-06-24 19:12:12 +0000404 void copyTo(void* dst) const;
halcanary7af21502015-02-23 12:17:59 -0800405 void writeToStream(SkWStream* dst) const;
reed@google.com70442a62011-06-23 21:48:04 +0000406
407 /**
reed@google.com8a85d0c2011-06-24 19:12:12 +0000408 * Return a copy of the data written so far. This call is responsible for
409 * calling unref() when they are finished with the data.
reed@google.com70442a62011-06-23 21:48:04 +0000410 */
reed@google.com8d0b5772011-06-24 13:07:31 +0000411 SkData* copyToData() const;
reed@google.com70442a62011-06-23 21:48:04 +0000412
bungeman@google.com88682b72013-07-19 13:55:41 +0000413 /** Reset, returning a reader stream with the current content. */
bungeman@google.comc29f3d82013-07-19 22:32:11 +0000414 SkStreamAsset* detachAsStream();
bungeman@google.com88682b72013-07-19 13:55:41 +0000415
416 /** Reset the stream to its original, empty, state. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417 void reset();
418 void padToAlign4();
419private:
420 struct Block;
421 Block* fHead;
422 Block* fTail;
423 size_t fBytesWritten;
reedfde05112016-03-11 13:02:28 -0800424 mutable sk_sp<SkData> fCopy; // is invalidated if we write after it is created
reed@google.com8a85d0c2011-06-24 19:12:12 +0000425
426 void invalidateCopy();
reed@google.com3b429982012-06-26 15:30:08 +0000427
bungeman@google.com88682b72013-07-19 13:55:41 +0000428 // For access to the Block type.
429 friend class SkBlockMemoryStream;
430 friend class SkBlockMemoryRefCnt;
431
reed@google.com3b429982012-06-26 15:30:08 +0000432 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433};
434
435
reed@google.coma718c5e2013-03-05 17:49:10 +0000436class SK_API SkDebugWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437public:
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000438 SkDebugWStream() : fBytesWritten(0) {}
reed@google.com3b429982012-06-26 15:30:08 +0000439
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 // overrides
mtklein36352bf2015-03-25 18:17:31 -0700441 bool write(const void* buffer, size_t size) override;
442 void newline() override;
443 size_t bytesWritten() const override { return fBytesWritten; }
reed@google.com3b429982012-06-26 15:30:08 +0000444
445private:
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000446 size_t fBytesWritten;
reed@google.com3b429982012-06-26 15:30:08 +0000447 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448};
449
450// for now
451typedef SkFILEStream SkURLStream;
452
453#endif