Showing posts with label NSInputStream example. Show all posts
Showing posts with label NSInputStream example. Show all posts

Sunday, May 12, 2013

NSInputStream read maxLength example ios

NSInputStream read maxLength
Reads up to a given number of bytes into a given buffer.
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len
Parameters
buffer
A data buffer. The buffer must be large enough to contain the number of bytes specified by len.
len
The maximum number of bytes to read.
Return Value( NSInputStream read maxLength example )
A number indicating the outcome of the operation:
  • A positive number indicates the number of bytes read;
  • 0 indicates that the end of the buffer was reached;
  • A negative number means that the operation failed.

( NSInputStream read maxLength example )

NSInteger result;
uint8_t buffer[BUFFER_LEN]; // BUFFER_LEN can be any positive integer
while((result = [iStream read:buffer maxLength:BUFFER_LEN]) != 0) {
    if(result > 0) {
        // buffer contains result bytes of data to be handled
    } else {
        // The stream had an error. You can get an NSError object using [iStream streamError]
    }
}
// Either the stream ran out of data or there was an error

( NSInputStream read maxLength example )

uint8_t *buf1;
        unsigned int numBytes1;
        if ([((NSInputStream *) stream) getBuffer: &buf length: &numBytes]) {
            NSLog(@"\t\tBytes are in the buffer before Op.");
        }

        uint8_t buffer[BUFFER_SIZE];
        int len = [((NSInputStream *) stream) read: buffer 
                                         maxLength: BUFFER_SIZE];

        uint8_t *buf2;
        unsigned int numBytes2;
        if ([((NSInputStream *) stream) getBuffer: &buf2 length: &numBytes2]) {
            NSLog(@"\t\tBytes in the buffer after Op.");
        }

( NSInputStream read maxLength example )

- (void)stream:(NSInputStream *)iStream handleEvent:(NSStreamEvent)event {
    BOOL shouldClose = NO;
    switch(event) {
        case  NSStreamEventEndEncountered:
            shouldClose = YES;
            // If all data hasn't been read, fall through to the "has bytes" event
            if(![iStream hasBytesAvailable]) break;
        case NSStreamEventHasBytesAvailable: ; // We need a semicolon here before we can declare local variables
            uint8_t *buffer;
            NSUInteger length;
            BOOL freeBuffer = NO;
            // The stream has data. Try to get its internal buffer instead of creating one
            if(![iStream getBuffer:&buffer length:&length]) {
                // The stream couldn't provide its internal buffer. We have to make one ourselves
                buffer = malloc(BUFFER_LEN * sizeof(uint8_t));
                freeBuffer = YES;
                NSInteger result = [iStream read:buffer maxLength:BUFFER_LEN];
                if(result < 0) {
                    // error copying to buffer
                    break;
                }
                length = result;
            }
            // length bytes of data in buffer
            if(freeBuffer) free(buffer);
            break;
        case NSStreamEventErrorOccurred:
            // some other error
            shouldClose = YES;
            break;
    }
    if(shouldClose) [iStream close];
}

NSInputStream read example ios


read: maxLength:

Reads up to a given number of bytes into a given buffer.
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len
Parameters
buffer
A data buffer. The buffer must be large enough to contain the number of bytes specified by len.
len
The maximum number of bytes to read.
Return Value( NSInputStream read example )
A number indicating the outcome of the operation:
  • A positive number indicates the number of bytes read;
  • 0 indicates that the end of the buffer was reached;
  • A negative number means that the operation failed.

( NSInputStream read example )

NSInteger result;
uint8_t buffer[BUFFER_LEN]; // BUFFER_LEN can be any positive integer
while((result = [iStream read:buffer maxLength:BUFFER_LEN]) != 0) {
    if(result > 0) {
        // buffer contains result bytes of data to be handled
    } else {
        // The stream had an error. You can get an NSError object using [iStream streamError]
    }
}
// Either the stream ran out of data or there was an error

( NSInputStream read example )

uint8_t *buf1;
        unsigned int numBytes1;
        if ([((NSInputStream *) stream) getBuffer: &buf length: &numBytes]) {
            NSLog(@"\t\tBytes are in the buffer before Op.");
        }

        uint8_t buffer[BUFFER_SIZE];
        int len = [((NSInputStream *) stream) read: buffer 
                                         maxLength: BUFFER_SIZE];

        uint8_t *buf2;
        unsigned int numBytes2;
        if ([((NSInputStream *) stream) getBuffer: &buf2 length: &numBytes2]) {
            NSLog(@"\t\tBytes in the buffer after Op.");
        }

