Hi,
I want to reimplement the QIODevice's read
qint64
QIODevice::read ( char * data, qint64 maxSize
)
qint64 QIODevice::read ( char * data, qint64 maxSize )
To copy to clipboard, switch view to plain text mode
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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?
Bookmarks