Class Name: DyBytes
Documentation: All Member Functions



getByte
setByte
size
clear
removeFront

append
appendByte
appendString
insert
insertByte
insertString

appendVariable
insertVariable
extractVariable
getAsString
crc32
compress
decompress
getErrMsg

The DyBytes class is a double-ended queue of bytes, and stores data to be sent or received from the network. DyBytes has built-in compression and CRC functions, and can safely handle binary data.

Example:

DyBytes dtest;
dtest.appendString("Hello World");
cout << dtest << " CRC: " << dtest.crc32() << endl;

DyBytes member functions:


unsigned char getByte(long int index);

Retrieve the byte at the specified index.


void setByte(long int index, unsigned char newByte);

Changes the byte at the specified index to the new value.


long int size(void);

Returns the number of bytes being stored.


void clear(void);

Clears all data within the object. The size is set to zero.


void removeFront(long int numRemovals);

Deletes numRemovals elements from the front of the data. If the data size was less than numRemovals, the object is completely cleared.


void append(DyBytes source);
bool append(DyBytes source, long int index, long int length);

void appendByte(unsigned char source);
void appendString(string source);

Append source to the end of the data. The second one will only copy length bytes starting at position index, and returns false if index is out of bounds.


void insert(DyBytes source);
bool insert(DyBytes source, long int index, long int length);

void insertByte(unsigned char source);
void insertString(string source);

Insert source at the beginning of the data. The second one will only copy length bytes starting at position index, and returns false if index is out of bounds.


template <class T>
unsigned int appendVariable(T theVariable);

Converts theVariable into a string of bytes, and appends them to the end of the data. The return value is the number of bytes that were added. (This will be equal to the size of theVariable). You can use sizeof() to determine variable lengths.

I recommend using fixed-width integers:

The 'standard' int, long int, etc. have different sizes on machines with different architectures. For example, int is usually four bytes on a 32-bit system, but eight bytes on a 64-bit system. If you use int32_t, you're guaranteed to get an integer that's four bytes long.

All values are stored in big-endian format.


template <class T>
unsigned int insertVariable(T theVariable);

Similar to appendVariable, but it inserts theVariable at the beginning of the data.


template <class T>
void extractVariable(long int index, T &theVariable);

Copies binary data from the DyBytes object, and uses it to assign a value to theVariable. It copies the first byte from position index, and continues until enough bytes to fill theVariable have been copied. So if theVariable is 4 bytes long, and index is 10, then the bytes from positions 10, 11, 12 and 13 will be used to fill theVariable. You can use the sizeof() function to determine variable sizes:

DyBytes theData;
int32_t myNumber = 12345;
int32_t numberTest = 0;

cout << "Number of bytes in myNumber: " << sizeof(myNumber) << endl;
theData.appendVariable(myNumber);
theData.extractVariable(0, numberTest);
cout << "Extracted variable: " << numberTest << endl;


string getAsString(void);

Return the entire contents of the DyBytes object as a string. If the object contains any binary data, it won't be correctly retrieved. In this case, use getByte instead.


uint32_t crc32(void);

Returns a 32-bit CRC hash for the data in this object, using the 'fast' CRC algorithm. This is a fast way to verify the data's integrity. Note: crc32 must be called after startup, or you'll get incorrect results.


bool compress(int compressionLevel);

Compresses the data currently stored in the DyBytes object. The original data is gone; the object now contains compressed binary data. The compression level should be between 0 (no compression) and 9 (maximum compression). The compression is done with zlib.

Returns false if compression errors occurred, and sets errMsg appropriately.


bool decompress(void);

Decompresses the data within the object. If the DyBytes object contained a single valid, compressed file, it should now contain the uncompressed version. Returns false if errors occurred, and sets errMsg appropriately.


string getErrMsg(void);

Return a description of the last compression or decompression error for this DyBytes.











Back to Contents