( NSInputStream read example )

- (void)stream:(NSInputStream *)iStream handleEvent:(NSStreamEvent)event {
    BOOL shouldClose = NO;
    switch(event) {
        case  NSStreamEventEndEncountered:
            shouldClose = YES;
            // If all data hasn't been read, fall through to the "has bytes" event
            if(![iStream hasBytesAvailable]) break;
        case NSStreamEventHasBytesAvailable: ; // We need a semicolon here before we can declare local variables
            uint8_t *buffer;
            NSUInteger length;
            BOOL freeBuffer = NO;
            // The stream has data. Try to get its internal buffer instead of creating one
            if(![iStream getBuffer:&buffer length:&length]) {
                // The stream couldn't provide its internal buffer. We have to make one ourselves
                buffer = malloc(BUFFER_LEN * sizeof(uint8_t));
                freeBuffer = YES;
                NSInteger result = [iStream read:buffer maxLength:BUFFER_LEN];
                if(result < 0) {
                    // error copying to buffer
                    break;
                }
                length = result;
            }
            // length bytes of data in buffer
            if(freeBuffer) free(buffer);
            break;
        case NSStreamEventErrorOccurred:
            // some other error
            shouldClose = YES;
            break;
    }
    if(shouldClose) [iStream close];
}

NSInputStream initWithURL example ios


initWithURL:

Initializes and returns an NSInputStream object that reads data from the file at a given URL.
- (id)initWithURL:(NSURL *)url
Parameters
url
The URL to the file.
Return Value( NSInputStream initWithURL example )
An initialized NSInputStream object that reads data from the file at url. If the file specified by url doesn’t exist or is unreadable, returns nil.
( NSInputStream initWithURL example )
NSInputStream * stream = [[NSInputStream alloc] initWithURL:yamlURL];
( NSInputStream initWithURL example )
- (void)viewDidLoad
{

int actuallyRead=0;

NSString *path=[self dateFilePath];

NSURL *audiourl=[[NSURL alloc]initFileURLWithPath:path];

NSLog(@"%@",audiourl);

inStream=[[NSInputStream alloc] initWithURL: audiourl];
[inStream open];
actuallyRead=[inStream read:buffer maxLength:sizeof(buffer)];

NSLog(@"%d",actuallyRead);

[dataBuffer1 appendBytes:buffer length:actuallyRead];

NSLog(@"%d",actuallyRead);

[inStream release];

[super viewDidLoad];

}

NSInputStream initWithFileAtPath example ios


initWithFileAtPath:

Initializes and returns an NSInputStream object that reads data from the file at a given path.
- (id)initWithFileAtPath:(NSString *)path
Parameters
path
The path to the file.
Return Value( NSInputStream initWithFileAtPath example )
An initialized NSInputStream object that reads data from the file at path. If the file specified by pathdoesn’t exist or is unreadable, returns nil.
( NSInputStream initWithFileAtPath example )
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"bin"];
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:path];
...
( NSInputStream initWithFileAtPath example )
- (void)setUpStreamsForInputFile:(NSString *)inpath outputFile:(NSString *)outpath  {
    self.p_iStream = [[NSInputStream alloc] initWithFileAtPath:inpath];
    [p_iStream setDelegate:self];

    // here: change the queue type and use a background queue (you can change priority)
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_async(queue, ^ {
        [p_iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                       forMode:NSDefaultRunLoopMode];
        [p_iStream open];

        // here: start the loop
        [[NSRunLoop currentRunLoop] run];
        // note: all code below this line won't be executed, because the above method NEVER returns.
    });    
}
( NSInputStream initWithFileAtPath example )
@synthesize numberOfBytesTransferred = _numberOfBytesTransferred;

static const NSUInteger blockSize = 65536; // should be adjusted

- (void)startWithSourcePath:(NSString *)srcPath
            destinationPath:(NSString *)dstPath
          completionHandler:(void (^)(NSUInteger, NSError *))completionHandler
{
    _buffer = malloc(blockSize);
    _numberOfBytesTransferred = _bufferLength = _bufferOffset = 0;
    _completionHandler = [completionHandler copy];
    _srcStream = [[NSInputStream alloc] initWithFileAtPath:srcPath];
    _dstStream = [[NSOutputStream alloc] initToFileAtPath:dstPath append:NO];
    _srcStream.delegate = self;
    _dstStream.delegate = self;
    [_srcStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    [_dstStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    [_srcStream open];
    [_dstStream open];
}