Reputation: 5155
I'm trying to implement a double buffer for a real-time audio application, and QAudioInput
requires it to be a subclass of QIODevice
. I'm finding the documentation for this method pretty confusing.
First of all, the method signature in the documentation doesn't match the header for QT 5.9.2, which has virtual qint64 writeData(const char *data, qint64 len) = 0;
.
The documentation has this signature though: qint64 QIODevice::writeData(const char *data, qint64 maxSize)
The maxSize
parameter confuses me because it implies that I can just buffer some of the data, which the documentation also implies with:
Writes up to
maxSize
bytes from data to the device. Returns the number of bytes written, or-1
if an error occurred.
However, immediately afterword the documentation says this, which seems contradictory to me:
When reimplementing this function it is important that this function writes all the data available before returning. This is required in order for
QDataStream
to be able to operate on the class.QDataStream
assumes all the information was written and therefore does not retry writing if there was a problem.
So is my QIODevice
implementation responsible for buffering all the data in a single call or not?
Upvotes: 1
Views: 806
Reputation: 7146
What they basically trying to say is: The passed data
is maxSize
bytes long. Your implementation should write all data and return the number of bytes written.
It is possible to write less data then available, but you should not. If you do, some classes that use your device may not react to this (like QDataStream). It depends on how QAudioInput
handles write calls. If it checks the result and writes missing data again if not completly written, not writing all data is fine. If thats not the case, you have to always write all data.
Simply try it out: always write only 1 byte (and return 1). If it works, it's fine, if not you have to always write all passed data, or fail with -1.
Upvotes: 1