Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:48:27+00:00 2026-06-04T05:48:27+00:00

I need to change the header informations of a file in document folder. What

  • 0

I need to change the header informations of a file in document folder.
What is the most recommended way to read and write back binary data?

  1. How to read the data from document folder binary to array / stream
  2. How to write the data back from array / stream to local iPad document folder?

Binary reading / writing in objective-c

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:48:28+00:00Added an answer on June 4, 2026 at 5:48 am

    This one is hard to answer without you being a little more specific, but the most efficient way to do this would arguably be to not change the file at all.

    Since we’re talking about iOS, there is no file-system-level access to those documents apart from your application itself. So why not save the additional/customized header data that you want to associate with your files in an application-wide meta-data-store (like e.g. iTunes or iPhoto do) and interleave them with the actual file headers during exports only?

    Regardless of that, I don’t really see a compelling reason to drop down to the C-level file functions to change those data: NSInputStream provides you with streaming file reading-access and NSOutputStream can be used to stream data to a file.

    If you go with my suggestion from above you’d likely end up with an API like this:

    typedef void (^DataExportHandler)(NSData *resultData, NSError *exportError);
    
    @interface DataStore (FileExport)
    
    /** If you wanted to abort the export, you could pass the stream into the `abort…:`-method
    @param  identifier  Something that you use internally to manage your stored files.
    @param  error       For good measure…
    @return The export stream for the object or `nil` if an error occurred.
    */
    - (NSInputStream *)exportStreamForObjectWithIdentifier:(id)identifier error:(NSError * __autoreleasing*)error;
    
    /** If your data are mostly small, it may be more convenient to not consume the exports as streams but as BLOBs, if the sizes vary you could implement this as a convenience…
    @param  identifier  Equivalent to the identifier in the method above
    @param  handler     Callback that is invoked once some time later when the export finished or failed. **Must not** be `nil`.
    */
    @return A cancellation token.
    - (id)asynchronouslyExportDataForObjectWithIdentifier:(id)identifier resultHandler:(DataExportHandler)handler;
    
    /**
    @param  exportToken  Either a stream from the first method or a token returned from the second one.
    */
    - (void)abortAsynchronousExportWithToken:(id)exportToken;
    
    @end
    

    Assuming ARC and not knowing what you have to do to interleave the additional metadata with the original, here is what the boilerplate part of the implementation might look like.

    The beef would clearly be in the part that I don’t show here: implementation of the delegate for the rawDataStream where you’d consume the data from the original file, interleaving the headers with your additional information.
    Although this should probably be factored out into a separate class, I’ve just implied that the data-store implements the NSStreamDelegate callbacks accordingly.

    After the headers you’d just pass through the rest of the file…

    /// Scribble of another helper class that can be used whenever one needs to consume a stream for its aggregate data:
    @interface _StreamConsumer : NSObject <NSStreamDelegate> {
        NSInputStream *_stream;
        DataExportHandler _handler;
        NSMutableData *_data;
    }
    
    // initiate the data, set itself as the stream’s delegate, open and schedule the stream in a runloop.
    - (id)initWithInputStream:(NSInputStream *)stream resultHandler:(DataExportHandler)handler;
    
    // forward the close to the stream
    - (void)close;
    
    // Implementation of the stream delegate callbacks can be more or less copy-pasted from Apple’s Stream Programming Guide (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Streams.html)
    @end
    
    @implementation DataStore (FileExport)
    
    - (id)asynchronouslyExportDataForObjectWithIdentifier:(id)someUniqueIdentifier resultHandler:(void (^)(NSData *fileData, NSError *exportError))
    {
        NSParameterAssert(handler);
        handler = [handler copy];
    
        NSError *setupError;
        NSInputStream *exportStream = [self exportStreamForObjectWithIdentifier:someUniqueIdentifier error:&setupError];
        if (!exportStream)
        {
            dispatch_async(dispatch_get_current_queue(), ^{
                handler(nil, setupError);
            });
    
            return nil;
        }
    
        _StreamConsumer *helper = [[_StreamConsumer alloc] initWithStream:exportStream resultHandler:handler];
    
        return helper;
    }
    
    - (void)abortAsynchronousExportWithToken:(id)exportToken
    {
        [exportToken close];
    }
    
    - (NSInputStream *)exportStreamForObjectWithIdentifier:(id)identifier error:(NSError * __autoreleasing*)error
    {
        // do your thing to retrieve the URL to the actual data-file and then:
        NSInputStream *rawDataStream = [NSInputStream inputStreamWithURL:rawFileURL];
    
        if (!rawDataStream)
        {
            // populate the error in a meaningful way
            return nil;
        }
    
        CFReadStream cfExportStream;
        CFWriteStream cfBuffer;
        CFStreamCreateBoundPair(kCFAllocatorDefault, &cfExportStream, &cfBuffer, someValueYouHaveTuned);
    
        if (!cfExportStream || !cfBuffer)
        {
            // error population
            return nil;
        }
    
        NSInputStream *exportStream = (__bridge_transfer NSInputStream *)cfExportStream;
    
        // HACKITY HACK: In reality, you’d want this stuff separated!
        // For the sake of simplicity, take the responsibility for that ourselves
        _exportBuffer = (__bridge_transfer NSOutputStream *)cfBuffer;
    
        rawDataStream.delegate = self;
        [rawDataStream open];
        [rawDataStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunloopDefaultMode];
        // END: HACKITY HACK
    
        return exportStream;
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to change some text values inside an after effect project file that
I need to change the background of the header and also the rows of
I need to change the below code to pass a file to a django
File header contains the all data about the file &mdash ; metadata. I want
Does anyone know of a way to change the height of a Page Header
I need change background of all text that have two spaces from the start
i need change color of SystemTray.ProgressIndicator color, how i can do it? I tried
I have database, and in one the tables I need change the values of
I use Pjax with tutorial from http://railscasts.com/episodes/294-playing-with-pjax?view=comments I don't need change url and this
Need to change li position by clicking to move up or to move down.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.