Here is the BinaryWriter. I did write the tests first, then wrote the documentation and finally the implementation. OK, I actually reused a lot from BinaryReader. Just made copies of BinaryReader’s files and first modified the tests. Writing tests is really great because it does force you to think about how you need to use the class. Writing documentation forces you to express yourself in a more informal way, which makes you think again about some of the details. I admit that the BinaryWriter task was easier because I already had some very similar experience with BinaryReader. Anyway, writing the tests first helped me to figure out what are the differences between the two classes.
In BinaryWriter byte order conversions are performed just as in BinaryReader. However, BinaryWriter does not have write origin but instead has an option to write into a growable buffer (one that is extended as new data is written) or a fixed-size (non-growable) buffer. Using fixed-size buffers would make writing more efficient as it would not be necessary to resize the underlying QByteArray before any write operation. And in some cases the size of the data that will be written will not be known.
Instead of hasReadAll(), BinaryWriter has a member function bytesWritten() which returns the number of bytes that were requested to be written. Not all of them may have actually been written if working with non-growable buffer, in which case hasWrittenPastEnd() would return true.
Attached is the patch containing the new files. You can also get it from my BitTorrent branch: http://bzr.calitko.org/developers/peter/calitko-BitTorrent . The revno is 71.
I’m not committing anything of this to mainstream yet. I’d like to wait for some feedback from you guys!
Regards,
Peter
