2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkStream_DEFINED
|
|
|
|
#define SkStream_DEFINED
|
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
|
|
|
|
2011-06-24 13:07:31 +00:00
|
|
|
class SkData;
|
2011-06-23 21:48:04 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
class SkStream;
|
|
|
|
class SkStreamRewindable;
|
|
|
|
class SkStreamSeekable;
|
|
|
|
class SkStreamAsset;
|
|
|
|
class SkStreamMemory;
|
|
|
|
|
2013-05-07 15:59:16 +00:00
|
|
|
/**
|
|
|
|
* SkStream -- abstraction for a source of bytes. Subclasses can be backed by
|
|
|
|
* memory, or a file, or something else.
|
|
|
|
*
|
|
|
|
* NOTE:
|
|
|
|
*
|
|
|
|
* Classic "streams" APIs are sort of async, in that on a request for N
|
|
|
|
* bytes, they may return fewer than N bytes on a given call, in which case
|
|
|
|
* the caller can "try again" to get more bytes, eventually (modulo an error)
|
|
|
|
* receiving their total N bytes.
|
2013-05-08 07:01:40 +00:00
|
|
|
*
|
2013-05-07 15:59:16 +00:00
|
|
|
* Skia streams behave differently. They are effectively synchronous, and will
|
|
|
|
* always return all N bytes of the request if possible. If they return fewer
|
|
|
|
* (the read() call returns the number of bytes read) then that means there is
|
|
|
|
* no more data (at EOF or hit an error). The caller should *not* call again
|
|
|
|
* in hopes of fulfilling more of the request.
|
|
|
|
*/
|
2013-05-29 13:43:31 +00:00
|
|
|
class SK_API SkStream : public SkRefCnt { //TODO: remove SkRefCnt
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2013-03-18 21:08:46 +00:00
|
|
|
/**
|
|
|
|
* Attempts to open the specified file, and return a stream to it (using
|
|
|
|
* mmap if available). On success, the caller must call unref() on the
|
|
|
|
* returned object. On failure, returns NULL.
|
|
|
|
*/
|
2013-05-29 13:43:31 +00:00
|
|
|
static SkStreamAsset* NewFromFile(const char path[]);
|
2013-03-19 07:15:10 +00:00
|
|
|
|
2012-06-21 20:25:03 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkStream)
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Reads or skips size number of bytes.
|
|
|
|
* If buffer == NULL, skip size bytes, return how many were skipped.
|
|
|
|
* If buffer != NULL, copy size bytes into buffer, return how many were copied.
|
|
|
|
* @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
|
|
|
|
* @param size the number of bytes to skip or copy
|
2013-07-19 13:55:41 +00:00
|
|
|
* @return the number of bytes actually read.
|
2013-05-29 13:43:31 +00:00
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual size_t read(void* buffer, size_t size) = 0;
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Skip size number of bytes.
|
|
|
|
* @return the actual number bytes that could be skipped.
|
|
|
|
*/
|
|
|
|
size_t skip(size_t size) {
|
2013-11-13 19:53:46 +00:00
|
|
|
return this->read(NULL, size);
|
2013-05-29 13:43:31 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 13:55:41 +00:00
|
|
|
/** Returns true when all the bytes in the stream have been read.
|
|
|
|
* This may return true early (when there are no more bytes to be read)
|
|
|
|
* or late (after the first unsuccessful read).
|
2013-05-29 13:43:31 +00:00
|
|
|
*/
|
2013-11-13 19:53:46 +00:00
|
|
|
virtual bool isAtEnd() const = 0;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
int8_t readS8();
|
|
|
|
int16_t readS16();
|
|
|
|
int32_t readS32();
|
|
|
|
|
|
|
|
uint8_t readU8() { return (uint8_t)this->readS8(); }
|
|
|
|
uint16_t readU16() { return (uint16_t)this->readS16(); }
|
|
|
|
uint32_t readU32() { return (uint32_t)this->readS32(); }
|
|
|
|
|
|
|
|
bool readBool() { return this->readU8() != 0; }
|
|
|
|
SkScalar readScalar();
|
|
|
|
size_t readPackedUInt();
|
2012-06-21 20:25:03 +00:00
|
|
|
|
2012-07-02 19:35:13 +00:00
|
|
|
/**
|
2013-03-20 15:56:03 +00:00
|
|
|
* Reconstitute an SkData object that was written to the stream
|
|
|
|
* using SkWStream::writeData().
|
2012-07-02 19:35:13 +00:00
|
|
|
*/
|
|
|
|
SkData* readData();
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
//SkStreamRewindable
|
2013-08-28 13:08:54 +00:00
|
|
|
/** Rewinds to the beginning of the stream. Returns true if the stream is known
|
|
|
|
* to be at the beginning after this call returns.
|
|
|
|
*/
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual bool rewind() { return false; }
|
|
|
|
|
|
|
|
/** Duplicates this stream. If this cannot be done, returns NULL.
|
|
|
|
* The returned stream will be positioned at the beginning of its data.
|
|
|
|
*/
|
|
|
|
virtual SkStreamRewindable* duplicate() const { return NULL; }
|
|
|
|
|
|
|
|
//SkStreamSeekable
|
|
|
|
/** Returns true if this stream can report it's current position. */
|
|
|
|
virtual bool hasPosition() const { return false; }
|
|
|
|
/** Returns the current position in the stream. If this cannot be done, returns 0. */
|
|
|
|
virtual size_t getPosition() const { return 0; }
|
|
|
|
|
|
|
|
/** Seeks to an absolute position in the stream. If this cannot be done, returns false.
|
|
|
|
* If an attempt is made to seek past the end of the stream, the position will be set
|
|
|
|
* to the end of the stream.
|
|
|
|
*/
|
|
|
|
virtual bool seek(size_t position) { return false; }
|
|
|
|
|
|
|
|
/** Seeks to an relative offset in the stream. If this cannot be done, returns false.
|
|
|
|
* If an attempt is made to move to a position outside the stream, the position will be set
|
|
|
|
* to the closest point within the stream (beginning or end).
|
|
|
|
*/
|
|
|
|
virtual bool move(long offset) { return false; }
|
|
|
|
|
|
|
|
/** Duplicates this stream. If this cannot be done, returns NULL.
|
|
|
|
* The returned stream will be positioned the same as this stream.
|
|
|
|
*/
|
|
|
|
virtual SkStreamSeekable* fork() const { return NULL; }
|
|
|
|
|
|
|
|
//SkStreamAsset
|
|
|
|
/** Returns true if this stream can report it's total length. */
|
|
|
|
virtual bool hasLength() const { return false; }
|
|
|
|
/** Returns the total length of the stream. If this cannot be done, returns 0. */
|
2013-11-13 19:53:46 +00:00
|
|
|
virtual size_t getLength() const { return 0; }
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
//SkStreamMemory
|
|
|
|
/** Returns the starting address for the data. If this cannot be done, returns NULL. */
|
|
|
|
//TODO: replace with virtual const SkData* getData()
|
|
|
|
virtual const void* getMemoryBase() { return NULL; }
|
|
|
|
|
2012-06-21 20:25:03 +00:00
|
|
|
private:
|
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
|
|
|
|
class SK_API SkStreamRewindable : public SkStream {
|
|
|
|
public:
|
|
|
|
virtual bool rewind() SK_OVERRIDE = 0;
|
|
|
|
virtual SkStreamRewindable* duplicate() const SK_OVERRIDE = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
|
|
|
|
class SK_API SkStreamSeekable : public SkStreamRewindable {
|
|
|
|
public:
|
|
|
|
virtual SkStreamSeekable* duplicate() const SK_OVERRIDE = 0;
|
|
|
|
|
|
|
|
virtual bool hasPosition() const SK_OVERRIDE { return true; }
|
|
|
|
virtual size_t getPosition() const SK_OVERRIDE = 0;
|
|
|
|
virtual bool seek(size_t position) SK_OVERRIDE = 0;
|
|
|
|
virtual bool move(long offset) SK_OVERRIDE = 0;
|
|
|
|
virtual SkStreamSeekable* fork() const SK_OVERRIDE = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
|
|
|
|
class SK_API SkStreamAsset : public SkStreamSeekable {
|
|
|
|
public:
|
|
|
|
virtual SkStreamAsset* duplicate() const SK_OVERRIDE = 0;
|
|
|
|
virtual SkStreamAsset* fork() const SK_OVERRIDE = 0;
|
|
|
|
|
|
|
|
virtual bool hasLength() const SK_OVERRIDE { return true; }
|
|
|
|
virtual size_t getLength() const SK_OVERRIDE = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
|
|
|
|
class SK_API SkStreamMemory : public SkStreamAsset {
|
|
|
|
public:
|
|
|
|
virtual SkStreamMemory* duplicate() const SK_OVERRIDE = 0;
|
|
|
|
virtual SkStreamMemory* fork() const SK_OVERRIDE = 0;
|
|
|
|
|
|
|
|
virtual const void* getMemoryBase() SK_OVERRIDE = 0;
|
|
|
|
};
|
|
|
|
|
2011-03-30 20:14:49 +00:00
|
|
|
class SK_API SkWStream : SkNoncopyable {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-26 15:30:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT_ROOT(SkWStream)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual ~SkWStream();
|
|
|
|
|
|
|
|
/** Called to write bytes to a SkWStream. Returns true on success
|
|
|
|
@param buffer the address of at least size bytes to be written to the stream
|
|
|
|
@param size The number of bytes in buffer to write to the stream
|
|
|
|
@return true on success
|
|
|
|
*/
|
|
|
|
virtual bool write(const void* buffer, size_t size) = 0;
|
|
|
|
virtual void newline();
|
|
|
|
virtual void flush();
|
|
|
|
|
2014-03-06 17:16:26 +00:00
|
|
|
virtual size_t bytesWritten() const = 0;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// helpers
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool write8(U8CPU);
|
|
|
|
bool write16(U16CPU);
|
|
|
|
bool write32(uint32_t);
|
|
|
|
|
|
|
|
bool writeText(const char text[]);
|
|
|
|
bool writeDecAsText(int32_t);
|
2010-10-12 23:08:13 +00:00
|
|
|
bool writeBigDecAsText(int64_t, int minDigits = 0);
|
2008-12-17 15:59:43 +00:00
|
|
|
bool writeHexAsText(uint32_t, int minDigits = 0);
|
|
|
|
bool writeScalarAsText(SkScalar);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool writeBool(bool v) { return this->write8(v); }
|
|
|
|
bool writeScalar(SkScalar);
|
|
|
|
bool writePackedUInt(size_t);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2014-03-05 18:25:20 +00:00
|
|
|
bool writeStream(SkStream* input, size_t length);
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2013-03-20 15:56:03 +00:00
|
|
|
/**
|
|
|
|
* Append an SkData object to the stream, such that it can be read
|
|
|
|
* out of the stream using SkStream::readData().
|
|
|
|
*
|
|
|
|
* Note that the encoding method used to write the SkData object
|
|
|
|
* to the stream may change over time. This method DOES NOT
|
|
|
|
* just write the raw content of the SkData object to the stream.
|
|
|
|
*/
|
2011-06-24 19:12:12 +00:00
|
|
|
bool writeData(const SkData*);
|
2014-03-05 18:25:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This returns the number of bytes in the stream required to store
|
|
|
|
* 'value'.
|
|
|
|
*/
|
|
|
|
static int SizeOfPackedUInt(size_t value);
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkString.h"
|
2013-10-11 18:50:45 +00:00
|
|
|
#include <stdio.h>
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
struct SkFILE;
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** A stream that wraps a C FILE* file stream. */
|
|
|
|
class SK_API SkFILEStream : public SkStreamAsset {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-26 15:30:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkFILEStream)
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Initialize the stream by calling sk_fopen on the specified path.
|
|
|
|
* This internal stream will be closed in the destructor.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
explicit SkFILEStream(const char path[] = NULL);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
enum Ownership {
|
|
|
|
kCallerPasses_Ownership,
|
|
|
|
kCallerRetains_Ownership
|
|
|
|
};
|
|
|
|
/** Initialize the stream with an existing C file stream.
|
|
|
|
* While this stream exists, it assumes exclusive access to the C file stream.
|
|
|
|
* The C file stream will be closed in the destructor unless the caller specifies
|
|
|
|
* kCallerRetains_Ownership.
|
|
|
|
*/
|
|
|
|
explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual ~SkFILEStream();
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Returns true if the current path could be opened. */
|
2008-12-17 15:59:43 +00:00
|
|
|
bool isValid() const { return fFILE != NULL; }
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
/** Close the current file, and open a new file with the specified path.
|
|
|
|
* If path is NULL, just close the current file.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
void setPath(const char path[]);
|
|
|
|
|
2011-10-03 16:01:10 +00:00
|
|
|
virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual bool isAtEnd() const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual bool rewind() SK_OVERRIDE;
|
|
|
|
virtual SkStreamAsset* duplicate() const SK_OVERRIDE;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual size_t getPosition() const SK_OVERRIDE;
|
|
|
|
virtual bool seek(size_t position) SK_OVERRIDE;
|
|
|
|
virtual bool move(long offset) SK_OVERRIDE;
|
|
|
|
virtual SkStreamAsset* fork() const SK_OVERRIDE;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual size_t getLength() const SK_OVERRIDE;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-07-19 13:55:41 +00:00
|
|
|
virtual const void* getMemoryBase() SK_OVERRIDE;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2013-05-29 13:43:31 +00:00
|
|
|
SkFILE* fFILE;
|
|
|
|
SkString fName;
|
|
|
|
Ownership fOwnership;
|
|
|
|
// fData is lazilly initialized when needed.
|
|
|
|
mutable SkAutoTUnref<SkData> fData;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
typedef SkStreamAsset INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
class SK_API SkMemoryStream : public SkStreamMemory {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-26 15:30:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkMemoryStream)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMemoryStream();
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
/** We allocate (and free) the memory. Write to it via getMemoryBase() */
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMemoryStream(size_t length);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
/** If copyData is true, the stream makes a private copy of the data. */
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMemoryStream(const void* data, size_t length, bool copyData = false);
|
2013-01-09 22:02:58 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Use the specified data as the memory for this stream.
|
|
|
|
* The stream will call ref() on the data (assuming it is not NULL).
|
2013-01-09 22:02:58 +00:00
|
|
|
*/
|
|
|
|
SkMemoryStream(SkData*);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual ~SkMemoryStream();
|
|
|
|
|
|
|
|
/** Resets the stream to the specified data and length,
|
|
|
|
just like the constructor.
|
|
|
|
if copyData is true, the stream makes a private copy of the data
|
|
|
|
*/
|
|
|
|
virtual void setMemory(const void* data, size_t length,
|
|
|
|
bool copyData = false);
|
2011-02-23 20:46:31 +00:00
|
|
|
/** Replace any memory buffer with the specified buffer. The caller
|
|
|
|
must have allocated data with sk_malloc or sk_realloc, since it
|
|
|
|
will be freed with sk_free.
|
|
|
|
*/
|
|
|
|
void setMemoryOwned(const void* data, size_t length);
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Return the stream's data in a SkData.
|
|
|
|
* The caller must call unref() when it is finished using the data.
|
2011-06-24 19:12:12 +00:00
|
|
|
*/
|
|
|
|
SkData* copyToData() const;
|
|
|
|
|
|
|
|
/**
|
2013-05-29 13:43:31 +00:00
|
|
|
* Use the specified data as the memory for this stream.
|
|
|
|
* The stream will call ref() on the data (assuming it is not NULL).
|
|
|
|
* The function returns the data parameter as a convenience.
|
2011-06-24 19:12:12 +00:00
|
|
|
*/
|
|
|
|
SkData* setData(SkData*);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void skipToAlign4();
|
|
|
|
const void* getAtPos();
|
|
|
|
size_t peek() const { return fOffset; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual bool isAtEnd() const SK_OVERRIDE;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual bool rewind() SK_OVERRIDE;
|
|
|
|
virtual SkMemoryStream* duplicate() const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual size_t getPosition() const SK_OVERRIDE;
|
|
|
|
virtual bool seek(size_t position) SK_OVERRIDE;
|
|
|
|
virtual bool move(long offset) SK_OVERRIDE;
|
|
|
|
virtual SkMemoryStream* fork() const SK_OVERRIDE;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual size_t getLength() const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-10-03 16:01:10 +00:00
|
|
|
virtual const void* getMemoryBase() SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
private:
|
2013-05-29 13:43:31 +00:00
|
|
|
SkData* fData;
|
|
|
|
size_t fOffset;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
typedef SkStreamMemory INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-17 19:10:36 +00:00
|
|
|
class SK_API SkFILEWStream : public SkWStream {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-26 15:30:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkFILEWStream)
|
|
|
|
|
|
|
|
SkFILEWStream(const char path[]);
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual ~SkFILEWStream();
|
|
|
|
|
|
|
|
/** Returns true if the current path could be opened.
|
|
|
|
*/
|
|
|
|
bool isValid() const { return fFILE != NULL; }
|
|
|
|
|
2011-10-03 16:01:10 +00:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual void flush() SK_OVERRIDE;
|
2014-03-06 17:16:26 +00:00
|
|
|
virtual size_t bytesWritten() const SK_OVERRIDE;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkFILE* fFILE;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkMemoryWStream : public SkWStream {
|
|
|
|
public:
|
2012-06-26 15:30:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkMemoryWStream)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMemoryWStream(void* buffer, size_t size);
|
2011-10-03 16:01:10 +00:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
2014-03-06 17:16:26 +00:00
|
|
|
virtual size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
|
2012-02-22 21:00:42 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
char* fBuffer;
|
|
|
|
size_t fMaxLength;
|
|
|
|
size_t fBytesWritten;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2011-03-30 20:14:49 +00:00
|
|
|
class SK_API SkDynamicMemoryWStream : public SkWStream {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-26 15:30:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkDynamicMemoryWStream)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkDynamicMemoryWStream();
|
|
|
|
virtual ~SkDynamicMemoryWStream();
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2011-10-03 16:01:10 +00:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
2014-03-06 17:16:26 +00:00
|
|
|
virtual size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
|
2008-12-17 15:59:43 +00:00
|
|
|
// random access write
|
|
|
|
// modifies stream and returns true if offset + size is less than or equal to getOffset()
|
|
|
|
bool write(const void* buffer, size_t offset, size_t size);
|
|
|
|
bool read(void* buffer, size_t offset, size_t size);
|
2010-10-26 19:48:49 +00:00
|
|
|
size_t getOffset() const { return fBytesWritten; }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
// copy what has been written to the stream into dst
|
2011-06-24 19:12:12 +00:00
|
|
|
void copyTo(void* dst) const;
|
2011-06-23 21:48:04 +00:00
|
|
|
|
|
|
|
/**
|
2011-06-24 19:12:12 +00:00
|
|
|
* Return a copy of the data written so far. This call is responsible for
|
|
|
|
* calling unref() when they are finished with the data.
|
2011-06-23 21:48:04 +00:00
|
|
|
*/
|
2011-06-24 13:07:31 +00:00
|
|
|
SkData* copyToData() const;
|
2011-06-23 21:48:04 +00:00
|
|
|
|
2013-07-19 13:55:41 +00:00
|
|
|
/** Reset, returning a reader stream with the current content. */
|
2013-07-19 22:32:11 +00:00
|
|
|
SkStreamAsset* detachAsStream();
|
2013-07-19 13:55:41 +00:00
|
|
|
|
|
|
|
/** Reset the stream to its original, empty, state. */
|
2008-12-17 15:59:43 +00:00
|
|
|
void reset();
|
|
|
|
void padToAlign4();
|
|
|
|
private:
|
|
|
|
struct Block;
|
|
|
|
Block* fHead;
|
|
|
|
Block* fTail;
|
|
|
|
size_t fBytesWritten;
|
2011-06-24 19:12:12 +00:00
|
|
|
mutable SkData* fCopy; // is invalidated if we write after it is created
|
|
|
|
|
|
|
|
void invalidateCopy();
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-07-19 13:55:41 +00:00
|
|
|
// For access to the Block type.
|
|
|
|
friend class SkBlockMemoryStream;
|
|
|
|
friend class SkBlockMemoryRefCnt;
|
|
|
|
|
2012-06-26 15:30:08 +00:00
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-05 17:49:10 +00:00
|
|
|
class SK_API SkDebugWStream : public SkWStream {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2014-03-06 17:16:26 +00:00
|
|
|
SkDebugWStream() : fBytesWritten(0) {}
|
2012-06-26 15:30:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkDebugWStream)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// overrides
|
2011-10-03 16:01:10 +00:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual void newline() SK_OVERRIDE;
|
2014-03-06 17:16:26 +00:00
|
|
|
virtual size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
|
2012-06-26 15:30:08 +00:00
|
|
|
|
|
|
|
private:
|
2014-03-06 17:16:26 +00:00
|
|
|
size_t fBytesWritten;
|
2012-06-26 15:30:08 +00:00
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// for now
|
|
|
|
typedef SkFILEStream SkURLStream;
|
|
|
|
|
|
|
|
#endif
|