From d1b64cf0bcfd8cdbeb8aeb077a65cf647cb45b26 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Tue, 24 Sep 2013 15:40:13 -0700 Subject: [PATCH] [Issue #1288] Adding loop around write to ensure entire length of data is written --- AFNetworking/AFURLConnectionOperation.m | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index f6e9d8d..595ea86 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -721,12 +721,20 @@ didReceiveResponse:(NSURLResponse *)response { NSUInteger length = [data length]; while (YES) { + NSUInteger totalNumberOfBytesWritten = 0; if ([self.outputStream hasSpaceAvailable]) { const uint8_t *dataBuffer = (uint8_t *)[data bytes]; - if ([self.outputStream write:&dataBuffer[0] maxLength:length] == -1) { - [self.connection cancel]; - [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:self.outputStream.streamError]; - return; + + NSInteger numberOfBytesWritten = 0; + while (totalNumberOfBytesWritten < length) { + numberOfBytesWritten = [self.outputStream write:&dataBuffer[0] maxLength:length]; + if (numberOfBytesWritten == -1) { + [self.connection cancel]; + [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:self.outputStream.streamError]; + return; + } else { + totalNumberOfBytesWritten += numberOfBytesWritten; + } } break;