objective-cnsoutputstream

How to write data to stream until all the data coming from server is written to NSOutputstream


How to write data to stream continuously and parse the stream until all the data coming from server is written to NSOutputstream

      NSLog(@"Response %@",[[NSString alloc] initWithData:self.m_cWebData encoding:NSUTF8StringEncoding]);
     NSUInteger written = [oStream write:(const uint8_t *)[self.m_cWebData bytes] maxLength:[self.m_cWebData length]];
     NSLog(@"Rcvd Data=%d written = %d",[self.m_cWebData length],written);
     [self.m_cWebData replaceBytesInRange:NSMakeRange(0,written) withBytes:"" length:0];
     NSLog(@"Rcvd Data after Reset =%d ",[self.m_cWebData length]);

And also I am starting parsing.but Stream:handleEvent is not getting called for the stream events.How to write the data to stream if total server response data is not taken by stream and there is some more data to be written to stream.How to handle this case.


Solution

  • do {
        bytesWritten = [oStream write:(const uint8_t *)[self.m_cWebData bytes] maxLength:dataLength - bytesWrittenSoFar];
    
        NSLog(@"Rcvd Data=%d written = %d",[self.m_cWebData length],bytesWritten);
        [self.m_cWebData replaceBytesInRange:NSMakeRange(0,bytesWritten) withBytes:"" length:0];
    
        NSLog(@"Rcvd Data after Reset =%d ",[self.m_cWebData length]);
        assert(bytesWritten != 0);
        if (bytesWritten == -1) {
    
        } else {
           bytesWrittenSoFar += bytesWritten;
        }
    } while (bytesWrittenSoFar != dataLength);