initWithFileAtPath:
Initializes and returns an
NSInputStream
object that reads data from the file at a given path.
- (id)initWithFileAtPath:(NSString *)path
Parameters
- path
- The path to the file.
Return Value( NSInputStream initWithFileAtPath example )
An initialized
NSInputStream
object that reads data from the file at path. If the file specified by pathdoesn’t exist or is unreadable, returns nil
.
( NSInputStream initWithFileAtPath example )
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"bin"];
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:path];
...
( NSInputStream initWithFileAtPath example )
- (void)setUpStreamsForInputFile:(NSString *)inpath outputFile:(NSString *)outpath {
self.p_iStream = [[NSInputStream alloc] initWithFileAtPath:inpath];
[p_iStream setDelegate:self];
// here: change the queue type and use a background queue (you can change priority)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^ {
[p_iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[p_iStream open];
// here: start the loop
[[NSRunLoop currentRunLoop] run];
// note: all code below this line won't be executed, because the above method NEVER returns.
});
}
( NSInputStream initWithFileAtPath example )
@synthesize numberOfBytesTransferred = _numberOfBytesTransferred;
static const NSUInteger blockSize = 65536; // should be adjusted
- (void)startWithSourcePath:(NSString *)srcPath
destinationPath:(NSString *)dstPath
completionHandler:(void (^)(NSUInteger, NSError *))completionHandler
{
_buffer = malloc(blockSize);
_numberOfBytesTransferred = _bufferLength = _bufferOffset = 0;
_completionHandler = [completionHandler copy];
_srcStream = [[NSInputStream alloc] initWithFileAtPath:srcPath];
_dstStream = [[NSOutputStream alloc] initToFileAtPath:dstPath append:NO];
_srcStream.delegate = self;
_dstStream.delegate = self;
[_srcStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_dstStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_srcStream open];
[_dstStream open];
}