Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSFileHandle.h |
Related sample code |
NSFileHandle
objects provide an object-oriented wrapper for accessing open files or communications channels.
See the PictureSharing example project to examine code that creates an NSFileHandle object to listen for incoming connections; the file-handle object is initialized from a socket obtained through BSD calls.
Note: The deallocation of an NSFileHandle
object deletes its descriptor and closes the represented file or channel unless the NSFileHandle
object was created with initWithFileDescriptor:
or initWithFileDescriptor:closeOnDealloc:
with NO
as the parameter argument.
+ fileHandleForReadingAtPath:
+ fileHandleForWritingAtPath:
+ fileHandleForUpdatingAtPath:
+ fileHandleWithStandardError
+ fileHandleWithStandardInput
+ fileHandleWithStandardOutput
+ fileHandleWithNullDevice
– acceptConnectionInBackgroundAndNotify
– acceptConnectionInBackgroundAndNotifyForModes:
– readInBackgroundAndNotify
– readInBackgroundAndNotifyForModes:
– readToEndOfFileInBackgroundAndNotify
– readToEndOfFileInBackgroundAndNotifyForModes:
– waitForDataInBackgroundAndNotify
– waitForDataInBackgroundAndNotifyForModes:
Returns a file handle initialized for reading the file, device, or named socket at the specified path.
+ (id)fileHandleForReadingAtPath:(NSString *)path
The path to the file, device, or named socket to access.
The initialized file handle, or nil
if no file exists at path.
The file pointer is set to the beginning of the file. The returned object responds only to NSFileHandle
read...
messages.
NSFileHandle.h
Returns a file handle initialized for reading and writing to the file, device, or named socket at the specified path.
+ (id)fileHandleForUpdatingAtPath:(NSString *)path
The path to the file, device, or named socket to access.
The initialized file handle, or nil
if no file exists at path.
The file pointer is set to the beginning of the file. The returned object responds to both NSFileHandle
read...
messages and writeData:
.
NSFileHandle.h
Returns a file handle initialized for writing to the file, device, or named socket at the specified path.
+ (id)fileHandleForWritingAtPath:(NSString *)path
The path to the file, device, or named socket to access.
The initialized file handle, or nil
if no file exists at path.
The file pointer is set to the beginning of the file. The returned object responds only to writeData:
.
NSFileHandle.h
Returns a file handle associated with a null device.
+ (id)fileHandleWithNullDevice
A file handle associated with a null device.
You can use null-device file handles as “placeholders” for standard-device file handles or in collection objects to avoid exceptions and other errors resulting from messages being sent to invalid file handles. Read messages sent to a null-device file handle return an end-of-file indicator (an empty NSData
object) rather than raise an exception. Write messages are no-ops, whereas fileDescriptor
returns an illegal value. Other methods are no-ops or return “sensible” values.
NSFileHandle.h
Returns the file handle associated with the standard error file.
+ (id)fileHandleWithStandardError
The shared file handle associated with the standard error file.
Conventionally this is a terminal device to which error messages are sent. There is one standard error file handle per process; it is a shared instance.
NSFileHandle.h
Returns the file handle associated with the standard input file.
+ (id)fileHandleWithStandardInput
The shared file handle associated with the standard input file.
Conventionally this is a terminal device on which the user enters a stream of data. There is one standard input file handle per process; it is a shared instance.
NSFileHandle.h
Returns the file handle associated with the standard output file.
+ (id)fileHandleWithStandardOutput
The shared file handle associated with the standard output file.
Conventionally this is a terminal device that receives a stream of data from a program. There is one standard output file handle per process; it is a shared instance.
NSFileHandle.h
Accepts a socket connection (for stream-type sockets only) in the background and creates a file handle for the “near” (client) end of the communications channel.
- (void)acceptConnectionInBackgroundAndNotify
This method is asynchronous. In a separate “safe” thread it accepts a connection, creates a file handle for the other end of the connection, and returns that object to the client by posting an NSFileHandleConnectionAcceptedNotification
in the run loop of the client. The notification includes as data a userInfo dictionary containing the created NSFileHandle
object; access this object using the NSFileHandleNotificationFileHandleItem
key.
The receiver must be created by an initWithFileDescriptor:
message that takes as an argument a stream-type socket created by the appropriate system routine. The object that will write data to the returned file handle must add itself as an observer of NSFileHandleConnectionAcceptedNotification
.
Note that this method does not continue to listen for connection requests after it posts NSFileHandleConnectionAcceptedNotification
. If you want to keep getting notified, you need to call acceptConnectionInBackgroundAndNotify
again in your observer method.
– enqueueNotification:postingStyle:coalesceMask:forModes:
(NSNotificationQueue
)– readInBackgroundAndNotify
– readToEndOfFileInBackgroundAndNotify
NSFileHandle.h
Accepts a socket connection (for stream-type sockets only) in the background and creates a file handle for the “near” (client) end of the communications channel.
- (void)acceptConnectionInBackgroundAndNotifyForModes:(NSArray *)modes
The runloop modes in which the connection accepted notification can be posted.
See acceptConnectionInBackgroundAndNotify
for details of how this method operates. This method differs from acceptConnectionInBackgroundAndNotify
in that modes specifies the run-loop mode (or modes) in which NSFileHandleConnectionAcceptedNotification
can be posted.
– enqueueNotification:postingStyle:coalesceMask:forModes:
(NSNotificationQueue
)– readInBackgroundAndNotifyForModes:
– readToEndOfFileInBackgroundAndNotifyForModes:
NSFileHandle.h
Returns the data available through the receiver.
- (NSData *)availableData
The data currently available through the receiver.
If the receiver is a file, returns the data obtained by reading the file from the file pointer to the end of the file. If the receiver is a communications channel, reads up to a buffer of data and returns it; if no data is available, the method blocks. Returns an empty data object if the end of file is reached. Raises NSFileHandleOperationException
if attempts to determine file-handle type fail or if attempts to read from the file or channel fail.
NSFileHandle.h
Disallows further access to the represented file or communications channel and signals end of file on communications channels that permit writing.
- (void)closeFile
The file or communications channel is available for other uses after the file handle represented by the receiver is closed. Further read and write messages sent to a file handle to which closeFile
has been sent raises an exception.
Sending closeFile
to a file handle does not cause its deallocation. The deallocation of an NSFileHandle
object deletes its descriptor and closes the represented file or channel unless the NSFileHandle
object was created with initWithFileDescriptor:
or initWithFileDescriptor:closeOnDealloc:
with NO
as the parameter argument.
NSFileHandle.h
Returns the file descriptor associated with the receiver.
- (int)fileDescriptor
The POSIX file descriptor associated with the receiver.
You can send this message to file handles originating from both file descriptors and file handles and receive a valid file descriptor so long as the file handle is open. If the file handle has been closed by sending it closeFile
, this method raises an exception.
NSFileHandle.h
Returns a file handle initialized with a file descriptor.
- (id)initWithFileDescriptor:(int)fileDescriptor
The POSIX file descriptor with which to initialize the file handle.
A file handle initialized with fileDescriptor.
You can create a file handle for a socket by using the result of a socket
call as fileDescriptor.
The object creating a file handle using this method owns fileDescriptor and is responsible for its disposition.
NSFileHandle.h
Returns a file handle initialized with a file handle, using a specified deallocation policy.
- (id)initWithFileDescriptor:(int)fileDescriptor closeOnDealloc:(BOOL)flag
The POSIX file descriptor with which to initialize the file handle.
YES
if the file descriptor should be closed when the receiver is deallocated, otherwise NO
.
A file handle initialized with fileDescriptor with a deallocation policy specified by flag.
If flag is NO
, the object creating a file handle using this method owns fileDescriptor and is responsible for its disposition.
NSFileHandle.h
Returns the position of the file pointer within the file represented by the receiver.
- (unsigned long long)offsetInFile
The position of the file pointer within the file represented by the receiver.
Raises an exception if the message is sent to a file handle representing a pipe or socket or if the file descriptor is closed.
NSFileHandle.h
Reads data up to a specified number of bytes from the receiver.
- (NSData *)readDataOfLength:(NSUInteger)length
The number of bytes to read from the receiver.
The data available through the receiver up to a maximum of length bytes.
If the receiver is a file, returns the data obtained by reading from the file pointer to length or to the end of the file, whichever comes first. If the receiver is a communications channel, the method reads data from the channel up to length. Returns an empty NSData
object if the file is positioned at the end of the file or if an end-of-file indicator is returned on a communications channel. Raises NSFileHandleOperationException
if attempts to determine file-handle type fail or if attempts to read from the file or channel fail.
NSFileHandle.h
Returns the data available through the receiver up to the end of file or maximum number of bytes.
- (NSData *)readDataToEndOfFile
The data available through the receiver up to UINT_MAX
bytes (the maximum value for unsigned integers) or, if a communications channel, until an end-of-file indicator is returned.
This method invokes readDataOfLength:
as part of its implementation.
NSFileHandle.h
Reads from the file or communications channel in the background and posts a notification when finished.
- (void)readInBackgroundAndNotify
This method performs an asynchronous availableData
operation on a file or communications channel and posts an NSFileHandleReadCompletionNotification
to the client process’s run loop.
The length of the data is limited to the buffer size of the underlying operating system. The notification includes a userInfo dictionary that contains the data read; access this object using the NSFileHandleNotificationDataItem
key.
Any object interested in receiving this data asynchronously must add itself as an observer of NSFileHandleReadCompletionNotification
. In communication via stream-type sockets, the receiver is often the object returned in the userInfo dictionary of NSFileHandleConnectionAcceptedNotification
.
Note that this method does not cause a continuous stream of notifications to be sent. If you wish to keep getting notified, you’ll also need to call readInBackgroundAndNotify
in your observer method.
– acceptConnectionInBackgroundAndNotify
– readToEndOfFileInBackgroundAndNotifyForModes:
– enqueueNotification:postingStyle:coalesceMask:forModes:
(NSNotificationQueue
)NSFileHandle.h
Reads from the file or communications channel in the background and posts a notification when finished.
- (void)readInBackgroundAndNotifyForModes:(NSArray *)modes
The runloop modes in which the read completion notification can be posted.
See readInBackgroundAndNotify
for details of how this method operates. This method differs from readInBackgroundAndNotify
in that modes specifies the run-loop mode (or modes) in which NSFileHandleReadCompletionNotification
can be posted.
– acceptConnectionInBackgroundAndNotifyForModes:
– enqueueNotification:postingStyle:coalesceMask:forModes:
(NSNotificationQueue
)NSFileHandle.h
Reads to the end of file from the file or communications channel in the background and posts a notification when finished.
- (void)readToEndOfFileInBackgroundAndNotify
This method performs an asynchronous readToEndOfFile
operation on a file or communications channel and posts an NSFileHandleReadToEndOfFileCompletionNotification
to the client process’s run loop.
The notification includes a userInfo dictionary that contains the data read; access this object using the NSFileHandleNotificationDataItem
key.
Any object interested in receiving this data asynchronously must add itself as an observer of NSFileHandleReadToEndOfFileCompletionNotification
. In communication via stream-type sockets, the receiver is often the object returned in the userInfo dictionary of NSFileHandleConnectionAcceptedNotification
.
– acceptConnectionInBackgroundAndNotify
– readToEndOfFileInBackgroundAndNotifyForModes:
– enqueueNotification:postingStyle:coalesceMask:forModes:
(NSNotificationQueue
)NSFileHandle.h
Reads to the end of file from the file or communications channel in the background and posts a notification when finished.
- (void)readToEndOfFileInBackgroundAndNotifyForModes:(NSArray *)modes
The runloop modes in which the read completion notification can be posted.
See readToEndOfFileInBackgroundAndNotify
for details of this method's operation. The method differs from readToEndOfFileInBackgroundAndNotify
in that modes specifies the run-loop mode (or modes) in which NSFileHandleReadToEndOfFileCompletionNotification
can be posted.
– acceptConnectionInBackgroundAndNotifyForModes:
– enqueueNotification:postingStyle:coalesceMask:forModes:
(NSNotificationQueue
)NSFileHandle.h
Puts the file pointer at the end of the file referenced by the receiver and returns the new file offset.
- (unsigned long long)seekToEndOfFile
The file offset with the file pointer at the end of the file. This is therefore equal to the size of the file.
Raises an exception if the message is sent to an NSFileHandle
object representing a pipe or socket or if the file descriptor is closed.
NSFileHandle.h
Moves the file pointer to the specified offset within the file represented by the receiver.
- (void)seekToFileOffset:(unsigned long long)offset
The offset to seek to.
Raises an exception if the message is sent to an NSFileHandle
object representing a pipe or socket, if the file descriptor is closed, or if any other error occurs in seeking.
NSFileHandle.h
Causes all in-memory data and attributes of the file represented by the receiver to be written to permanent storage.
- (void)synchronizeFile
This method should be invoked by programs that require the file to always be in a known state. An invocation of this method does not return until memory is flushed.
NSFileHandle.h
Truncates or extends the file represented by the receiver to a specified offset within the file and puts the file pointer at that position.
- (void)truncateFileAtOffset:(unsigned long long)offset
The offset within the file that will mark the new end of the file.
If the file is extended (if offset is beyond the current end of file), the added characters are null bytes.
NSFileHandle.h
Checks to see if data is available in a background thread.
- (void)waitForDataInBackgroundAndNotify
When the data becomes available, the thread notifies all observers with NSFileHandleDataAvailableNotification
. After the notification has been posted, the thread is terminated.
NSFileHandle.h
Checks to see if data is available in a background thread.
- (void)waitForDataInBackgroundAndNotifyForModes:(NSArray *)modes
The runloop modes in which the data available notification can be posted.
When the data becomes available, the thread notifies all observers with NSFileHandleDataAvailableNotification
. After the notification has been posted, the thread is terminated. This method differs from waitForDataInBackgroundAndNotify
in that modes specifies the run-loop mode (or modes) in which NSFileHandleDataAvailableNotification
can be posted.
NSFileHandle.h
Synchronously writes data to the file, device, pipe, or socket represented by the receiver.
- (void)writeData:(NSData *)data
The data to be written.
If the receiver is a file, writing takes place at the file pointer’s current position. After it writes the data, the method advances the file pointer by the number of bytes written. Raises an exception if the file descriptor is closed or is not valid, if the receiver represents an unconnected pipe or socket endpoint, if no free space is left on the file system, or if any other writing error occurs.
NSFileHandle.h
Strings that are used as keys in a userinfo dictionary in a file handle notification.
NSString * const NSFileHandleNotificationFileHandleItem; NSString * const NSFileHandleNotificationDataItem;
NSFileHandleNotificationFileHandleItem
A key in the userinfo dictionary in a NSFileHandleConnectionAcceptedNotification notification.
The corresponding value is the NSFileHandle
object representing the “near” end of a socket connection.
Available in Mac OS X v10.0 and later.
Declared in NSFileHandle.h
.
NSFileHandleNotificationDataItem
A key in the userinfo dictionary in a NSFileHandleReadCompletionNotification and NSFileHandleReadToEndOfFileCompletionNotification.
The corresponding value is an NSData
object containing the available data read from a socket connection.
Available in Mac OS X v10.0 and later.
Declared in NSFileHandle.h
.
NSFileHandle.h
Constant that defines the name of a file operation exception.
extern NSString *NSFileHandleOperationException;
NSFileHandleOperationException
Raised by NSFileHandle
if attempts to determine file-handle type fail or if attempts to read from a file or channel fail.
Available in Mac OS X v10.0 and later.
Declared in NSFileHandle.h
.
NSFileHandle.h
Constant that is currently unused.
NSString * const NSFileHandleNotificationMonitorModes;
NSFileHandleNotificationMonitorModes
Currently unused.
Available in Mac OS X v10.0 and later.
Declared in NSFileHandle.h
.
NSFileHandle.h
NSFileHandle
posts several notifications related to asynchronous background I/O operations. They are set to post when the run loop of the thread that started the asynchronous operation is idle.
This notification is posted when an NSFileHandle
object establishes a socket connection between two processes, creates an NSFileHandle
object for one end of the connection, and makes this object available to observers by putting it in the userInfo dictionary. To cause the posting of this notification, you must send either acceptConnectionInBackgroundAndNotify
or acceptConnectionInBackgroundAndNotifyForModes:
to an NSFileHandle
object representing a server stream-type socket.
The notification object is the NSFileHandle
object that sent the notification. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
The |
|
|
An |
NSFileHandle.h
This notification is posted when the background thread determines that data is currently available for reading in a file or at a communications channel. The observers can then issue the appropriate messages to begin reading the data. To cause the posting of this notification, you must send either waitForDataInBackgroundAndNotify
or waitForDataInBackgroundAndNotifyForModes:
to an appropriate NSFileHandle
object.
The notification object is the NSFileHandle
object that sent the notification. This notification does not contain a userInfo dictionary.
NSFileHandle.h
This notification is posted when the background thread reads the data currently available in a file or at a communications channel. It makes the data available to observers by putting it in the userInfo dictionary. To cause the posting of this notification, you must send either readInBackgroundAndNotify
or readInBackgroundAndNotifyForModes:
to an appropriate NSFileHandle
object.
The notification object is the NSFileHandle
object that sent the notification. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
An |
|
|
An |
NSFileHandle.h
This notification is posted when the background thread reads all data in the file or, if a communications channel, until the other process signals the end of data. It makes the data available to observers by putting it in the userInfo dictionary. To cause the posting of this notification, you must send either readToEndOfFileInBackgroundAndNotify
or readToEndOfFileInBackgroundAndNotifyForModes:
to an appropriate NSFileHandle
object.
The notification object is the NSFileHandle
object that sent the notification. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
An |
|
|
An |
NSFileHandle.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)