Article from: http://blog.csdn.net/caryaliu/article/details/7640197
iOS cocoa programming, reading data from Nsinputstream includes several steps:
1. Create and initialize a Nsinputstream instance from the data source
2. Configure the input stream object to a run Loop,open the stream
3. Handling events through the delegate function of a stream object
4. When all the data is read, the memory processing of the stream object is done
One, the preparation of using streaming objects
Before using the Nsinputstream object you must have a stream of data sources, the type of the data source can be a file, a NSData object, or a network socket.
Nsinputstream initialization functions and factory methods can create and initialize an instance of a nsinputstream from NSData and files. The following example is an instance of creating a nsinputstream from a file:
[CPP] view plain copy print? -(void) Setupstreamforfile: (NSString *) path {//IStream is nsinputstream instance variable iStream = [[Nsinpu Tstream alloc] Initwithfileatpath:path]; [IStream setdelegate:self]; [IStream Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; [IStream Open]; The example above shows that when you create an object you should set its delegate. When a Nsinputstream object is configured to a run loop and there is a flow-related event (for example, there is readable data in the stream), the object receives a stream:handleevent: message.
Before you open the stream, send a scheduleinrunloop:formode message to the stream object that configures the object to a run loop to receive the stream events. In this way, delegate blocking can be avoided when there is no data to read in the stream. If the stream is happening on another thread, you need to verify that the Stream object is configured in the run loop of that thread. You should not try to operate from a stream in a different thread other than a thread that contains the run loop of the flow object. Finally, an open message is sent to the Nsinputstream object to begin streaming operations on the input data.
Second, dealing with stream Events
When you send an open message to a Stream object, you can find the current state of it. The following message lets you know if the stream object has data readable and any error attributes:
Streamstatus
Hasbytesavailable
The state returned by Streamerror is a Nsstreamstatus constant that can indicate whether the stream object is in Opening,reading, or at the end of the stream, and so on. The error returned is a Nserror object that encapsulates any error messages that may occur.
Importantly, once the open stream object, the stream object will always send a stream:handleevent to its delegate: the message until the end of the stream object is reached. The parameters of these messages contain a Nsstreamevent constant indicating the type of the stream event. The most common type of event for a Nsinputstream object is nsstreameventopencompleted,nsstreameventhasbytesavailable, Nsstreameventendencountered. We are particularly interested in the Nsstreameventhasbytesavailable event. The following example is a good way to handle nsstreameventhasbytesavailable events:
[CPP] View plain copy print? - (void) stream: (nsstream *) Stream handleevent: (nsstreamevent) eventcode { switch (eventcode) { case NSStreamEventHasBytesAvailable: { if (!_data) { _ data = [[nsmutabledata data] retain]; } uint8_t buf[1024]; unsigned int len = 0; len = [(nsinputstream *) Stream read: buf maxlength:1024]; if (len) { [_data appendbytes: (const void *) buf length:len]; // bytesread is an instance variable of type NSNumber. [bytesread setintvalue:[bytesread intValue]+len]; } else { nslog (@ "no buffer!"); } break; } // continued }
Stream:handleevent: The function uses a switch statement to identify the nsstreamevent constant, and when the constant is msstreameventhasbytesavailable, the delegate function lazy Create a Nsmutabledata object _data to receive the read data. Then declare a uint8_t type array of size 1024 buf, invoke the read:maxlength: function reads the specified size of data from the stream into buf, if Read succeeds, Delegate will add the read data to the Nsmutabledata object _data and update the total read Data bytesread.
As for how much data is read from the stream, in general, use some commonly used data size specifications, such as 512bytes,1kb,4kb (a page size).
Third, processing stream object
When the Nsinputstream object reaches the end of the steam, it sends a nsstreameventendencountered event type constant to the stream:handleevent: function. The delegate function should make the opposite of preparing to use a stream object, that is, to close the Stream object, remove it from the run loop, and eventually release the stream object. As shown in the following code:
[CPP] view Plain &