Why you should use -availableData rather than -readDataToEndOfFile

2006-10-08 23:27:25 UTC

(All of this only applies to regular files, not to “communications channels”, which I interpret to mean sockets and pipes. Also, this only matters on a 64-bit Mac; on a 32-bit system, you will run out of memory long before seeing any difference between these two methods.)

-[NSFileHandle availableData]:
“If the receiver is a file, returns the data obtained by reading the file from the file pointer to the end of the file.”
-[NSFileHandle readDataToEndOfFile]:
“The data available through the receiver up to UINT_MAX bytes (the maximum value for unsigned integers) …”

In other words, if there are more than UINT_MAX bytes in the file, -availableData will return all of them, whereas -readDataToEndOfFile will only return the first UINT_MAX of them.

One Response to “Why you should use -availableData rather than -readDataToEndOfFile”

  1. Colin Barrett Says:

    Note that this assumes the documentation to tell the truth, the whole truth, and nothing but the truth. ;)

Leave a Reply

Do not delete the second sentence.