Utils::Encodings::BinaryWriter Class Reference

#include <BinaryWriter.h> [code]

Inherited by Protocols::Gnutella::Packets::BinaryWriter.

Inheritance diagram for Utils::Encodings::BinaryWriter:

Inheritance graph
[legend]
Collaboration diagram for Utils::Encodings::BinaryWriter:

Collaboration graph
[legend]
List of all members.

Detailed Description

Writes binary data to a raw buffer (QByteArray).

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 &)
BinaryWriteroperator= (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


Member Enumeration Documentation

enum Utils::Encodings::BinaryWriter::ByteOrder
 

Enumerator:
BigEndian 
LittleEndian 

Definition at line 55 of file BinaryWriter.h.


Constructor & Destructor Documentation

Utils::Encodings::BinaryWriter::BinaryWriter const BinaryWriter  )  [private]
 

BinaryWriter::BinaryWriter ByteOrder  byteOrder,
bool  autoGrow = true
 

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.

See also:
hasReadPastEnd(), buffer(), reserve()

Definition at line 61 of file BinaryWriter.cpp.

BinaryWriter::BinaryWriter QByteArray *  buffer,
ByteOrder  byteOrder,
bool  autoGrow = false
 

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.

See also:
hasReadPastEnd(), buffer(), reserve()

Definition at line 39 of file BinaryWriter.cpp.

BinaryWriter::~BinaryWriter  ) 
 

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.


Member Function Documentation

QByteArray Utils::Encodings::BinaryWriter::buffer  )  const [inline]
 

Returns the underlying raw data buffer.

Definition at line 122 of file BinaryWriter.h.

BinaryWriter::ByteOrder Utils::Encodings::BinaryWriter::byteOrder  )  const [inline]
 

Returns the currently set ByteOrder.

See also:
setByteOrder()

Definition at line 118 of file BinaryWriter.h.

size_t BinaryWriter::bytesWritten  )  const
 

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().

See also:
hasWrittenAll(), hasWrittenPastEnd()

Definition at line 303 of file BinaryWriter.cpp.

void BinaryWriter::doWrite char *  source,
size_t  count
[inline, private]
 

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.

See also:
setByteOrder()

Definition at line 132 of file BinaryWriter.cpp.

void BinaryWriter::growBuffer size_t  count,
bool  force = false
[inline, private]
 

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.

bool BinaryWriter::hasWrittenAll  )  const
 

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.

See also:
hasWrittenPastEnd(), bytesWritten()

Definition at line 274 of file BinaryWriter.cpp.

bool BinaryWriter::hasWrittenPastEnd  )  const
 

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.

See also:
hasWrittenAll(), bytesWritten()

Definition at line 289 of file BinaryWriter.cpp.

BinaryWriter& Utils::Encodings::BinaryWriter::operator= const BinaryWriter  )  [private]
 

void BinaryWriter::reserve size_t  count  ) 
 

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.

Note:
An auto-grow buffer will be turned into a non auto-grow buffer after this call. Typically we create auto-growable writers to which we output our messages. The functions Ggep::writeData() or Packet::writePayload() could then reserve e.g. 10 bytes if they know that is the fixed length of the data. Then hasWrittenAll() would reveal if we have written less than 10 bytes. Making the buffer non auto-grow makes it possible to reveal whether we wrote 11 bytes by calling hasWrittenPastEnd().

Definition at line 97 of file BinaryWriter.cpp.

void BinaryWriter::setByteOrder ByteOrder  byteOrder  ) 
 

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.

void BinaryWriter::writeByte uchar  value  ) 
 

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.

See also:
setByteOrder(), hasReadPastEnd()

Definition at line 155 of file BinaryWriter.cpp.

void BinaryWriter::writeBytes const QByteArray &  bytes  ) 
 

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.

Note:
NO Byte ordering conversions are performed.
See also:
hasWrittenPastEnd()

Definition at line 213 of file BinaryWriter.cpp.

void Utils::Encodings::BinaryWriter::writeInt16 qint16  value  )  [inline]
 

See also:
writeUInt16()

Definition at line 126 of file BinaryWriter.h.

void Utils::Encodings::BinaryWriter::writeInt32 qint32  value  )  [inline]
 

See also:
writeUInt32()

Definition at line 130 of file BinaryWriter.h.

void Utils::Encodings::BinaryWriter::writeInt64 qint64  value  )  [inline]
 

See also:
writeUInt64()

Definition at line 134 of file BinaryWriter.h.

void BinaryWriter::writeString const QByteArray &  string,
char  terminator = '\0'
 

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.

See also:
hasReadPastEnd()

Definition at line 236 of file BinaryWriter.cpp.

void BinaryWriter::writeUInt16 quint16  value  ) 
 

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.

See also:
setByteOrder(), hasReadPastEnd()

Definition at line 169 of file BinaryWriter.cpp.

void BinaryWriter::writeUInt32 quint32  value  ) 
 

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.

See also:
setByteOrder(), hasReadPastEnd()

Definition at line 183 of file BinaryWriter.cpp.

void BinaryWriter::writeUInt64 quint64  value  ) 
 

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.

See also:
setByteOrder(), hasReadPastEnd()

Definition at line 197 of file BinaryWriter.cpp.


Member Data Documentation

Data Utils::Encodings::BinaryWriter::d [private]
 

Definition at line 111 of file BinaryWriter.h.


The documentation for this class was generated from the following files: