Saturday, April 27, 2013

NSFileHandle writeData example ios


writeData:

Synchronously writes the specified data to the receiver.
- (void)writeData:(NSData *)data
Parameters
data
The data to be written.
Discussion of [NSFileHandle writeData]
If the receiver is a file, writing takes place at the file pointer’s current position. After it writes the data, the method advances the file pointer by the number of bytes written. [NSFileHandle writeData]This method raises an exception if the file descriptor is closed or is not valid, if the receiver represents an unconnected pipe or socket endpoint, if no free space is left on the file system, or if any other writing error occurs.

Example of [NSFileHandle writeData]

Write to file.
----------------------------------------------------------------------------
NSString *testFile = @"/test.txt";
NSFileHandle * file = [NSFileHandle fileHandleForWritingAtPath:testFile];
[file seekToFileOffset:30]; 
NSLog(@"offset : %llu", [file offsetInFile]); 

NSData *databuffer = [file readDataOfLength:10];
[file seekToFileOffset:50]; 
[file writeData :databuffer];

[file closeFile]; 

----------------------------------------------------------------------------
- (NSString *)outputFromExporter:(COExporter *)exporter input:(NSString *)input {
  NSString *exportedString = nil;
  NSString *path = [exporter path];
  NSTask *task = [[NSTask alloc] init];

  NSPipe *writePipe = [NSPipe pipe];
  NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
  NSPipe *readPipe = [NSPipe pipe];
  NSFileHandle *readHandle = [readPipe fileHandleForReading];

  NSMutableData *outputData = [[NSMutableData alloc] init];
  NSData *readData = nil;

  // Set the launch path and I/O for the task
  [task setLaunchPath:path];
  [task setStandardInput:writePipe];
  [task setStandardOutput:readPipe];

  // Launch the exporter, it will convert the raw OPML into HTML, Plaintext, etc
  [task launch];

  // Write the raw OPML representation to the exporter's input stream
  [writeHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
  [writeHandle closeFile];

  while ((readData = [readHandle availableData]) && [readData length]) {
    [outputData appendData:readData];
  }

  exportedString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
  return exportedString;
}

NSFileHandle waitForDataInBackgroundAndNotifyForModes example ios


waitForDataInBackgroundAndNotifyForModes:

Asynchronously checks to see if data is available.
- (void)waitForDataInBackgroundAndNotifyForModes:(NSArray *)modes
Parameters
modes
The runloop modes in which the data available notification can be posted.
Discussion of [NSFileHandle waitForDataInBackgroundAndNotifyForModes]
When the data becomes available, this method posts a NSFileHandleDataAvailableNotification notification on the current thread. This method differs from waitForDataInBackgroundAndNotify in that modes specifies the run-loop mode (or modes) in which NSFileHandleDataAvailableNotification can be posted. [NSFileHandle waitForDataInBackgroundAndNotifyForModes]
You must call this method from a thread that has an active run loop.
Example of [NSFileHandle waitForDataInBackgroundAndNotifyForModes]
-(void) errorData: (NSNotification *) notification
{ NSFileHandle *fileHandle = (NSFileHandle*) [notification object]; NSData *data = [fileHandle availableData]; if ([data length]) { // consume data // ... [fileHandle waitForDataInBackgroundAndNotifyForModes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
} else { // EOF was hit, remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object:fileHandle]; } }

NSFileHandle waitForDataInBackgroundAndNotify example ios


waitForDataInBackgroundAndNotify

Asynchronously checks to see if data is available.
- (void)waitForDataInBackgroundAndNotify
Discussion
When the data becomes available, this method posts a NSFileHandleDataAvailableNotification notification on the current thread.[NSFileHandle waitForDataInBackgroundAndNotify]
You must call this method from a thread that has an active run loop.
Example of [NSFileHandle waitForDataInBackgroundAndNotify]

/** * Fired whenever new data becomes available on STDERR * * @private */
-(void) errorData: (NSNotification *) notification { NSFileHandle *fileHandle = (NSFileHandle*) [notification object]; NSData *data = [fileHandle availableData]; if ([data length]) { // consume data // ... [fileHandle waitForDataInBackgroundAndNotify]; } else { // EOF was hit, remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object:fileHandle]; } }

---------------------------------------------------------------------------

-(void) outData: (NSNotification *) notification
{
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
NSData *data = [fileHandle availableData];

if ([data length]) {

    NSString *currentString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    // do something 
}

[fileHandle waitForDataInBackgroundAndNotify]; //Checks to see if data is available in a background thread.
}


-(void) errData: (NSNotification *) notification
{
NSLog(@"errData");
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
[fileHandle waitForDataInBackgroundAndNotify];
}

NSFileHandle truncateFileAtOffset example ios


truncateFileAtOffset:

Truncates or extends the file represented by the receiver to a specified offset within the file and puts the file pointer at that position.
- (void)truncateFileAtOffset:(unsigned long long)offset
Parameters
offset
The offset within the file that will mark the new end of the file.
Discussion of [NSFileHandle truncateFileAtOffset]
If the file is extended (if offset is beyond the current end of file), the added characters are null bytes.
Example of [NSFileHandle truncateFileAtOffset]

// get path to Documents/somefile.txt
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"somefile.txt"];

// create if needed
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
    [[NSData data] writeToFile:path atomically:YES];
} 

// append
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
[handle writeData:[@"line of text\n" dataUsingEncoding:NSUTF8StringEncoding]];

NSFileHandle synchronizeFile example ios


synchronizeFile

Causes all in-memory data and attributes of the file represented by the receiver to be written to permanent storage.
- (void)synchronizeFile
Discussion of [NSFileHandle synchronizeFile]
This method should be invoked by programs that require the file to always be in a known state. An invocation of this method does not return until memory is flushed.
Example of [NSFileHandle synchronizeFile]
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filename];
...
[fileHandle writeData:dataComeInFromURLConnection];
...
[fileHandle synchronizeFile];