Read Buffer Optimal Solution

Howdy People,
I wanted to know if anyone has a clever or more optimal solution to a simple read buffer problem.

using namespace std ;

class BaseIO {
	protected :
		int read4k ( char * buf );
};

class ArbitraryIO : public BaseIO {
	private :
		int read ( int readBytes, char * buf) ;
		ArbitraryIO() = default ;
};

We need to implement ArbitraryIO's read function that returns the number of bytes read and taking in the number of bytes requested and a pointer to a buffer we need to fill. We can only implement this read function by calling read4k that reads 4k bytes at a time, returns the number of bytes read and takes in a pointer to a buffer that is filled by the function. Here is my solution, let me know what you think :

int ArbitraryIO::read ( int readBytes , char * buf ) {
	char * tempOutputBuffer = new char [ readBytes ];
	char * read4kBytes ;
	int bytesIterToRead = ceil ( (double)readBytes / 4000.0);
	int totalBytesRead = 0;
	int totalBytesNeeded = readBytes;

	for ( int i = 0 ; i < bytesIterToRead ; ++ i) {
		int bytesRead = read4k ( read4kBytes );
		totalBytesRead += bytesRead;
		totalBytesNeeded -= bytesRead;
		if ( totalBytesNeeded < 0 ) {
			memcpy( tempOutputBuffer + i * 4000, read4kBytes, bytesRead + totalBytesNeeded) ;
			buf = tempOutputBuffer ;
			return readBytes ;
		}
		else if ( bytesRead < 4000 and totalBytesNeeded > 0) {
			memcpy ( tempOutputBuffer + i * 4000, read4kBytes, bytesRead);
			buf = tempOutputBuffer ;
			return totalBytesRead ;
		}
		memcpy ( tempOutputBuffer+i*4000,read4kBytes,bytesRead );
	}
	buf = tempOutputBuffer;
	return totalBytesRead ;
}
Comments (1)