2011-07-28 18:26:00 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
/*
|
2011-07-28 18:26:00 +04:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 18:59:43 +03:00
|
|
|
*
|
2011-07-28 18:26:00 +04:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 18:59:43 +03:00
|
|
|
*/
|
|
|
|
|
2011-07-28 18:26:00 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
#ifndef SkStream_DEFINED
|
|
|
|
#define SkStream_DEFINED
|
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
|
|
|
|
2011-06-24 17:07:31 +04:00
|
|
|
class SkData;
|
2011-06-24 01:48:04 +04:00
|
|
|
|
2011-04-27 00:06:08 +04:00
|
|
|
class SK_API SkStream : public SkRefCnt {
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2012-06-22 00:25:03 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
/** Called to rewind to the beginning of the stream. If this cannot be
|
|
|
|
done, return false.
|
|
|
|
*/
|
|
|
|
virtual bool rewind() = 0;
|
|
|
|
/** If this stream represents a file, this method returns the file's name.
|
|
|
|
If it does not, it returns NULL (the default behavior).
|
|
|
|
*/
|
|
|
|
virtual const char* getFileName();
|
|
|
|
/** Called to read or skip size number of bytes.
|
|
|
|
If buffer is NULL and size > 0, skip that many bytes, returning how many were skipped.
|
|
|
|
If buffer is NULL and size == 0, return the total length of the stream.
|
|
|
|
If buffer != NULL, copy the requested number of bytes into buffer, returning how many were copied.
|
|
|
|
@param buffer If buffer is NULL, ignore and just skip size bytes, otherwise copy size bytes into buffer
|
|
|
|
@param size The number of bytes to skip or copy
|
|
|
|
@return bytes read on success
|
|
|
|
*/
|
|
|
|
virtual size_t read(void* buffer, size_t size) = 0;
|
|
|
|
|
|
|
|
/** Return the total length of the stream.
|
|
|
|
*/
|
|
|
|
size_t getLength() { return this->read(NULL, 0); }
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
/** Skip the specified number of bytes, returning the actual number
|
|
|
|
of bytes that could be skipped.
|
|
|
|
*/
|
|
|
|
size_t skip(size_t bytes);
|
|
|
|
|
|
|
|
/** If the stream is backed by RAM, this method returns the starting
|
|
|
|
address for the data. If not (i.e. it is backed by a file or other
|
|
|
|
structure), this method returns NULL.
|
|
|
|
The default implementation returns NULL.
|
|
|
|
*/
|
|
|
|
virtual const void* getMemoryBase();
|
|
|
|
|
|
|
|
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-22 00:25:03 +04:00
|
|
|
|
2012-07-02 23:35:13 +04:00
|
|
|
/**
|
|
|
|
* Create a new SkData from the stream contents. This balances the call
|
|
|
|
* SkWStream::writeData().
|
|
|
|
*/
|
|
|
|
SkData* readData();
|
|
|
|
|
2012-06-22 00:25:03 +04:00
|
|
|
private:
|
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
2011-03-31 00:14:49 +04:00
|
|
|
class SK_API SkWStream : SkNoncopyable {
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT_ROOT(SkWStream)
|
|
|
|
|
2008-12-17 18:59:43 +03: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();
|
|
|
|
|
|
|
|
// helpers
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
bool write8(U8CPU);
|
|
|
|
bool write16(U16CPU);
|
|
|
|
bool write32(uint32_t);
|
|
|
|
|
|
|
|
bool writeText(const char text[]);
|
|
|
|
bool writeDecAsText(int32_t);
|
2010-10-13 03:08:13 +04:00
|
|
|
bool writeBigDecAsText(int64_t, int minDigits = 0);
|
2008-12-17 18:59:43 +03:00
|
|
|
bool writeHexAsText(uint32_t, int minDigits = 0);
|
|
|
|
bool writeScalarAsText(SkScalar);
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
bool writeBool(bool v) { return this->write8(v); }
|
|
|
|
bool writeScalar(SkScalar);
|
|
|
|
bool writePackedUInt(size_t);
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
bool writeStream(SkStream* input, size_t length);
|
2011-06-24 23:12:12 +04:00
|
|
|
|
|
|
|
bool writeData(const SkData*);
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkString.h"
|
|
|
|
|
|
|
|
struct SkFILE;
|
|
|
|
|
|
|
|
/** A stream that reads from a FILE*, which is opened in the constructor and
|
|
|
|
closed in the destructor
|
|
|
|
*/
|
|
|
|
class SkFILEStream : public SkStream {
|
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkFILEStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
/** Initialize the stream by calling fopen on the specified path. Will be
|
|
|
|
closed in the destructor.
|
|
|
|
*/
|
|
|
|
explicit SkFILEStream(const char path[] = NULL);
|
|
|
|
virtual ~SkFILEStream();
|
|
|
|
|
|
|
|
/** Returns true if the current path could be opened.
|
|
|
|
*/
|
|
|
|
bool isValid() const { return fFILE != NULL; }
|
|
|
|
/** Close the current file, and open a new file with the specified
|
|
|
|
path. If path is NULL, just close the current file.
|
|
|
|
*/
|
|
|
|
void setPath(const char path[]);
|
|
|
|
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool rewind() SK_OVERRIDE;
|
|
|
|
virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual const char* getFileName() SK_OVERRIDE;
|
2008-12-17 18:59:43 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
SkFILE* fFILE;
|
|
|
|
SkString fName;
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
typedef SkStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/** A stream that reads from a file descriptor
|
|
|
|
*/
|
|
|
|
class SkFDStream : public SkStream {
|
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkFDStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
/** Initialize the stream with a dup() of the specified file descriptor.
|
|
|
|
If closeWhenDone is true, then the descriptor will be closed in the
|
|
|
|
destructor.
|
|
|
|
*/
|
|
|
|
SkFDStream(int fileDesc, bool closeWhenDone);
|
|
|
|
virtual ~SkFDStream();
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
/** Returns true if the current path could be opened.
|
|
|
|
*/
|
|
|
|
bool isValid() const { return fFD >= 0; }
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool rewind() SK_OVERRIDE;
|
|
|
|
virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual const char* getFileName() SK_OVERRIDE { return NULL; }
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
private:
|
|
|
|
int fFD;
|
|
|
|
bool fCloseWhenDone;
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
typedef SkStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkMemoryStream : public SkStream {
|
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkMemoryStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
SkMemoryStream();
|
|
|
|
/** We allocate (and free) the memory. Write to it via getMemoryBase()
|
|
|
|
*/
|
|
|
|
SkMemoryStream(size_t length);
|
|
|
|
/** if copyData is true, the stream makes a private copy of the data
|
|
|
|
*/
|
|
|
|
SkMemoryStream(const void* data, size_t length, bool copyData = false);
|
|
|
|
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 23:46:31 +03: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 23:12:12 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the stream's data in a SkData. The caller must call unref() when
|
|
|
|
* it is finished using the data.
|
|
|
|
*/
|
|
|
|
SkData* copyToData() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
SkData* setData(SkData*);
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
void skipToAlign4();
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool rewind() SK_OVERRIDE;
|
|
|
|
virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual const void* getMemoryBase() SK_OVERRIDE;
|
2008-12-17 18:59:43 +03:00
|
|
|
const void* getAtPos();
|
|
|
|
size_t seek(size_t offset);
|
|
|
|
size_t peek() const { return fOffset; }
|
2012-08-23 22:09:54 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
private:
|
2011-06-24 23:12:12 +04:00
|
|
|
SkData* fData;
|
|
|
|
size_t fOffset;
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
typedef SkStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \class SkBufferStream
|
|
|
|
This is a wrapper class that adds buffering to another stream.
|
|
|
|
The caller can provide the buffer, or ask SkBufferStream to allocated/free
|
|
|
|
it automatically.
|
|
|
|
*/
|
|
|
|
class SkBufferStream : public SkStream {
|
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkBufferStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
/** Provide the stream to be buffered (proxy), and the size of the buffer that
|
|
|
|
should be used. This will be allocated and freed automatically. If bufferSize is 0,
|
|
|
|
a default buffer size will be used.
|
|
|
|
The proxy stream is referenced, and will be unreferenced in when the
|
|
|
|
bufferstream is destroyed.
|
|
|
|
*/
|
|
|
|
SkBufferStream(SkStream* proxy, size_t bufferSize = 0);
|
|
|
|
/** Provide the stream to be buffered (proxy), and a buffer and size to be used.
|
|
|
|
This buffer is owned by the caller, and must be at least bufferSize bytes big.
|
|
|
|
Passing NULL for buffer will cause the buffer to be allocated/freed automatically.
|
|
|
|
If buffer is not NULL, it is an error for bufferSize to be 0.
|
|
|
|
The proxy stream is referenced, and will be unreferenced in when the
|
|
|
|
bufferstream is destroyed.
|
|
|
|
*/
|
|
|
|
SkBufferStream(SkStream* proxy, void* buffer, size_t bufferSize);
|
|
|
|
virtual ~SkBufferStream();
|
|
|
|
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool rewind() SK_OVERRIDE;
|
|
|
|
virtual const char* getFileName() SK_OVERRIDE;
|
|
|
|
virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual const void* getMemoryBase() SK_OVERRIDE;
|
2008-12-17 18:59:43 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
kDefaultBufferSize = 128
|
|
|
|
};
|
|
|
|
// illegal
|
|
|
|
SkBufferStream(const SkBufferStream&);
|
|
|
|
SkBufferStream& operator=(const SkBufferStream&);
|
|
|
|
|
|
|
|
SkStream* fProxy;
|
|
|
|
char* fBuffer;
|
|
|
|
size_t fOrigBufferSize, fBufferSize, fBufferOffset;
|
|
|
|
bool fWeOwnTheBuffer;
|
|
|
|
|
|
|
|
void init(void*, size_t);
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
typedef SkStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-17 23:10:36 +04:00
|
|
|
class SK_API SkFILEWStream : public SkWStream {
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkFILEWStream)
|
|
|
|
|
|
|
|
SkFILEWStream(const char path[]);
|
2008-12-17 18:59:43 +03:00
|
|
|
virtual ~SkFILEWStream();
|
|
|
|
|
|
|
|
/** Returns true if the current path could be opened.
|
|
|
|
*/
|
|
|
|
bool isValid() const { return fFILE != NULL; }
|
|
|
|
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual void flush() SK_OVERRIDE;
|
2012-06-26 19:30:08 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
private:
|
|
|
|
SkFILE* fFILE;
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkMemoryWStream : public SkWStream {
|
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkMemoryWStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
SkMemoryWStream(void* buffer, size_t size);
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
2012-02-23 01:00:42 +04:00
|
|
|
size_t bytesWritten() const { return fBytesWritten; }
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
private:
|
|
|
|
char* fBuffer;
|
|
|
|
size_t fMaxLength;
|
|
|
|
size_t fBytesWritten;
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
2011-03-31 00:14:49 +04:00
|
|
|
class SK_API SkDynamicMemoryWStream : public SkWStream {
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkDynamicMemoryWStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
SkDynamicMemoryWStream();
|
|
|
|
virtual ~SkDynamicMemoryWStream();
|
2011-06-24 23:12:12 +04:00
|
|
|
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
2008-12-17 18:59:43 +03: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 23:48:49 +04:00
|
|
|
size_t getOffset() const { return fBytesWritten; }
|
2011-06-24 23:12:12 +04:00
|
|
|
size_t bytesWritten() const { return fBytesWritten; }
|
2008-12-17 18:59:43 +03:00
|
|
|
|
|
|
|
// copy what has been written to the stream into dst
|
2011-06-24 23:12:12 +04:00
|
|
|
void copyTo(void* dst) const;
|
2011-06-24 01:48:04 +04:00
|
|
|
|
|
|
|
/**
|
2011-06-24 23:12:12 +04: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-24 01:48:04 +04:00
|
|
|
*/
|
2011-06-24 17:07:31 +04:00
|
|
|
SkData* copyToData() const;
|
2011-06-24 01:48:04 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
// reset the stream to its original state
|
|
|
|
void reset();
|
|
|
|
void padToAlign4();
|
|
|
|
private:
|
|
|
|
struct Block;
|
|
|
|
Block* fHead;
|
|
|
|
Block* fTail;
|
|
|
|
size_t fBytesWritten;
|
2011-06-24 23:12:12 +04:00
|
|
|
mutable SkData* fCopy; // is invalidated if we write after it is created
|
|
|
|
|
|
|
|
void invalidateCopy();
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SkDebugWStream : public SkWStream {
|
|
|
|
public:
|
2012-06-26 19:30:08 +04:00
|
|
|
SK_DECLARE_INST_COUNT(SkDebugWStream)
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
// overrides
|
2011-10-03 20:01:10 +04:00
|
|
|
virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
|
|
|
|
virtual void newline() SK_OVERRIDE;
|
2012-06-26 19:30:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// for now
|
|
|
|
typedef SkFILEStream SkURLStream;
|
|
|
|
|
|
|
|
#endif
|