When does NSInputStream's -getBuffer:length: actually work?

  • NSInputStream has a -getBuffer:length: method that lets you get the available data from the stream without copying. This is great for performance, and I've written my client code to take advantage of it if it's supported, but every time I've tested, it isn't supported (i.e. just returns NO) so my code just falls back to calling -read:maxLength: instead.

    Does anyone know in what circumstances, or in what types of streams, this method actually works?

    —Jens
  • On 25 Jul 2012, at 18:08, Jens Alfke wrote:

    > NSInputStream has a -getBuffer:length: method that lets you get the available data from the stream without copying. This is great for performance, and I've written my client code to take advantage of it if it's supported, but every time I've tested, it isn't supported (i.e. just returns NO) so my code just falls back to calling -read:maxLength: instead.
    >
    > Does anyone know in what circumstances, or in what types of streams, this method actually works?

    I don't know for sure, but I would bet it's streams reading from files, which have become memory mapped.

    Bob
  • On 25 Jul 2012, at 10:08 AM, Jens Alfke wrote:
    > NSInputStream has a -getBuffer:length: method that lets you get the available data from the stream without copying. This is great for performance, and I've written my client code to take advantage of it if it's supported, but every time I've tested, it isn't supported (i.e. just returns NO) so my code just falls back to calling -read:maxLength: instead.
    >
    > Does anyone know in what circumstances, or in what types of streams, this method actually works?

    It sort of works for streams reading from a memory buffer, IIRC. By "sort of", I mean it sometimes returns data and sometimes silently discards data. I now avoid NSStream/CFStream unless I'm dealing with an API that requires them--- they seem to be an unmaintained corner of the codebase.
  • On 25.07.2012, at 19:08, Jens Alfke wrote:

    > NSInputStream has a -getBuffer:length: method that lets you get the available data from the stream without copying. This is great for performance, and I've written my client code to take advantage of it if it's supported, but every time I've tested, it isn't supported (i.e. just returns NO) so my code just falls back to calling -read:maxLength: instead.
    >
    > Does anyone know in what circumstances, or in what types of streams, this method actually works?

    I could imagine where this can NOT work:

    In cases where the buffer is shared for input and output. Since the two streams could possibly execute on different threads, returning the internal buffer in the input stream's thread while the output stream is still executing will likely result in a race condition of the buffers content.

    So, I would guess CFStreamCreateBoundPair and possibly all CFStreamCreatePairWith* won't work.

    Andreas
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today