class Stream
Stream is the base class of all U++ streams.
U++ streams generally serve in two different roles:
As basic raw binary streams, which includes text input and output.
As binary serialization target/source. This mainly needs to store flag indicating serialization direction.
While Stream offers a basic interface for stream based operations, more specific operations are handled by one of the following classes derived from Stream:
In order to achieve optimal performance of buffered stream operations, the most frequent operations have a little bit more complicated implementation that demands proper definition of virtual methods, as they must correctly adjust some protected data members of Stream. Therefore the implementation of some method can be considered a part of interface definition:
void Stream::Put(int c)
{
if(ptr < wrlim)
*ptr++ = c;
else
_Put(c);
}
int Stream::Term()
{
return ptr < rdlim ? *ptr : _Term();
}
int Stream::Get()
{
return ptr < rdlim ? *ptr++ : _Get();
}
int64 Stream::GetPos() const
{
return dword(ptr - buffer) + pos;
}
int64 pos
Protected.
Position of buffer in the stream.
byte *buffer
Protected.
Pointer to beginning of buffer.
byte *ptr
Protected.
Pointer to current input/output byte.
byte *rdlim
Protected.
Read limit. Get method returns values from buffer as long as ptr < rdlim.
byte *wrlim
Protected.
Write limit. Put method returns values from buffer as long as ptr < wrlim.
virtual void _Put(int w)
Called by Put method in case that output byte cannot be stored into buffer (ptr >= wrlim). If Stream wants to use buffering, this method should adjust buffer, ptr and wrlim.
virtual int _Term()
Called by Term method in case that input byte is not in buffer (ptr >= rdlim). If Stream wants to use buffering, this method should adjust buffer, ptr and rdlim.
|
Return value |
Value at current position in the stream. Current position is not advanced. |
virtual int _Get()
Called by Get method in case that input byte is not in buffer (ptr >= rdlim). If Stream wants to use buffering, this method should adjust buffer, ptr and rdlim.
|
Return value |
Value read from the stream. |
virtual void _Put(const void *data, dword size)
Directly called by Put method. Writes a block of binary data.
virtual dword _Get(void *data, dword size)
Directly called by Get method.
Stream()
Default constructor. Sets stream into Loading serialization mode and zero level indentation. All protected variables are set to 0 / NULL.
Stream(const Stream& s)
Default copy constructor.
~Stream()
Default destructor.
Public Member List: Raw Operations
|
|
virtual void Seek(int64 pos)
Seeks to given position.
void SetVersion(int ver)
Sets the arbitrary number ver to the stream. Client code can use it in any way it needs, but it is designed to store the the version information during serialization.
int GetVersion() const
Reads the number set by SetVersion.
virtual int64 GetSize() const
|
Return value |
Size of stream. |
virtual void SetSize(int64 size)
Alters the size of the stream.
virtual void Flush()
If stream has any internal buffers (like FileStream), writes these buffers to OS.
virtual void Close()
Closes stream.
virtual bool IsOpen() const = 0
|
Return value |
true if stream is open. |
bool IsError() const
|
Return value |
true if error was encountered during stream operations since opening it or last ClearError call - error code is non-zero. |
bool IsOK() const
void SetError(int c = 0)
Sets stream error code.
void SetLastError()
Sets stream error to last OS-specific error (obtained e.g. by GetLastError call in Win32 or in errno in Posix). This error can be interpreted by GetErrorMessage function.
int GetError() const
Returns current error-code. Zero indicates no error.
String GetErrorText() const
Returns the text description of the last error.
void ClearError()
Clears error code.
int64 GetPos() const
|
Return value |
Current position in the stream. |
int64 GetLeft() const
|
Return value |
Bytes between current position and the end of stream - equivalent to GetSize() - GetPos(). |
void SeekEnd(int64 rel = 0)
Sets current position in the stream relative to the end of stream. Same as Seek(GetSize() + rel).
|
rel |
Position - should be less or equal to zero. |
void SeekCur(int64 rel)
Sets current position in the stream relative to the current position. Same as Seek(GetPos() + rel).
bool IsEof()
There are no more byte to be read from the stream. Is also true in case of error.
void Put(int c)
Puts single byte into the output stream.
int Term()
int Peek()
Peeks byte from input stream not advancing current position. If there are no more bytes in input stream or error occurred, negative value is returned.
int Get()
Reads single byte from input stream, advancing current position. If there are no more bytes in input stream or error occurred, negative value is returned.
|
Return value |
Byte read from input stream. |
const byte *PeekPtr(int size = 1)
This is a special optimization method; it might return a pointer to data of size bytes at current position in the stream, but it is allowed to return NULL - in that case you need to use Get to load data. PeekPtr does not move the position forward, you need to use some of Seek methods to do that.
const byte *GetPtr(int size = 1)
This is a special optimization method; it might return a pointer to data of size bytes at current position in the stream, but it is allowed to return NULL - in that case you need to use Get to load data. Unlike PeekPtr, it advances stream by size.
byte *PutPtr(int size = 1)
This is a special optimization method; it might return a writable pointer where you can write output data of size bytes, but it is allowed to return NULL - in that case you need to output data using Put. Advances stream by size.
const byte *GetSzPtr(int& size)
This is a special optimization method, it returns a pointer to data at current position of stream, sets size to the maximal possible amount of bytes that can be read from this pointer and advances stream by this size. size can be set to zero, in that case stream has to be read by another means (e.g. Get).
void Put(const void *data, int size)
Writes a block of raw binary data to the output stream.
int Get(void *data, int size)
Reads at most size bytes from the stream to data. Returns the number of bytes actually read.
String Get(int size)
Reads at most size bytes from the input stream and returns result as String.
String GetAll(int size)
Reads exactly size bytes from the input stream and returns result as String. If there is not enough bytes left, returns String::GetVoid(). Note that is size is large (currently > 4MB), function reads result in chunks in order to avoid problems with corrupted streams, e.g. situation where the size is obtained from the stream, is corrupted and too big and allocating that much memory would cause out-of-memory situation.
int Skip(int size)
Skips size bytes in input Stream. This is similar to SeekCur(size), however it works with Streams that do not support seeking. Returns a number of bytes actually skipped (can be less than required because of EOF).
bool GetAll(void *data, int size)
Reads size bytes from the stream to memory at data. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception). Returns true if required number of bytes was read.
void Put64(const void *data, int64 size)
Writes a block of raw binary data to the output stream, unlike regular Put, block size can be greater than 2GB, if CPU is 64 bit.
int64 Get64(void *data, int64 size)
Reads at most size bytes from the stream to data. Returns the number of bytes actually read. Unlike regular Get, block size can be greater than 2GB, if CPU is 64 bit.
bool GetAll64(void *data, int64 size)
Reads size bytes from the stream to memory at data. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception). Returns true if required number of bytes was read. Unlike regular Get, block size can be greater than 2GB, if CPU is 64 bit.
size_t Get(Huge& h, size_t size)
Reads data into h, returns the size read.
bool GetAll(Huge& h, size_t size)
Reads data into h, returns the size read. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception).
void LoadThrowing()
Sets stream into the mode that throws LoadingError exception when LoadError is invoked. This mode is typical for serialization usage of stream.
void LoadError()
Performs SetError(ERROR_LOADING_FAILED). If Stream set to the LoadThrowing mode (by LoadThrowing() method), LoadingError exception is thrown.
int Get8()
Reads a single byte from the stream. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) - this is the difference from Get() method.
|
Return value |
Byte from stream or -1. |
int Get16()
Reads 16-bit value from the stream in platform specific format (either little-endian or big-endian). If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned.
|
Return value |
16-bit value. |
int Get32()
Reads 32-bit value from the stream in platform specific format (either little-endian or big-endian). If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned (but that is a valid return value as well).
|
Return value |
32-bit value. |
int64 Get64()
Reads 64-bit value from the stream in platform specific format (either little-endian or big-endian). If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned (but that is a valid return value as well).
|
Return value |
64-bit value. |
int Get16le()
Reads 16-bit value from the stream in the little-endian mode. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned.
|
Return value |
16-bit value. |
int Get32le()
Reads 32-bit value from the stream in the little-endian mode. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned (but that is a valid return value as well).
|
Return value |
32-bit value. |
int64 Get64le()
Reads 64-bit value from the stream in the little-endian mode. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned (but that is a valid value return as well).
|
Return value |
64-bit value. |
int Get16be()
Reads 16-bit value from the stream in the big-endian mode. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 is returned.
|
Return value |
16-bit value. |
int Get32be()
Reads 32-bit value from the stream in the big-endian mode. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned (but that is a valid value return as well).
|
Return value |
32-bit value. |
int64 Get64be()
Reads 64-bit value from the stream in the big-endian mode. If there is not enough data in the stream, LoadError is invoked (that in turn might throw an exception) and -1 returned (but that is a valid value return as well).
|
Return value |
64-bit value. |
String GetLine()
Reads single line from the stream. Line is delimited by '\n' character or the end of file (or error). '\r' characters are ignored.
|
Return value |
Line from the stream. |
int GetUtf8()
Reads single Utf8 encoded value from the stream.
|
Return value |
Utf-8 value. |
void Put16(word q)
Writes 16-bit value q in platform specific format (either little-endian or big-endian).
void Put32(dword q)
Writes 32-bit value q in platform specific format (either little-endian or big-endian).
void Put64(int64 q)
Writes 64-bit value q in platform specific format (either little-endian or big-endian).
void Put16le(word q)
Writes 16-bit value q in little-endian mode.
void Put32le(dword q)
Writes 32-bit value q in little-endian mode.
void Put64le(int64 q)
Writes 64-bit value q in little-endian mode.
void Put16be(word q)
Writes 16-bit value q in big-endian mode.
void Put32be(dword q)
Writes 32-bit value q in big-endian mode.
void Put64be(int64 q)
Writes 64-bit value q in big-endian mode.
void PutUtf8(int c)
Puts a single Utf8 value to the stream.
void Put(const char *s)
Writes zero terminated string to the stream (zero is not written).
void Put(const String& s)
Writes String to the the stream. String can contain zero characters.
void Put(int c, int count)
Writes single byte to the stream requested number of times.
void Put0(int count)
Writes zero byte to the stream requested number of times. This variation is mainly provided because Put(0, count) causes ambiguity as 0 is considered as NULL pointer too...
void PutCrLf()
Writes CR-LF pair to the stream.
void PutEol()
Writes platform specific "end of line" to the stream. It is CR-LF pair on Win32 platform or single LF on Posix platform.
Stream& operator<<(EOLenum)
This is the same as PutEol. EOLenum contains single value, EOL. This is operator allows more convenient way to express PutEol in the chain of << operators.
void PutLine(const char *s)
Writes a line to stream - equivalent of Put(s); PutEol();
|
s |
Zero-terminated string. |
void PutLine(const String& s)
Writes a line to stream - equivalent of Put(s); PutEol();
void Put(Stream& s, int64 size = INT64_MAX, dword click = 4096)
Writes a content of specified stream to the stream.
|
size |
Maximum number of bytes to write. Default value INT64_MAX means whole input stream from current position to the end is written. |
|
click |
Size of buffer used for copying. |
Public Member List: Serialization Support
|
|
void SetLoading()
Sets the stream into the loading mode.
void SetStoring()
Sets the stream into the storing mode.
bool IsLoading() const
Returns true if stream is in loading mode.
bool IsStoring() const
Returns true if stream is in storing mode.
void SerializeRaw(byte *data, int64 count)
void SerializeRaw(word *data, int64 count)
void SerializeRaw(int16 *data, int64 count)
void SerializeRaw(dword *data, int64 count)
void SerializeRaw(int *data, int64 count)
void SerializeRaw(uint64 *data, int64 count)
void SerializeRaw(float *data, int64 count)
void SerializeRaw(double *data, int64 count)
Serializes raw data. Might invoke LoadError if there is not enough data to load. Data are always stored in little-endian mode (conversion performed on BE systems as necessary).
void SerializeRLE(byte *data, int count)
Serializes raw data, using simple RLE compression.
Stream& operator%(bool& d)
Serializes bool variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(char& d)
Serializes char variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(signed char& d)
Serializes signed char variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(unsigned char& d)
Serializes unsigned char variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(short& d)
Serializes short variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(unsigned short& d)
Serializes unsigned short variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(int& d)
Serializes int variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(unsigned int& d)
Serializes unsigned int variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(long& d)
Serializes long variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(unsigned long& d)
Serializes unsigned long variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(float& d)
Serializes float variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(double& d)
Serializes double variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(int64& d)
Serializes int64 variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(uint64& d)
Serializes uint64 variable d. Might invoke LoadError if there is not enough data to load. Returns *this for chaining.
Stream& operator%(String& s)
Serializes String variable s. Might invoke LoadError if there is not enough data to load or input data are invalid. Returns *this for chaining.
Stream& operator/(String& s)
Serializes String variable s using RLE compression and packed format for length. Might invoke LoadError if there is not enough data to load or input data are invalid. Returns *this for chaining.
Stream& operator%(WString& s)
Serializes WString variable. Might invoke LoadError if there is not enough data to load or input data are invalid. Returns *this for chaining.
Stream& operator/(WString& s)
Serializes String variable using RLE compression and packed format for length. Might invoke LoadError if there is not enough data to load or input data are invalid. Returns *this for chaining.
void Pack(dword& i)
Serializes dword value using format optimized for storing small values. Values 0..254 are stored as serializes as single byte, other values result in 5 bytes. Might invoke LoadError if there is not enough data to load or input data are invalid.
Stream& operator/(int& i)
Serializes int value using format optimized for storing small values. Might invoke LoadError if there is not enough data to load or input data are invalid.
|
Return value |
*this for chaining. |
Stream& operator/(unsigned int& i)
Serializes unsigned int value using format optimized for storing small values. Might invoke LoadError if there is not enough data to load or input data are invalid.
|
Return value |
*this for chaining. |
void Magic(dword magic = 0x7d674d7b)
Serializes "magic value" to ensure stream integrity. When loading, this value is loaded and checked - mismatch results in invoking LoadError.
void Pack(bool& a, bool& b, bool& c, bool& d, bool& e, bool& f, bool& g, bool& h)
Serializes a set of boolean values compressed into single byte.
void Pack(bool& a, bool& b, bool& c, bool& d, bool& e, bool& f, bool& g)
Serializes a set of boolean values compressed into single byte.
void Pack(bool& a, bool& b, bool& c, bool& d, bool& e, bool& f)
Serializes a set of boolean values compressed into single byte.
void Pack(bool& a, bool& b, bool& c, bool& d, bool& e)
Serializes a set of boolean values compressed into single byte.
void Pack(bool& a, bool& b, bool& c, bool& d)
Serializes a set of boolean values compressed into single byte.
void Pack(bool& a, bool& b, bool& c)
Serializes a set of boolean values compressed into single byte.
void Pack(bool& a, bool& b)
Serializes a set of boolean values compressed into single byte.
class StringStream : public Stream
StringStream is stream that uses String as storage medium. The maximum output size is limited to 2GB (maximum String size limit).
Derived from Stream
StringStream()
Consructs empty stream and sets it into Storing serialization mode (like Create).
StringStream(const String& data)
Constructs stream with specified content and sets it into Loading serialization mode (like Open).
Public Member List: Serialization Support
|
|
void Open(const String& data)
Sets the content of stream to specified String and sets it into the Loading serialization mode.
void Limit(int sz)
Sets the output size limit. Exception StringStream::LimitExc is thrown if this the output size reaches this limit.
void Create()
Creates empty StringStream and sets it into Storing serialization mode.
void Reserve(int n)
Reserves additional n bytes of internal storage as optimization hint. This might speedup the operation in certain cases if you know how much data approximately will be put into StringStream.
String GetResult()
Returns resulting String.
operator String()
class MemStream : public Stream
Stream that is using raw memory as its content.
Derived from Stream
MemStream(void *data, int size)
Constructs MemStream at specified memory buffer.
|
data |
Pointer to the stream content. |
class MemReadStream : public MemStream
Read-only stream using raw memory as content.
Derived from MemStream
MemReadStream(const void *data, int size)
Constructs MemStream at specified memory buffer.
|
data |
Pointer to the stream content. |
class BlockStream : public Stream
BlockStream implements operations needed to manage streams that are able to read or write a block of data at random positon. BlockStream provides buffered implementation of such stream. It implements all virtual methods of Stream, with exception of IsOpen and Close, using new virtual methods Read, Write and SetStreamSize.
Derived from BufferStream
enum { READ, CREATE, APPEND, READWRITE, NOWRITESHARE, DELETESHARE, NOREADSHARE, SHAREMASK }
This enum defines basic operation modes of BlockStream (used combined with binary or).
|
APPEND |
Append mode - means that initial position in the stream is at the end of it. |
|
READWRITE |
Enables subsequent streams full access to stream. |
|
NOWRITESHARE |
Disables subsequent streams to write to the stream. |
|
DELETESHARE |
Enables subsequent streams to delete the stream. |
|
NOREADSHARE |
Disables subsequent streams to read the stream. |
void SetBufferSize(dword newsize)
Sets a new size of internal buffer.
|
newsize |
The new size of buffer. |
dword GetBufferSize() const
Return value Size of buffer.
virtual dword Read(int64 at, void *ptr, dword size)
Implementation of this virtual method in derived class should read a block of data at specified position in media.
|
ptr |
Pointer to buffer to receive data. |
|
size |
Requested size of data. |
|
Return value |
Size of data read. |
virtual void Write(int64 at, const void *data, dword size)
Implementation of this virtual method in derived method should write a block of data at specified position in media.
virtual void SetStreamSize(int64 size)
Implementation of this virtual method in derived class should adjust the size of media.
|
size |
Requested new size of media. |
int64 GetStreamSize() const
Returns current media size. Note that this might be different from current GetSize() - media size adjustment can be deffered to flushing the buffer.
|
Return value |
Current media size. |
void OpenInit(dword mode, int64 file_size)
Initializes the BlockStream to specified mode and actual media size.
|
file_size |
Actual media size. |
class FileStream : public BlockStream
Classical file stream.
Derived from BlockStream
FileStream(const char *filename, dword mode)
Opens file stream in specified mode (as defined in BlockStream).
|
filename |
The name of the file. |
FileStream(const char *filename, dword mode, mode_t acm = 0644)
Posix specific.
Opens file stream in specified mode (as defined in BlockStream) and specific POSIX access rights.
|
filename |
The name of the file. |
FileStream(int std_handle)
Posix specific.
Assigns existing file handle to FileStream.
|
std_handle |
File handle of open file. FileStream takes ownership of this handle. |
FileStream()
Creates empty unopened FileStream.
operator bool() const
Return value True if stream is open.
FileTime GetTime() const
Returns last-write time of stream.
|
Return value |
FileTime structure. |
void SetTime(const FileTime& tm)
Sets the last modification time of stream.
bool Open(const char *filename, dword mode)
Posix specific.
Opens file stream in specified mode (as defined in BlockStream) and specific POSIX access rights. mode can be one of READ, CREATE, APPEND, READWRITE possibly combined (using '|') with flag NOWRITESHARE. This enforces exclusive write access to the file.
|
filename |
The name of the file. |
bool Open(const char *filename, dword mode, mode_t acm = 0644)
Posix specific.
Opens file stream in specified mode (as defined in BlockStream) and specific POSIX access rights. mode can be one of READ, CREATE, APPEND, READWRITE possibly combined (using '|') with flag NOWRITESHARE. This flag uses flock system call to enforce exclusive write access to the file. In READ, if file does not exist, function fails. In CREATE mode, files is always created empty. In APPEND and READWRITE modes, file is created if it does not already exist.
|
filename |
The name of the file. |
HANDLE GetHandle() const
Return value File handle, either POSIX or WIN32.
class FileIn : public FileStream
Simple helper class that represents FileStream in read mode.
Derived from FileStream
FileIn(const char *fn)
Opens file for reading.
FileIn()
Constructs empty FileStream.
bool Open(const char *fn)
Opens file for reading.
|
Return value |
True if open was successful. |
class FileOut : public FileStream
Simple helper class that represents FileStream in write mode.
Derived from FileStream
FileOut(const char *fn)
Opens file for writing.
FileOut()
Constructs non-opened FileStream.
bool Open(const char *fn)
Opens file for writing.
|
Return value |
True if open was successful. |
bool Open(const char *fn, mode_t acm = 0644)
Opens file for writing.
POSIX specific
Return value True if open was successful.
class FileAppend : public FileStream
Simple helper class that represents FileStream in append mode - that in fact means in write mode with current position at the end of the file.
Derived from FileStream
FileAppend(const char *fn)
Opens file in append mode.
FileAppend()
Constructs empty FileStream.
bool Open(const char *fn)
Opens file in append mode.
|
Return value |
true when Open was successful. |
class SizeStream : public Stream
Special output stream that in fact does not store output data, only counts the total number of bytes written.
Derived from BufferStream
SizeStream()
Default constructor.
void Open()
Reopens data - resets the counter of output bytes.
operator int64() const
Returns current number of bytes written.
class CompareStream : public Stream
Special output stream that instead of storing data performs their comparison to the data of another stream.
Derived from BufferStream
CompareStream()
Constructs closed CompareStream.
CompareStream(Stream& aStream)
Constructors CompareStream opened for comparison with specified stream.
|
aStream |
Stream to compare with. |
void Open(Stream& aStream)
Opens CompareStream for comparison with the specified stream.
|
aStream |
Stream to compare with. |
bool IsEqual()
|
Return value |
true if all bytes written so far match those in comparison stream. |
operator bool()
|