#include <BinaryReader.h> [code]
Inherited by Protocols::Gnutella::Packets::BinaryReader.
Inheritance diagram for Utils::Encodings::BinaryReader:


This class encapsulates some functions, which would make it easier to read binary data out of raw data buffers. All functions do byte order conversions when necessary.
It is safe to read pass the end of the underlying raw data. Use hasReadPastEnd() to check whether such event occurred. Use hasReadAll() to check whether all underlying data has been read.
If you want to change the binary format used by some function or add support for further user types, derive a new class from this one.
Extract an interface class? Rename to BinaryReader base and make the dtor protected to avoid deleting using base-class pointer? Same for BinaryWriter.
Definition at line 47 of file BinaryReader.h.
Public Types | |
| enum | ByteOrder { BigEndian = 0, LittleEndian = 1 } |
| enum | ReadOrigin { ReadFromStart = 0, ReadFromEnd = 1 } |
Public Member Functions | |
| BinaryReader (const QByteArray &rawData, ByteOrder) | |
| Constructs a BinaryReader object for rawData. | |
| uchar | lookAhead (const int count) |
| Looks ahead in the buffer without changing the read pointers. | |
| bool | canRead (const int count) |
| Returns whether count bytes are available for reading. | |
| uchar | readByte () |
| Reads a single byte from the underlying raw data buffer. | |
| quint16 | readUInt16 () |
| Reads a two byte unsigned integer from the underlying raw data buffer. | |
| quint32 | readUInt32 () |
| Reads a four byte unsigned integer from the underlying raw data buffer. | |
| quint64 | readUInt64 () |
| Reads eight byte unsigned integer from the underlying raw data buffer. | |
| qint16 | readInt16 () |
| qint32 | readInt32 () |
| qint64 | readInt64 () |
| QByteArray | readAll () |
| Read all bytes still available in the underlying buffer. | |
| QByteArray | readBytes (size_t count) |
| Reads count bytes from the underlying raw data buffer. | |
| QByteArray | readString (char terminator= '\0') |
| Reads a terminator terminated string from the underlying raw data buffer. | |
| QByteArray | getSharedBytes (size_t count) |
| Returns a QByteArray that shares some part of the underlying raw data. | |
| ByteOrder | byteOrder () const |
| Returns the currently set ByteOrder. | |
| ReadOrigin | readOrigin () const |
| Returns the currently set ReadOrigin. | |
| void | setByteOrder (ByteOrder) |
| Sets the byte order of the used in the underlying raw data buffer. | |
| void | setReadOrigin (ReadOrigin) |
| Sets the read origin (from start to end or end to start or raw data buffer). | |
| bool | hasReadPastEnd () const |
| Tells whether a read would have read past the end of the data buffer. | |
| bool | hasReadAll () const |
| Tells whether all data from the underlying data buffer has been read. | |
Private Member Functions | |
| BinaryReader (const BinaryReader &) | |
| BinaryReader & | operator= (const BinaryReader &) |
| void | doRead (char *destination, size_t count) |
| Reads size bytes from the underlying raw buffer into destination. | |
| void | bumpPtrs (size_t count) |
| Moves internal read pointers depending on current readOrigin(). | |
| void | copyBytes (const char *beg, const char *end, char *dest) |
| Copies (possibly swapped) bytes from end - beg bytes in dest. | |
| const char * | getRangePtrs (size_t count) |
| Returns pointer to the position from which to read count bytes. | |
Private Attributes | |
| Data | d |
Classes | |
| class | Data |
|
|
Definition at line 52 of file BinaryReader.h. |
|
|
Definition at line 58 of file BinaryReader.h. |
|
|
|
|
||||||||||||
|
Constructs a BinaryReader object for rawData. Initializes the underlying raw data buffer using rawData and sets default byte order using byteOrder. Definition at line 34 of file BinaryReader.cpp. |
|
|
Moves internal read pointers depending on current readOrigin(). Moves the start read position count bytes forward, or moves the end read position count backward depending on the current readOrigin(). Definition at line 100 of file BinaryReader.cpp. |
|
|
Returns the currently set ByteOrder.
Definition at line 125 of file BinaryReader.h. |
|
|
Returns whether count bytes are available for reading.
Definition at line 136 of file BinaryReader.cpp. |
|
||||||||||||||||
|
Copies (possibly swapped) bytes from end - beg bytes in dest. The function will copy the bytes and if required by the selected byteOrder() it will swap them while copying. Definition at line 87 of file BinaryReader.cpp. |
|
||||||||||||
|
Reads size bytes from the underlying raw buffer into destination. This function performs a safe read from the underlying buffer. It will not read beyond the end of the buffer and if it would have done so, then hasReadPastEnd() would return true. Byte ordering conversions are also performed if required.
Definition at line 54 of file BinaryReader.cpp. |
|
|
Returns pointer to the position from which to read count bytes. If the current readOrigin() is ReadFromEnd than the returned pointer can be used to read the last count bytes. Otherwise, a pointer to the first count bytes is returned.
Definition at line 73 of file BinaryReader.cpp. |
|
|
Returns a QByteArray that shares some part of the underlying raw data.
This function may be useful when reading nested data structures. For example you read a packet and encounter an extension which you know should be of size count bytes. If corrupt extension data could result in reading more than these count bytes, then a BinaryReader will both ease reading and verification of data. It may however be inefficient to copy data using readBytes() and getSharedBytes() is the solution. This function uses QByteArray::fromRawData() to create a QByteArray object that wraps around the underlying data of the BinaryReader without copying bytes. The called MUST ensure that this BinaryReader exists as long as any unmodified copy of the QByteArray returned by getSharedBytes() exists. Use this function with caution!
Definition at line 260 of file BinaryReader.cpp. |
|
|
Tells whether all data from the underlying data buffer has been read. This function returns true if the user of the object of class BinaryReader read all the data available in the underlying data buffer. If user tried to read past the end, then both hasReadAll() and hasReadPastEnd() will return true!
Definition at line 363 of file BinaryReader.cpp. |
|
|
Tells whether a read would have read past the end of the data buffer. If the user of the object tried to read more bytes than were actually available in the underlying raw data buffer, no matter from which readOridin(), this function returns true and false otherwise.
Definition at line 349 of file BinaryReader.cpp. |
|
|
Looks ahead in the buffer without changing the read pointers. This function returns a single byte which is count bytes away from the current read postition. If the requested byte is beyond the end of the raw data buffer a zero byte is returned. Call canRead() if you want to make sure that the returned byte is within the data.
Definition at line 118 of file BinaryReader.cpp. |
|
|
|
|
|
Read all bytes still available in the underlying buffer.
Definition at line 206 of file BinaryReader.cpp. |
|
|
Reads a single byte from the underlying raw data buffer. This function performs a safe read from the underlying buffer. It will not read beyond the end of the buffer and if it would have done so, then hasReadPastEnd() would return true. Byte ordering conversions are also performed if required.
Definition at line 150 of file BinaryReader.cpp. |
|
|
Reads count bytes from the underlying raw data buffer. This function performs a safe read from the underlying buffer. It will not read beyond the end of the buffer and if it would have done so, then hasReadPastEnd() would return true.
Definition at line 224 of file BinaryReader.cpp. |
|
|
Definition at line 136 of file BinaryReader.h. |
|
|
Definition at line 140 of file BinaryReader.h. |
|
|
Definition at line 144 of file BinaryReader.h. |
|
|
Returns the currently set ReadOrigin.
Definition at line 132 of file BinaryReader.h. |
|
|
Reads a terminator terminated string from the underlying raw data buffer. Reads bytes from the underlying data buffer until terminator is reached. The terminator is not appended to the returned QByteArray, but the internal start read position is set beyond that terminator byte. If the end of the data buffer is reached before the terminator byte is encountered, then a QByteArray containing the data read so far is returned and hasReadPastEnd() will return true. If readOrigin() is ReadFromEnd, then a terminator started string will be extracted from the end of the underlying data buffer. The terminator will not be appended to the returned QByteArray, but the end read position is set at the zero. The first byte in the returned QByteArray is the byte right after the starting terminator byte. Definition at line 290 of file BinaryReader.cpp. |
|
|
Reads a two byte unsigned integer from the underlying raw data buffer. This function performs a safe read from the underlying buffer. It will not read beyond the end of the buffer and if it would have done so, then hasReadPastEnd() would return true. Byte ordering conversions are also performed if required.
Definition at line 166 of file BinaryReader.cpp. |
|
|
Reads a four byte unsigned integer from the underlying raw data buffer. This function performs a safe read from the underlying buffer. It will not read beyond the end of the buffer and if it would have done so, then hasReadPastEnd() would return true. Byte ordering conversions are also performed if required.
Definition at line 182 of file BinaryReader.cpp. |
|
|
Reads eight byte unsigned integer from the underlying raw data buffer. This function performs a safe read from the underlying buffer. It will not read beyond the end of the buffer and if it would have done so, then hasReadPastEnd() would return true. Byte ordering conversions are also performed if required.
Definition at line 198 of file BinaryReader.cpp. |
|
|
Sets the byte order of the used in the underlying raw data buffer. The byteOrder determines whether any conversions should be made by the functions readUInt16(), readUInt32(), readUInt64(), etc. Definition at line 314 of file BinaryReader.cpp. |
|
|
Sets the read origin (from start to end or end to start or raw data buffer). If readOrigin is set to ReadFromStart, then data is read as normally - from the beginning of the raw data buffer. After a read the start position of the reader is incremented accordingly. If readOrigin is set to ReadFromEnd, then data is read off the end of the of the underlying raw data buffer. After a read the end position of the reader is decremented accordingly. Definition at line 335 of file BinaryReader.cpp. |
|
|
Definition at line 118 of file BinaryReader.h. |