NSInputStream 和 NSOutputStream

对于 NSInputStream 和 NSOutputStream 一直没怎么搞清楚,今天抽一些时间在此记录一下!

NSInputStream 与 NSOutputStream 都继承于 NSStream, NSStream 是一个抽象的基类, 规定了Stream共有的一些行为…

什么是Stream

Stream翻译成为流,它是对我们读写文件的一个抽象。 你可以这样想象,当你读文件和写文件的时候,文件的内容就像水流一样哗哗的 像你流过来或者流给别人,这样岂不是很爽。 而Stream就帮我们做了这样的事情, 实际上,它是把文件的内容,一小段一小段的读出或 写入,来到达这样的效果

NSStream

NSStream 是Cocoa平台下对流这个概念的实现类, NSInputStream 和 NSOutputStream 则是它的两个子类,分别对应了读文件和 写文件。

NSInputStream

NSInputStream 对应的是读文件,所以要记住它是要将文件的内容读到内存(你声明的一段buffer)里, 下面一段是测试代码

-(void)doTestInputStream{NSString*path=@"/Users/usr/Desktop/stream.txt";NSInputStream*readStream=[[NSInputStreamalloc]initWithFileAtPath:path];[readStreamsetDelegate:self];[readStreamscheduleInRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSRunLoopCommonModes];[readStreamopen];//调用open开始读文件}-(void)stream:(NSStream*)aStreamhandleEvent:(NSStreamEvent)eventCode{switch(eventCode){caseNSStreamEventHasBytesAvailable:{uint8_tbuf[1024];NSInputStream*reads=(NSInputStream*)aStream;NSIntegerblength=[readsread:bufmaxLength:sizeof(buf)];//把流的数据放入bufferNSData*data=[NSDatadataWithBytes:(void*)buflength:blength];NSString*string=[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];NSLog(@"%@",string);}break;//错误和无事件处理caseNSStreamEventErrorOccurred:{}break;caseNSStreamEventNone:break;//打开完成caseNSStreamEventOpenCompleted:{NSLog(@"NSStreamEventOpenCompleted");}break;default:break;}}@end

NSOutputStream

NSOutputStream 对应的是写文件,它是要将已存在的内存(buffer)里的数据写入文件, 下面同样一段是测试代码

-(NSData*)dataWillWrite{staticNSData*data=nil;staticdispatch_once_tonceToken;dispatch_once(&onceToken,^{data=[NSDatadataWithContentsOfFile:@"/Users/usr/Desktop/stream.txt"];});returndata;}-(void)doTestOutputStream{NSString*path=@"/Users/usr/Desktop/stream-write.txt";NSOutputStream*writeStream=[[NSOutputStreamalloc]initToFileAtPath:pathappend:YES];[writeStreamsetDelegate:self];[writeStreamscheduleInRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSRunLoopCommonModes];[writeStreamopen];}-(void)stream:(NSStream*)aStreamhandleEvent:(NSStreamEvent)eventCode{switch(eventCode){caseNSStreamEventHasSpaceAvailable:{NSIntegerbufSize=5;uint8_tbuf[bufSize];if(self.location+bufSize>[selfdataWillWrite].length){[[selfdataWillWrite]getBytes:bufrange:NSMakeRange(self.location,self.location+bufSize-[selfdataWillWrite].length)];}else{[[selfdataWillWrite]getBytes:bufrange:NSMakeRange(self.location,bufSize)];}NSOutputStream*writeStream=(NSOutputStream*)aStream;[writeStreamwrite:bufmaxLength:sizeof(buf)];//把buffer里的数据,写入文件self.location+=bufSize;if(self.location>=[[selfdataWillWrite]length]){//写完后关闭流[aStreamclose];}}break;caseNSStreamEventEndEncountered:{[aStreamclose];}break;//错误和无事件处理caseNSStreamEventErrorOccurred:{}break;caseNSStreamEventNone:break;//打开完成caseNSStreamEventOpenCompleted:{NSLog(@"NSStreamEventOpenCompleted");}break;default:break;}}

用途

NSInputStream 和 NSOutputStream 常用与网络传输中,比如要将一个很大的文件传送给服务器,那么NSInputStream这时候是 很好的选择, 我们可以查看到 NSURLRequest 有一个属性叫HTTPBodyStream, 这时只要设置好一个NSInputStream的实例就可以 了,最大的好处就是可以节省我们很多的内存。

另外要说明的是,NSInputStream 和 NSOutputStream其实是对 CoreFoundation 层对应的CFReadStreamRef 和 CFWriteStreamRef 的高层抽象。在使用CFNetwork时,常常会使用到CFReadStreamRef 与 CFWriteStreamRef。 下面是一段相关代码

// Keep a reference to self to use for controller callbacks//CFStreamClientContextctx={0,(__bridgevoid*)(self),NULL,NULL,NULL};// Get callbacks for stream data, stream end, and any errors//CFOptionFlagsregisteredEvents=(kCFStreamEventHasBytesAvailable|kCFStreamEventEndEncountered|kCFStreamEventErrorOccurred);// Create a read-only socket//CFReadStreamRefreadStream;CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,(__bridgeCFStringRef)host,(UInt32)port,&readStream,NULL);// Schedule the stream on the run loop to enable callbacks//if(CFReadStreamSetClient(readStream,registeredEvents,socketCallback,&ctx)){CFReadStreamScheduleWithRunLoop(readStream,CFRunLoopGetCurrent(),kCFRunLoopCommonModes);}else{[selfnetworkFailedWithErrorMessage:@"Failed to assign callback method"];return;}// Open the stream for reading//if(CFReadStreamOpen(readStream)==NO){[selfnetworkFailedWithErrorMessage:@"Failed to open read stream"];return;}CFErrorReferror=CFReadStreamCopyError(readStream);if(error!=NULL){if(CFErrorGetCode(error)!=0){NSString*errorInfo=[NSStringstringWithFormat:@"Failed to connect stream; error '%@' (code %ld)",(__bridgeNSString*)CFErrorGetDomain(error),CFErrorGetCode(error)];[selfnetworkFailedWithErrorMessage:errorInfo];}CFRelease(error);return;}

你可能感兴趣的:(NSInputStream 和 NSOutputStream)