initWithData:
Initializes and returns an
NSInputStream
object for reading from a given NSData
object.
- (id)initWithData:(NSData *)data
Parameters
- data
- The data object from which to read. The contents of data are copied.
Return Value
An initialized
NSInputStream
object for reading from data.
( NSInputStream initWithData example )
NSInputStream *insrm = [[NSInputStream alloc] initWithData:data];
[insrm open];
while ([insrm hasBytesAvailable]) {
uint8_t buf[128];
NSUInteger bytesRead = [insrm read:buf maxLength:128];
NSLog(@"read %d bytes",bytesRead);
}
( NSInputStream initWithData example )
NSInputStream *stream = [[NSInputStream alloc] initWithData:myData];
[stream open];
while ([stream read:&buffer maxLength:bufferLength] > 0) {
// do something with buffer
}
( NSInputStream initWithData example )
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event {
NSLog(@"Stream triggered.");
switch(event) {
case NSStreamEventErrorOccurred:
{
NSLog(@"%@",[stream streamError].localizedDescription);
}
break;
case NSStreamEventHasSpaceAvailable: {
if(stream == outputStream) {
NSLog(@"outputStream is ready.");
}
break;
}
case NSStreamEventHasBytesAvailable: {
if(stream == inputStream) {
NSLog(@"inputStream is ready.");
uint8_t buf[1024];
unsigned int len = 0;
len = [inputStream read:buf maxLength:1024];
if(len > 0) {
NSMutableData* data=[[NSMutableData alloc] initWithLength:0];
[data appendBytes: (const void *)buf length:len];
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self readIn:s];
}
}
break;
}
default: {
NSLog(@"Stream is sending an Event: %i", event);
break;
}
}
}