IOS Nsstream Use

Source: Internet
Author: User

  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

    • Nsstream base class
// 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;
    • Nsinputstream

  

// 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;
    • Nsoutputstream
// 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.