| Derived from | |
| Framework | CoreFoundation/CoreFoundation.h |
| Declared in | CFStream.h |
CFReadStream provides an interface for reading a byte stream either synchronously or asynchronously. You can create streams that read bytes from a block of memory, a file, or a generic socket. All streams need to be opened, using CFReadStreamOpen, before reading.
Use CFWriteStream for writing byte streams. The CFNetwork framework defines an additional type of stream for reading responses to HTTP requests.
CFReadStream is “toll-free bridged” with its Cocoa Foundation counterpart, NSInputStream. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSInputStream * parameter, you can pass in a CFReadStreamRef, and in a function where you see a CFReadStreamRef parameter, you can pass in an NSInputStream instance. Note, however, that you may have either a delegate or callbacks but not both. See Interchangeable Data Types for more information on toll-free bridging.
CFReadStreamCopyProperty
CFReadStreamGetBuffer
CFReadStreamCopyError
CFReadStreamGetError
CFReadStreamGetStatus
CFReadStreamHasBytesAvailable
Closes a readable stream.
void CFReadStreamClose ( CFReadStreamRef stream );
The stream to close.
This function terminates the flow of bytes and releases any system resources required by the stream. The stream is removed from any run loops in which it was scheduled. Once closed, the stream cannot be reopened.
CFStream.hReturns the error associated with a stream.
CFErrorRef CFReadStreamCopyError ( CFReadStreamRef stream );
The stream to examine.
A CFError object that describes the current problem with stream, or NULL if there is no error. Ownership follows the Create Rule.
CFStream.hReturns the value of a property for a stream.
CFTypeRef CFReadStreamCopyProperty ( CFReadStreamRef stream, CFStringRef propertyName );
The stream to examine.
The name of the stream property to obtain. The available properties for standard Core Foundation streams are listed in CFStream Reference.
The value of the property propertyName. Ownership follows the Create Rule.
Each type of stream can define a set of properties that either describe or configure individual streams. A property can be any information about a stream, other than the actual data the stream handles. Examples include the headers from an HTTP transmission, the expected number of bytes, file permission information, and so on. Use CFReadStreamSetProperty to modify the value of a property, although some properties are read-only.
CFStream.hCreates a readable stream for a block of memory.
CFReadStreamRef CFReadStreamCreateWithBytesNoCopy ( CFAllocatorRef alloc, const UInt8 *bytes, CFIndex length, CFAllocatorRef bytesDeallocator );
The allocator to use to allocate memory for the new object. Pass NULL or kCFAllocatorDefault to use the current default allocator.
The memory buffer to read. This memory must exist for the lifetime of the new stream.
The size of bytes.
The allocator to use to deallocate bytes when the stream is deallocated. Pass kCFAllocatorNull to prevent the stream from deallocating bytes.
The new read stream, or NULL on failure. Ownership follows the Create Rule.
You must open the stream, using CFReadStreamOpen, before reading from it.
CFStream.hCreates a readable stream for a file.
CFReadStreamRef CFReadStreamCreateWithFile ( CFAllocatorRef alloc, CFURLRef fileURL );
The allocator to use to allocate memory for the new object. Pass NULL or kCFAllocatorDefault to use the current default allocator.
The URL of the file to read. The URL must use the file scheme.
The new readable stream object, or NULL on failure. Ownership follows the Create Rule.
You must open the stream, using CFReadStreamOpen, before reading from it.
CFStream.hReturns a pointer to a stream’s internal buffer of unread data, if possible.
const UInt8 * CFReadStreamGetBuffer ( CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead );
The stream to examine.
The maximum number of bytes to read. If greater than 0, maxBytesToRead limits the number of bytes read; if 0 or less, all available bytes are read.
On return, contains the length of returned buffer. If stream is not open or has encountered an error, numBytesRead is set to -1.
A pointer to the internal buffer of unread data for stream, if possible; NULL otherwise. The buffer is good only until the next stream operation called on the stream. You should neither change the contents of the returned buffer nor attempt to deallocate the buffer; it is still owned by the stream. The bytes returned in the buffer are considered read from the stream.
CFStream.hReturns the error status of a stream. (Deprecated. Use CFReadStreamCopyError instead.)
CFStreamError CFReadStreamGetError ( CFReadStreamRef stream );
The stream to examine.
The error status of stream returned in a CFStreamError structure.
The error field is 0 if no error has occurred. If the error field is not 0, the domain field contains a code that identifies the domain in which the value of the error field should be interpreted.
CFStream.hReturns the current state of a stream.
CFStreamStatus CFReadStreamGetStatus ( CFReadStreamRef stream );
The stream to examine.
The current state of stream. See CFStreamStatus for the list of possible states.
CFStream.hReturns the type identifier the CFReadStream opaque type.
CFTypeID CFReadStreamGetTypeID ( void );
The type identifier for the CFReadStream opaque type.
CFStream.hReturns a Boolean value that indicates whether a readable stream has data that can be read without blocking.
Boolean CFReadStreamHasBytesAvailable ( CFReadStreamRef stream );
The stream to examine.
TRUE if data can be read from stream without blocking, otherwise FALSE. If stream cannot tell if data is available without actually trying to read the data, this function returns TRUE.
CFStream.hOpens a stream for reading.
Boolean CFReadStreamOpen ( CFReadStreamRef stream );
The stream to open.
TRUE if stream was successfully opened, FALSE otherwise. If stream is not in the kCFStreamStatusNotOpen state, this function returns FALSE.
Opening a stream causes it to reserve all the system resources it requires. If the stream can open in the background without blocking, this function always returns true. To learn when a background open operation completes, you can either schedule the stream into a run loop with CFReadStreamScheduleWithRunLoop and wait for the stream’s client (set with CFReadStreamSetClient) to be notified or you can poll the stream using CFReadStreamGetStatus, waiting for a status of kCFStreamStatusOpen or kCFStreamStatusError.
You do not need to wait until a stream has finished opening in the background before calling the CFReadStreamRead function. The read operation will simply block until the open has completed.
CFStream.hReads data from a readable stream.
CFIndex CFReadStreamRead ( CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength );
The stream from which to read.
The buffer into which to place the data.
The size of buffer and the maximum number of bytes to read.
The number of bytes read; 0 if the stream has reached its end; or -1 if either the stream is not open or an error occurs.
If stream is in the process of opening, this function waits until it has completed. This function blocks until at least one byte is available; it does not block until buffer is filled. To avoid blocking, call this function only if CFReadStreamHasBytesAvailable returns TRUE or after the stream’s client (set with CFReadStreamSetClient) is notified of a kCFStreamEventHasBytesAvailable event.
CFStream.hSchedules a stream into a run loop.
void CFReadStreamScheduleWithRunLoop ( CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode );
The stream to schedule.
The run loop with which to schedule stream.
The run loop mode of runLoop in which to schedule stream.
After scheduling stream with a run loop, its client (set with CFReadStreamSetClient) is notified when various events happen with the stream, such as when it finishes opening, when it has bytes available, and when an error occurs. A stream can be scheduled with multiple run loops and run loop modes. Use CFReadStreamUnscheduleFromRunLoop to later remove stream from the run loop.
CFStream.hAssigns a client to a stream, which receives callbacks when certain events occur.
Boolean CFReadStreamSetClient ( CFReadStreamRef stream, CFOptionFlags streamEvents, CFReadStreamClientCallBack clientCB, CFStreamClientContext *clientContext );
The stream to modify.
The set of events for which the client should receive callbacks. The events are listed in CFStreamEventType. If you pass kCFStreamEventNone, the current client for stream is removed.
The client callback function to be called when one of the events requested in streamEvents occurs. If NULL, the current client for stream is removed.
A structure holding contextual information for the stream client. The function copies the information out of the structure, so the memory pointed to by clientContext does not need to persist beyond the function call. If NULL, the current client for stream is removed.
TRUE if the stream supports asynchronous notification, otherwise FALSE.
To avoid polling and blocking, you can register a client to hear about interesting events that occur on a stream. Only one client per stream is allowed; registering a new client replaces the previous one.
Once you have set a client, you need to schedule the stream in a run loop using CFReadStreamScheduleWithRunLoop so that the client can receive the asynchronous notifications. You can schedule each stream in multiple run loops (for example, if you are using a thread pool). It is the caller's responsibility to ensure that at least one of the scheduled run loops is being run, otherwise the callback cannot be called.
Although all Core Foundation streams currently support asynchronous notification, future stream types may not. If a stream does not support asynchronous notification, this function returns false. Typically, such streams never block for device I/O (for example, a stream reading memory) and don’t benefit from asynchronous notification.
CFStream.hSets the value of a property for a stream.
Boolean CFReadStreamSetProperty ( CFReadStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue );
The stream to modify.
The name of the property to set. The available properties for standard Core Foundation streams are listed in CFStream Reference.
The value to which to set the property propertyName for stream. The allowed data type of the value depends on the property being set.
TRUE if stream recognizes and accepts the given property-value pair, otherwiseFALSE.
Each type of stream can define a set of properties that either describe or configure individual streams. A property can be any interesting information about a stream. Examples include the headers from an HTTP transmission, the expected number of bytes, file permission information, and so on. Properties that can be set configure the behavior of the stream and may be modifiable only at particular times, such as before the stream has been opened. (In fact, you should assume that you can set properties only before opening the stream, unless otherwise noted.) To read the value of a property use CFReadStreamCopyProperty, although some properties are write-only.
CFStream.hRemoves a read stream from a given run loop.
void CFReadStreamUnscheduleFromRunLoop ( CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode );
The stream to unschedule.
The run loop from which to remove stream.
The run loop mode of runLoop from which to remove stream.
CFStream.hCallback invoked when certain types of activity takes place on a readable stream.
typedef void (*CFReadStreamClientCallBack) ( CFReadStreamRef stream, CFStreamEventType eventType, void *clientCallBackInfo );
If you name your function MyCallBack, you would declare it like this:
void MyCallBack ( CFReadStreamRef stream, CFStreamEventType eventType, void *clientCallBackInfo );
The stream that experienced the event eventType.
The event that caused the callback to be called. The possible events are listed in CFStreamEventType.
The info member of the CFStreamClientContext structure that was used when setting the client for stream.
This callback is called only for the events requested when setting the client with CFReadStreamSetClient.
CFStream.hA reference to a readable stream object.
typedef struct __CFReadStream *CFReadStreamRef;
CFStream.hA structure that contains program-defined data and callbacks with which you can configure a stream’s client behavior.
struct CFStreamClientContext {
CFIndex version;
void *info;
CFAllocatorRetainCallBack retain;
CFAllocatorReleaseCallBack release;
CFAllocatorCopyDescriptionCallBack copyDescription;
};
typedef struct CFStreamClientContext CFStreamClientContext;
versionVersion number of the structure. Must be 0.
infoAn arbitrary pointer to program-defined data, which can be associated with the client. This pointer is passed to the callbacks defined in the context and to the client callback function CFReadStreamClientCallBack.
retainA retain callback for your program-defined info pointer. Can be NULL.
releaseA release callback for your program-defined info pointer. Can be NULL.
copyDescriptionA copy description callback for your program-defined info pointer. Can be NULL.
CFStream.hLast updated: 2007-05-03