Saturday, May 11, 2013

NSInputStream inputStreamWithData example ios


inputStreamWithData:

Creates and returns an initialized NSInputStream object for reading from a given NSData object.
+ (id)inputStreamWithData:(NSData *)data
Parameters
data
The data object from which to read. The contents of data are copied.
Return Value( NSInputStream inputStreamWithData example )
An initialized NSInputStream object for reading from data. If data is not an NSData object, this method returns nil.
( NSInputStream inputStreamWithData example )
NSInputStream *dataStream = [NSInputStream inputStreamWithData:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBodyStream:dataStream];
( NSInputStream inputStreamWithData example )
ALAssetRepresentation *rep = [currentAsset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

iStream = [NSInputStream inputStreamWithData:data];
[iStream open];
( NSInputStream inputStreamWithData example )
NSInputStream *inputStream = [NSInputStream inputStreamwithData:myData]; //assuming myData is NSData*

float myFloat;

if([inputStream hasBytesAvailable]) { // NO if you've already read to the end of myData
  NSInteger bytesRead = [inputStream read:&myFloat maxLength:sizeof(myFloat)];
  NSAssert(bytesRead == sizeof(myFloat);
}