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

  • Home
  • SEARCH
  • 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 7158045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T12:59:44+00:00 2026-05-28T12:59:44+00:00

My app downloads a file in UTF-8 format, which is too large to read

  • 0

My app downloads a file in UTF-8 format, which is too large to read using the NSString initWithContentsOfFile method. The problem I have is that the NSFileHandle readDataOfLength method reads a specified number of bytes, and I may end up only reading part of a UTF-8 character. What is the best solution here?

LATER:

Let it be recorded in the ship’s log that the following code works:

    NSData *buf = [NSData dataWithContentsOfFile:path
                                      options:NSDataReadingMappedIfSafe
                                        error:nil];

NSString *data = [[[NSString alloc] 
                   initWithBytesNoCopy:(void *)buf.bytes 
                   length:buf.length 
                   encoding:NSUTF8StringEncoding 
                   freeWhenDone:NO] autorelease];

My main problem was actually to do with the encoding, not the task of reading the file.

  • 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-05-28T12:59:45+00:00Added an answer on May 28, 2026 at 12:59 pm

    You can use NSData +dataWithContentsOfFile:options:error: with the NSDataReadingMappedIfSafe option to map your file to memory rather than loading it. So that’ll use the virtual memory manager in iOS to ensure that bits of the file are swapped in and out of RAM in the same way that a desktop OS handles its on-disk virtual memory file. So you don’t need enough RAM to keep the entire file in memory at once, you just need the file to be small enough to fit in the processor’s address space (so, gigabytes). You’ll get an object that acts exactly like a normal NSData, which should save you most of the hassle related to using an NSFileHandle and manually streaming.

    You’ll probably then need to convert portions to NSString since you can realistically expect that to convert from UTF-8 to another format (though it might not; it’s worth having a go with -initWithData:encoding: and seeing whether NSString is smart enough just to keep a reference to the original data and to expand from UTF-8 on demand), which I think is what your question is really getting at.

    I’d suggest you use -initWithBytes:length:encoding: to convert a reasonable number of bytes to a string. You can then use -lengthOfBytesUsingEncoding: to find out how many bytes it actually made sense of and advance your read pointer appropriately. It’s a safe assumption that NSString will discard any part characters at the end of the bytes you provide.

    EDIT: so, something like:

    // map the file, rather than loading it
    NSData *data = [NSData dataWithContentsOfFile:...whatever...
                             options:NSDataReadingMappedIfSafe
                             error:&youdDoSomethingSafeHere];
    
    // we'll maintain a read pointer to our current location in the data
    NSUinteger readPointer = 0;
    
    // continue while data remains
    while(readPointer < [data length])
    {
        // work out how many bytes are remaining
        NSUInteger distanceToEndOfData = [data length] - readPointer;
    
        // grab at most 16kb of them, being careful not to read too many
        NSString *newPortion = 
             [[NSString alloc] initWithBytes:(uint8_t *)[data bytes] + readPointer
                     length:distanceToEndOfData > 16384 ? 16384 : distanceToEndOfData
                     encoding:NSUTF8StringEncoding];
    
        // do whatever we want with the string
        [self doSomethingWithFragment:newPortion];
    
        // advance our read pointer by the number of bytes actually read, and
        // clean up
        readPointer += [newPortion lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
        [newPortion release];
    }
    

    Of course, an implicit assumption is that all UTF-8 encodings are unique, which I have to admit not to being knowledgable enough to say for absolute certain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little Updater in my mac app, which automatically downloads a dmg-file
I have an app that downloads a very large file (over 50MB). Unfortunately I'm
I have an app in which I am trying to download a file. I
I have downloaded the GeoLiteCountry CSV file from Maxmind - http://www.maxmind.com/app/geolitecountry . Using the
I have a XML File in the following format: <?xml version='1.0' encoding='UTF-8'?> <entry xmlns='http://www.w3.org/2005/Atom'
I'm writing an iPhone app which downloads a tar-gzipped file from a Webserver, then
I have an app that downloads a file from an FTP server upon tapping
My app downloads a small image file from a remote server and I am
I'm having few doubts on implementing file downloads. I'm creating an app where I
I want my app to catch downloads of a particular kind of file. Here's

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.