class DataStream

General purpose class used for encapsulating the reading and writing of data from and to various sources using a common interface.

Public

Constructors

DataStream

DataStream(u16 accessMode = READ)

Creates an unnamed stream.

DataStream

DataStream(const String &name, u16 accessMode = READ)

Creates a named stream.

Methods

~DataStream

virtual ~DataStream() noexcept = default

GetName

const String &GetName() const

GetAccessMode

u16 GetAccessMode() const

IsReadable

virtual bool IsReadable() const

IsWriteable

virtual bool IsWriteable() const

IsFile

virtual bool IsFile() const = 0

Checks whether the stream reads/writes from a file system.

Read

virtual size_t Read(void *outData, size_t byteCount) const = 0

Read the requisite number of bytes from the stream, stopping at the end of the file.

Advances the read pointer.

outData
Pre-allocated buffer to read the data into.
byteCount
Number of bytes to read.

Returns: Number of bytes actually read.

Write

virtual size_t Write(const void *data, size_t byteCount)

Write the requisite number of bytes to the stream and advance the write pointer.

data
Buffer containing bytes to write.
byteCount
Number of bytes to write.

Returns: Number of bytes actually written.

ReadBits

virtual size_t ReadBits(uint8_t *outData, uint32_t count)

Reads bits from the stream into the provided buffer from the current cursor location and advances the cursor.

If the stream doesn't support per-bit reads, data size will be rounded up to nearest byte.

outData
Buffer to read the data from. Must have enough capacity to store bits.
count
Number of bits to read.

Returns: Number of bits actually read.

WriteBits

virtual size_t WriteBits(const uint8_t *data, uint32_t count)

Writes bits from the provided buffer into the stream at the current cursor location and advances the cursor.

If the stream doesn't support per-bit writes, data size will be rounded up to nearest byte.

data
Buffer to write the data from. Must have enough capacity to store bits.
count
Number of bits to write.

Returns: Number of bits actually written.

WriteString

virtual void WriteString(const String &string, StringEncoding encoding = StringEncoding::UTF8)

Writes the provided narrow string to the steam.

String is convered to the required encoding before being written.

string
String containing narrow characters to write, encoded as UTF8.
encoding
Encoding to convert the string to before writing.

WriteString

virtual void WriteString(const WString &string, StringEncoding encoding = StringEncoding::UTF8)

Writes the provided wide string to the steam.

String is convered to the required encoding before being written.

string
String containing wide characters to write, encoded as specified by platform for wide characters.
encoding
Encoding to convert the string to before writing.

GetAsString

virtual String GetAsString()

Returns a string containing the entire stream.

Returns: String data encoded as UTF-8.

GetAsWString

virtual WString GetAsWString()

Returns a wide string containing the entire stream.

Returns: Wide string encoded as specified by current platform.

Skip

virtual size_t Skip(size_t count) = 0

Skip a defined number of bytes.

Returns the actual number of bytes skipped.

Seek

virtual size_t Seek(size_t pos) = 0

Repositions the read or write cursor to the specified byte.

Returns the actual byte the cursor has been placed at, in case end has been reached earlier

Tell

virtual size_t Tell() const = 0

Returns the current cursor byte offset from beginning.

Align

virtual void Align(uint32_t count = 1)

Aligns the read/write cursor to a byte boundary. determines the alignment in bytes.

Note the requested alignment might not be achieved if count > 1 and it would move the cursor past the capacity of the buffer, as the cursor will be clamped to buffer end regardless of alignment.

Eof

virtual bool Eof() const = 0

Returns true if the stream has reached the end.

Size

size_t Size() const

Returns the total size of the data to be read from the stream, or 0 if this is indeterminate for this stream.

Clone

virtual SPtr<DataStream> Clone(bool copyData = true) const = 0

Creates a copy of this stream.

copyData
If true the internal stream data will be copied as well, otherwise it will just reference the data from the original stream (in which case the caller must ensure the original stream outlives the clone). This is not relevant for file streams.

Flush

virtual bool Flush() = 0

Flushes the stream, writing any buffer data to the destination.

Returns false if some error occurred and data was not written correctly. Does not need to be called if stream was just read from.

Close

virtual bool Close() = 0

Closes the stream.

This makes further operations invalid. Executes a Flush() before closing, and passes the return value from Flush().

Protected

Fields

mName

String mName

mSize

size_t mSize

mAccess

u16 mAccess