Created
June 1, 2017 14:19
-
-
Save fel-cesar/e991a2bcd2272ae3da45b5de804c9e9f to your computer and use it in GitHub Desktop.
Convert NSInputStream to NSData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(NSData*) dataWithInputStream:(NSInputStream*) stream { | |
NSMutableData * data = [NSMutableData data]; | |
[stream open]; | |
NSInteger result; | |
uint8_t buffer[1024]; // BUFFER_LEN can be any positive integer | |
while((result = [stream read:buffer maxLength:1024]) != 0) { | |
if(result > 0) { | |
// buffer contains result bytes of data to be handled | |
[data appendBytes:buffer length:result]; | |
} else { | |
// The stream had an error. You can get an NSError object using [iStream streamError] | |
if (result<0) { | |
[NSException raise:@"STREAM_ERROR" format:@"%@", [stream streamError]]; | |
} | |
} | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copy stream. also you will change the input stream read empty