QFile::read(char *data, qint64 maxSize) return value
-
When reading file chunk-by-chunk and
QFile::read(char *data, qint64 maxSize)returns positive value that is less than maxSize, what exactly does this mean? It it safe to keep callingread()until it returns either -1 or 0 or does this mean that there was an error and I should abort reading?Documentation on virtual
QIODevice::readDatafunction that is used to implementQFilestates that "When reimplementing this function it is important that this function reads all the required data before returning." which implies that return value that is anything other than maxSize (and possibly 0 for end-of-file?) is an error condition. HoweverQIODevice::readdocumentation doesn't mention this.Can I rely on
read()doing the job of reading maxSize of bytes (excepting errors and end of file) or should I implement this logic myself? -
When reading file chunk-by-chunk and
QFile::read(char *data, qint64 maxSize)returns positive value that is less than maxSize, what exactly does this mean? It it safe to keep callingread()until it returns either -1 or 0 or does this mean that there was an error and I should abort reading?Documentation on virtual
QIODevice::readDatafunction that is used to implementQFilestates that "When reimplementing this function it is important that this function reads all the required data before returning." which implies that return value that is anything other than maxSize (and possibly 0 for end-of-file?) is an error condition. HoweverQIODevice::readdocumentation doesn't mention this.Can I rely on
read()doing the job of reading maxSize of bytes (excepting errors and end of file) or should I implement this logic myself?Reads at most maxSize bytes from the device into data...
(emphasis mine)So the answer to your question is "yes," read will read as much data as:
- is available
- is <= your maxSize value
Note, however, that if this is a device that provides information in chunks (like the old spectrum analyzers), you may need to continue to read (with some timeout value set) until read() returns a 0.
-
Reads at most maxSize bytes from the device into data...
(emphasis mine)So the answer to your question is "yes," read will read as much data as:
- is available
- is <= your maxSize value
Note, however, that if this is a device that provides information in chunks (like the old spectrum analyzers), you may need to continue to read (with some timeout value set) until read() returns a 0.
@mzimmers Hmm. Usually it's a QFile constructed from local file path, but it also can be opened from a file descriptor returned from Android's
ContentResolver.openAssetFileDescriptor()(which could be just about anything, although it's usually either a file or a pipe that's written to dynamically on the other end).