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


This class encapsulates some functions, which would make it easier to write binary data out in raw data buffers. All functions do byte order conversions when necessary.
If you created an object with auto-gorwable buffer and a write function would have resulted in writing past the end of the buffer, then the buffer() would be automatically resized to fit the new data. Trying to write past the end of a non auto-growable buffer will silently fail. Use hasWrittenPastEnd() to check whether that happened. Use bytesWritten() to find out how many bytes have already been written and hasWrittenAll() to check whether the underlying buffer was completely written.
The function buffer() will return a copy of the given buffer. The buffer can always be manually grown to guarantee that some number of bytes can by be written by calling reserve().
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.
Definition at line 50 of file BinaryWriter.h.
Public Types | |
| enum | ByteOrder { BigEndian = 0, LittleEndian = 1 } |
Public Member Functions | |
| BinaryWriter (ByteOrder, bool autoGrow=true) | |
| Constructs a BinaryWriter object to write in an internal growable buffer. | |
| BinaryWriter (QByteArray *buffer, ByteOrder, bool autoGrow=false) | |
| Constructs a BinaryWriter object to write in buffer. | |
| ~BinaryWriter () | |
| Destructor of BinaryWriter. | |
| void | reserve (size_t bytesCount) |
| Grows the underlying buffer to make room for count more bytes. | |
| void | writeByte (uchar) |
| Writes a single byte to the underlying raw data buffer. | |
| void | writeUInt16 (quint16) |
| Writes a two byte unsigned integer to the underlying raw data buffer. | |
| void | writeUInt32 (quint32) |
| Writes a four byte unsigned integer to the underlying raw data buffer. | |
| void | writeUInt64 (quint64) |
| Writes eight byte unsigned integer to the underlying raw data buffer. | |
| void | writeInt16 (qint16) |
| void | writeInt32 (qint32) |
| void | writeInt64 (qint64) |
| void | writeBytes (const QByteArray &) |
| Writes count bytes to the underlying raw data buffer. | |
| void | writeString (const QByteArray &, char terminator= '\0') |
| Writes a terminator terminated string to the underlying raw data buffer. | |
| ByteOrder | byteOrder () const |
| Returns the currently set ByteOrder. | |
| void | setByteOrder (ByteOrder) |
| Sets the byte order of the used in the underlying raw data buffer. | |
| bool | hasWrittenAll () const |
| Tells whether the underlying buffer is completely written. | |
| bool | hasWrittenPastEnd () const |
| Tells whether a write would have read past the end of the data buffer. | |
| size_t | bytesWritten () const |
| Returns the number of bytes that the client requested to write. | |
| QByteArray | buffer () const |
| Returns the underlying raw data buffer. | |
Private Member Functions | |
| BinaryWriter (const BinaryWriter &) | |
| BinaryWriter & | operator= (const BinaryWriter &) |
| void | growBuffer (size_t count, bool force=false) |
| Grows a growable underlying buffer to make room for count more bytes. | |
| void | doWrite (char *destination, size_t size) |
| Writes count bytes to the underlying raw buffer from source. | |
Private Attributes | |
| Data | d |
Classes | |
| class | Data |
|
|
Definition at line 55 of file BinaryWriter.h. |
|
|
|
|
||||||||||||
|
Constructs a BinaryWriter object to write in an internal growable buffer. Initializes a BinaryWriter object to write raw data into an internal buffer. The byte order is set to the value of byteOrder. The argument autoGrow controlls whether the buffer should be automatically resized to fit new data being written to it.
Definition at line 61 of file BinaryWriter.cpp. |
|
||||||||||||||||
|
Constructs a BinaryWriter object to write in buffer. Initializes a BinaryWriter object to write raw data into the given buffer. The byte order is set to the value of byteOrder. The argument autoGrow controlls whether the buffer should be automatically resized to fit new data being written to it.
Definition at line 39 of file BinaryWriter.cpp. |
|
|
Destructor of BinaryWriter. If a BinaryWriter object was created that uses an internal growable buffer, this buffer will be freed here. Definition at line 78 of file BinaryWriter.cpp. |
|
|
Returns the underlying raw data buffer.
Definition at line 122 of file BinaryWriter.h. |
|
|
Returns the currently set ByteOrder.
Definition at line 118 of file BinaryWriter.h. |
|
|
Returns the number of bytes that the client requested to write. This function returns the number of bytes that the client requested to write, including the bytes that would have been written beyond the end of the underlying buffer. If you can to know how many bytes were actually written to the underlying buffer use buffer().length().
Definition at line 303 of file BinaryWriter.cpp. |
|
||||||||||||
|
Writes count bytes to the underlying raw buffer from source. This function performs a safe write to the underlying buffer. It will not write beyond the end of the buffer and if it would have done so, then hasWrittenPastEnd() would return true. For an object working with a growable buffer the underlying buffer will be grown as required. Byte ordering conversions are also performed if required.
Definition at line 132 of file BinaryWriter.cpp. |
|
||||||||||||
|
Grows a growable underlying buffer to make room for count more bytes. Nothing is done for a non-growable buffer unless force is true. The underlying buffer is expanded at most count bytes to make sure the count bytes can be written. Definition at line 110 of file BinaryWriter.cpp. |
|
|
Tells whether the underlying buffer is completely written. If the user of this object wrote exactly as much bytes as were available in the underlying buffer, then true is returned. If an auto-growable buffer was created and it has never been manually grown, that this function would return true after each write call.
Definition at line 274 of file BinaryWriter.cpp. |
|
|
Tells whether a write would have read past the end of the data buffer. If the user of the object tried to write more bytes than were actually available in the underlying raw data buffer, this function returns true and false otherwise. If an auto-growable buffer was created then this function will always return false as the buffer will automatically be resized to fit all new data.
Definition at line 289 of file BinaryWriter.cpp. |
|
|
|
|
|
Grows the underlying buffer to make room for count more bytes. No matter whether an auto-growable or non auto-growable buffer was created, this function would make sure count more bytes can be written.
Definition at line 97 of file BinaryWriter.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 254 of file BinaryWriter.cpp. |
|
|
Writes a single byte to the underlying raw data buffer. This function performs a safe write to the underlying buffer. It will not write beyond the end of a non-growable buffer and if it would have done so, then hasReadPastEnd() would return true. A growable buffer will be grown to fit the new data. Byte ordering conversions are also performed if required.
Definition at line 155 of file BinaryWriter.cpp. |
|
|
Writes count bytes to the underlying raw data buffer. This function performs a safe write to the underlying buffer. It will not write beyond the end of a non-growable buffer and if it would have done so, then hasReadPastEnd() would return true. A growable buffer will be grown to fit the new data.
Definition at line 213 of file BinaryWriter.cpp. |
|
|
Definition at line 126 of file BinaryWriter.h. |
|
|
Definition at line 130 of file BinaryWriter.h. |
|
|
Definition at line 134 of file BinaryWriter.h. |
|
||||||||||||
|
Writes a terminator terminated string to the underlying raw data buffer. Writes the bytes of string and appends the terminating terminator. It is not verified whether the string contains the terminator character and it is appended in any case! Nothing is written to the underlying buffer if the that would have resulted in writing past the end of a non-growable buffer. The start write position is advanced though. A growable buffer will be grown as needed. Use hasReadPastEnd() to figure out whether all the data could fit.
Definition at line 236 of file BinaryWriter.cpp. |
|
|
Writes a two byte unsigned integer to the underlying raw data buffer. This function performs a safe write to the underlying buffer. It will not write beyond the end of a non-growable buffer and if it would have done so, then hasReadPastEnd() would return true. A growable buffer will be grown to fit the new data. Byte ordering conversions are also performed if required.
Definition at line 169 of file BinaryWriter.cpp. |
|
|
Writes a four byte unsigned integer to the underlying raw data buffer. This function performs a safe write to the underlying buffer. It will not write beyond the end of a non-growable buffer and if it would have done so, then hasReadPastEnd() would return true. A growable buffer will be grown to fit the new data. Byte ordering conversions are also performed if required.
Definition at line 183 of file BinaryWriter.cpp. |
|
|
Writes eight byte unsigned integer to the underlying raw data buffer. This function performs a safe write to the underlying buffer. It will not write beyond the end of a non-growable buffer and if it would have done so, then hasReadPastEnd() would return true. A growable buffer will be grown to fit the new data. Byte ordering conversions are also performed if required.
Definition at line 197 of file BinaryWriter.cpp. |
|
|
Definition at line 111 of file BinaryWriter.h. |