瀏覽代碼

[Issue #1288] Adding loop around write to ensure entire length of data is written

Mattt Thompson 12 年之前
父節點
當前提交
d1b64cf0bc
共有 1 個文件被更改,包括 12 次插入4 次删除
  1. 12 4
      AFNetworking/AFURLConnectionOperation.m

+ 12 - 4
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;