0

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.

  • So `m_cWebData` is getting populated by a thread other than the one you show? This is implied by the "Rcvd Data after Reset" after you empty it (using an unconventional method by the look of it). – trojanfoe Feb 05 '13 at 11:33
  • I am writing data to stream in a separate thread,though that data is parsed and parsed data has been reset,how to write to stream again.parser is stopping in middle because it doesnt found the end tag – user2031994 Feb 05 '13 at 11:51
  • And where is this parser implemented? – trojanfoe Feb 05 '13 at 11:51
  • parser implemented in SAXParser class.SAXHAndler will be handling the requests – user2031994 Feb 05 '13 at 12:07
  • And does it parse the file you are writing to, or an in-memory buffer? – trojanfoe Feb 05 '13 at 12:09
  • parsing the data until end node is available.and if the stream contains start node and some data within that node,if the end node is not in the stream content then parser will keep on search for end node and it will not stop. – user2031994 Feb 05 '13 at 12:28
  • You're aren't making any sense. I don't understand *when* the parser is invoked. I don't understand *why* you are writing from a `NSMutableData` object to file from one thread when it's being populated from another, with no thread synchronization. I pretty much don't understand anything about your question. – trojanfoe Feb 05 '13 at 12:30
  • Please see the total request handling here.. http://stackoverflow.com/questions/14643416/how-to-use-nsoperation-and-dispatch-queue – user2031994 Feb 05 '13 at 12:51
  • Though I am parsing it will stop.. if stream content doesn't contain end node.. – user2031994 Feb 05 '13 at 12:53

1 Answers1

0
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);
Marek R
  • 32,568
  • 6
  • 55
  • 140