Skip to content

Instantly share code, notes, and snippets.

@fel-cesar
Created June 1, 2017 14:19
Show Gist options
  • Save fel-cesar/e991a2bcd2272ae3da45b5de804c9e9f to your computer and use it in GitHub Desktop.
Save fel-cesar/e991a2bcd2272ae3da45b5de804c9e9f to your computer and use it in GitHub Desktop.
Convert NSInputStream to NSData
+(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;
}
@778477
Copy link

778477 commented Jul 31, 2019

copy stream. also you will change the input stream read empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment