PDA

View Full Version : QIODevice read()



ShaChris23
3rd May 2007, 01:16
Hi,

I want to reimplement the QIODevice's read



qint64 QIODevice::read ( char * data, qint64 maxSize )


as follows:

- I want the new Read function to block until it has filled nBytes that I want to read.

Would this function do?



bool Read( char* address,
int numBytesToBeRead )
{
do
{
// If waitForReadyRead returns false, there is something wrong
if( mDevice->waitForReadyRead( -1 ) == false )
{
return false;
}
} while( mDevice->bytesAvailable() != numBytesToRead );

// Now that there are numBytesToBeRead available, read it all at once
mDevice->read( address, numBytesToBeRead );

return true;
}


A few concerns:

1) Once there is at least 1 byte to be read in the "pipe," would waitForReadyRead() constantly return? i.e. it wont block anymore since there are bytes to be read even if it's not up to the number of bytes I want to read yet.

2) Can it be that the "pipe" would eventually be full and that bytesAvailable() will never be equal to numBytesToRead? i.e. if numBytesToRead is 1 GB...maybe if the program doesnt do a read() at all, bytesAvailable will never be equal to 1GB.

I simply just want an API that's like the POSIX TCP socket where the read() blocks forever waiting for X amount of bytes to come through. Is there something similar in Qt?

ShaChris23
3rd May 2007, 01:29
Oh..or do I have to use this method in conjunction?

setReadBufferSize( 0 );

Excerpt from Qt reference manual:
Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.