Brief introduction
Nsstream is a base class that has two subclasses Nsinputstream and Inoutputstream in cocoa. Corresponds to the input and output streams, respectively. Streams provide an easy way to exchange data between different and medium, which is device-independent. You can create a stream through Nsdata,file,socketdata, and when you're working with big data, you can use the stream to read and process the edges. The stream objects in the cocoa correspond to the stream objects in the core Foundation. We can convert each other through the toll-free bridging method. Nsstream, Nsinputstream and Nsoutputstream correspond to Cfstream, Cfreadstream and Cfwritestream respectively. But the two are not exactly the same. The Core Foundation typically uses callback functions to process data. In addition, we can subclass Nsstream, Nsinputstream, and Nsoutputstream to define properties and behaviors, while stream objects in the core Foundation cannot be extended.
Introduction to Classes and methods
// Open and close stream -(void) open; -(void) close;
// Add and remove Runloop-(void) Scheduleinrunloop: (Nsrunloop *) Arunloop Formode: (NSString *) mode; -(void) Removefromrunloop: (Nsrunloop *) Arunloop Formode: (NSString *) mode;
// read Len-length content into buffer // The return value is the actual length of the read, so the maximum is Len-(Nsinteger) Read: (uint8_t *) buffer maxLength: (Nsuinteger) Len;
// read current stream data and size, note that buffer is valid only before the next stream -(BOOL) GetBuffer: (uint8_t * __nullable * __nonnull) buffer length: ( Nsuinteger *) Len;
// writes the data in buffer to the stream // return value actual write size -(Nsinteger) write: (const uint8_t *) buffer maxLength: (Nsuinteger) Len;
Example
Large file processing, TP
//Initialize InputStream- (void) Readstream: (NSString *) filepath{Nsinputstream*inputstream =[[Nsinputstream alloc] initwithfileatpath:filepath]; InputStream.Delegate=Self ; [InputStream Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; [InputStream open];}//Initialize OutputStream- (void) Writestream: (NSString *) filepath{Nsoutputstream*writestream =[[Nsoutputstream alloc] Inittofileatpath:filepath Append:yes]; [Writestream setdelegate:self]; [Writestream Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsrunloopcommonmodes]; [Writestream open];}
//Agent Method Processing- (void) Stream: (Nsstream *) Astream handleevent: (nsstreamevent) eventcode{Switch(EventCode) { Casensstreameventhasbytesavailable: {//Readuint8_t buf[2048]; Nsinteger Len=0; Len= [(Nsinputstream *) Astream read:buf maxLength:2048];//reading Data if(len) {[Data appendbytes:buf Length:len]; } } Break; Casensstreameventendencountered: {[Astream close]; } Break; Casensstreameventhasspaceavailable: {//WriteNsinteger bufSize =2048; uint8_t Buf[bufsize]; [Data getbytes:buf length:bufsize]; Nsoutputstream*writestream = (Nsoutputstream *) Astream; Nsinteger Len= [Writestream write:buf maxLength:sizeof(BUF)]; [Data setLength:0]; Self.location+=Len; if(self.location>self.filesize) {[Astream close]; } } Break; default: Break; }}
Mac quickly generates large file Python scripts.
Import OS for in range (0,200): print i os.system ("cat/usr/share/dict /words >>~/mkfile.test.txt")print"makefile over! " "
IOS Nsstream Use