2

I am new to ios I have 10 chunks of 1024 bytes. this chunks are not in order. If i know the number of chunk how can i insert the data in a file of size 10*1024? So far i have the following code:

//method called when new chunk arrvies


 NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //create a file name to write the data to using the documents directory:
    filePath = [NSString stringWithFormat:@"%@/example.txt", 
                documentsDirectory];

    NSFileHandle *writeFile = [NSFileHandle fileHandleForWritingToURL:filePath error:nil];
    [writeFile truncateFileAtOffset:10240*sizeof(Byte)];

How to add new data using fseek?

I did try to use the following code:

if ((chunkNo == 0 )||(chunkNo == 10))
    {
        NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:filePath append:YES];
        [stream open];
        NSData *chunk = chunkContainer; // some data
        [stream write:(uint8_t *)[chunk bytes] maxLength:[chunk length]];
        [stream close];

    }
    else{
        NSData *chunk = chunkContainer; // some data
        NSUInteger insertPoint = chunkNo*1024; // get the insertion point //
        // make sure the file exists, if it does, do the following //
        NSData *oldData = [NSData dataWithContentsOfFile:filePath];

        NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:filePath append:NO];
        [stream open];
        [stream write:(uint8_t *)[oldData bytes] maxLength:insertPoint]; // write the old data up to the insertion point //
         [stream write:(uint8_t *)[chunk bytes] maxLength:[chunk length]]; // write the new data //
         [stream write:(uint8_t *)([oldData bytes]+insertPoint) maxLength:([oldData length]-insertPoint)]; // write the rest of old data at the end of the file //

        [stream close];
    }

BUT the result file is not fully correct, if the chunks are not in the right order. If the chunks are in the right order, than the result file is correctly displayed.

Tarik
  • 10,810
  • 2
  • 26
  • 40
just ME
  • 1,817
  • 6
  • 32
  • 53
  • Consider using `NSFileHandle` inlace of `NSOutputStream ` if you do not have an actual stream to write.. – zaph Jun 24 '14 at 12:48
  • Why don't you use the `NSFileHandle` for writing? Also, where are these chinks coming from and is a binary file the best option for you? – Wain Jun 24 '14 at 12:52
  • the chunks that arrive are in NSData. Could you please write for me an answer with using NSFileHndle? please? I am really new to this type of things...and though i did a search..i could t find any example based on my needs – just ME Jun 24 '14 at 12:53

2 Answers2

4

Example:

NSString *filePath = [@"~/Desktop/FHTest.txt" stringByExpandingTildeInPath];

[[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData new] attributes:nil];
NSFileHandle *fh;

NSData *data1 = [@"test Data" dataUsingEncoding:NSUTF8StringEncoding];
fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh writeData:data1];
[fh closeFile];

NSData *data2 = [@"more Data" dataUsingEncoding:NSUTF8StringEncoding];
fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh seekToFileOffset:100];
[fh writeData:data2];
[fh closeFile];

Note that the seekToFileOffset:100 will fill expand the file as necessary filling as necessary with 0x00 bytes.

To create a file of specific size, in this case 100 bytes:

unsigned long long size = 100;
[[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData new] attributes:nil];
NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh seekToFileOffset:size-1];
[fh writeData:[@"\x00" dataUsingEncoding:NSUTF8StringEncoding]];
[fh closeFile];
zaph
  • 111,848
  • 21
  • 189
  • 228
  • I was going to mention *sparse files* but it seems HFS+ does not support them, so there is no point... – trojanfoe Jun 24 '14 at 13:23
  • @trojanfoe Yeah. For any file system that uses bitmaps for allocation as opposed to lists as traditional unix, sparse files are difficult to implement. (I wrote two file system for for mini-computers, one that had to support sparse files with a bit-maped allocation system—nasty.) – zaph Jun 24 '14 at 13:30
  • @Zaph thank you for your example with using seekToFileOffset. I ll give it a try right now. Could you also please specify how to create a file of a specific size lenght? – just ME Jun 24 '14 at 13:31
  • @Zaph I wasn't going to mention them for your benefit, but for the benefit of others ;-) However as the question/answer is so Apple-focused and as HFS+ is so predominant, it's kinda irrelevant. – trojanfoe Jun 24 '14 at 13:32
  • @Zaph: this example would work, right? NSFileHandle *writeFile = [NSFileHandle fileHandleForWritingToURL:filePath error:nil]; [writeFile truncateFileAtOffset:10240*sizeof(Byte)]; – just ME Jun 24 '14 at 13:32
  • @trojanfoe I need this example for types of files like wav, aif, txt...and so on. What do you mean by HFS+ does not support them? – just ME Jun 24 '14 at 13:35
  • This is not a sparse file, what would be thew sparse areas are filled with 0x00 bytes. – zaph Jun 24 '14 at 13:37
  • 1. Post your new code. 2. If one does not have an education in Computer Science one needs to do do the reading/studying on one's own, that is what I did. (when I went to school they didn't have Computer Science.) – zaph Jun 24 '14 at 14:07
0

Regarding just the first part of the question (How I got here), if you just want an empty file of a specific size...

[[NSData dataWithBytes:@[] length:size] writeToFile:path atomically:false];
BadPirate
  • 25,802
  • 10
  • 92
  • 123