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 3226878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:30:31+00:00 2026-05-17T16:30:31+00:00

I am trying to make a customized input stream based off of the one

  • 0

I am trying to make a customized input stream based off of the one by Dave DeLong here that also allows for reading data from a server via NSURL. So far, I have this approach, which works fine for local files:

@interface RJRStreamReader : NSObject {
    @private
    NSFileHandle *fileHandle;
    NSStringEncoding encoding;
    NSString *lineDelimiter;
    unsigned long long currentOffset;
    unsigned long long totalFileLength;
    int chunkSize;
}

@property(readwrite, assign) NSStringEncoding encoding;
@property(readwrite, assign) unsigned long long currentOffset;
@property(readwrite, copy) NSString *lineDelimiter;
@property(readwrite, assign) int chunkSize;
@property(readonly) unsigned long long totalFileLength;

-(id) initWithLocalFile:(NSString *) fileName;

-(id) initWithURL:(NSURL *) remoteURL;

-(id) initWithFileHandle:(NSFileHandle *) fh;

-(NSString *) readToEnd;

-(NSString *) readLine;

/**
 @summary Reads a block of bytes from a stream
 @param blockLen the number of bytes to read
 @returns a string containing the data from the bytes read
 */
-(NSString *) readBlock:(int) blockLen;

@end

And my implementation:

@implementation RJRStreamReader

@synthesize currentOffset, lineDelimiter, encoding, chunkSize, totalFileLength;

-(id) initWithLocalFile:(NSString *)fileName
{
    if (self = [super init])
    {
        fileHandle = [NSFileHandle fileHandleForReadingAtPath:fileName];
        if (fileHandle == nil)
        {
            [self release];
            return nil;
        }

        chunkSize = 10;
        encoding = NSUTF8StringEncoding;
        lineDelimiter = [[NSString alloc] initWithString:@"\n"];
        [fileHandle retain];
        currentOffset = 0ULL;
        [fileHandle seekToEndOfFile];
        totalFileLength = [fileHandle offsetInFile];
    }

    return self;
}

-(id) initWithURL:(NSURL *)remoteURL
{
    if (self = [super init])
    {
        NSError *err = nil;
        fileHandle = [NSFileHandle fileHandleForReadingFromURL:remoteURL error:&err];
        if (err)
        {
            NSLog(@"Error occurred, aborting. Details: %@", err);
            [self release];
            return nil;
        }

        chunkSize = 10;
        encoding = NSUTF8StringEncoding;
        lineDelimiter = [[NSString alloc] initWithString:@"\n"];
        [fileHandle retain];
        currentOffset = 0ULL;
        [fileHandle seekToEndOfFile];
        totalFileLength = [fileHandle offsetInFile];
    }

    return self;
}

-(id) initWithFileHandle:(NSFileHandle *) fh
{
    if (self = [super init])
    {
        fileHandle = fh;
        if (!fh)
        {
            [self release];
            [NSException raise:@"FH cannot be nil!" format:@"FH cannot be nil!"];
            return nil;
        }

        chunkSize = 10;
        encoding = NSUTF8StringEncoding;
        lineDelimiter = [[NSString alloc] initWithString:@"\n"];
        [fileHandle retain];
        currentOffset = 0ULL;
        [fileHandle seekToEndOfFile];
        totalFileLength = [fileHandle offsetInFile];
    }

    return self;
}

-(NSString *) readBlock:(int)blockLen
{
    if (currentOffset >= totalFileLength) { return nil; }

    [fileHandle seekToFileOffset:currentOffset];
    NSData *data = [fileHandle readDataOfLength:blockLen];
    currentOffset += blockLen;
    [fileHandle seekToFileOffset:currentOffset];

    return [[[NSString alloc] initWithData:data encoding:encoding] autorelease];
}

-(NSString *) readLine
{
    /* 
       if you want to see the code for this method, visit this link: 
       https://stackoverflow.com/questions/3707427/how-to-read-data-from-nsfilehandle-line-by-line
       it is exactly the same
     */
}

-(NSString *) readToEnd
{
    if (currentOffset >= totalFileLength) { return nil; }

    [fileHandle seekToFileOffset:currentOffset];
    NSData *data = [fileHandle readDataToEndOfFile];
    currentOffset = totalFileLength;
    [fileHandle seekToEndOfFile];

    return [[[NSString alloc] initWithData:data encoding:encoding] autorelease];
}

-(void) dealloc
{
    [fileHandle closeFile];
    [fileHandle release];
    [lineDelimiter release];
    [super dealloc];
}

@end

The problem is, when I try to use it like this:

RJRStreamReader *stream = [[RJRStreamReader alloc] initWithURL:[NSURL URLWithString:@"http://www.stackoverflow.com/robots.txt"];
NSString *s = [stream readToEnd];

s will always be empty because the NSFileHandle returned by [NSFileHandle fileHandleForReadingFromURL:remoteURL] always seems to return nil, without any errors.

Is this a bug in my code or an undocumented feature of theirs?

Thanks

  • 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-17T16:30:32+00:00Added an answer on May 17, 2026 at 4:30 pm

    OK, this is my problem:

    According to the Documentation

    fileHandleForReadingFromURL:error:
    Returns a file handle initialized for reading the file, device, or named socket at the specified URL.

    It doesn’t read from a servers URL, which led me to this approach (I would like it for people to edit this for me, as it needs lazy loading, optimization, etc.)

    RJRStreamReader.h:

    @interface RJRStreamReader : NSObject {
        @private
        NSData *data;
        NSStringEncoding encoding;
        NSString *lineDelimiter;
        unsigned long long currentOffset;
        unsigned long long totalFileLength;
        int chunkSize;
    }
    
    @property(readwrite, assign) NSStringEncoding encoding;
    @property(readwrite, assign) unsigned long long currentOffset;
    @property(readwrite, copy) NSString *lineDelimiter;
    @property(readwrite, assign) int chunkSize;
    @property(readonly) unsigned long long totalFileLength;
    
    -(id) initWithLocalFile:(NSString *) fileName;
    
    -(id) initWithURL:(NSURL *) remoteURL;
    
    -(id) initWithFileHandle:(NSFileHandle *) fh;
    
    -(id) initWithData:(NSData *) theData;
    
    -(NSString *) readToEnd;
    
    -(NSString *) readLine;
    
    /**
     @summary Reads a block of bytes from a stream
     @param blockLen the number of bytes to read
     @returns a string containing the data from the bytes read
     */
    -(NSString *) readBlock:(int) blockLen;
    
    @end
    

    RJRStreamReader.m:

    @implementation RJRStreamReader
    
    @synthesize currentOffset, lineDelimiter, encoding, chunkSize, totalFileLength;
    
    -(id) initWithLocalFile:(NSString *)fileName
    {
        if (self = [super init])
        {
            NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:fileName];
    
            if (fileHandle == nil)
            {
                [self release];
                return nil;
            }
    
            data = [[fileHandle readDataToEndOfFile] retain];
            chunkSize = 10;
            encoding = NSUTF8StringEncoding;
            lineDelimiter = [[NSString alloc] initWithString:@"\n"];
            currentOffset = 0ULL;
            [fileHandle seekToEndOfFile];
            totalFileLength = [fileHandle offsetInFile];
            [fileHandle closeFile];
        }
    
        return self;
    }
    
    -(id) initWithURL:(NSURL *)remoteURL
    {
        if (self = [super init])
        {
            NSError *err = nil;
            NSURLResponse *resp = nil;
    
            data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:remoteURL] returningResponse:&resp error:&err];
    
            if (err)
            {
                NSLog(@"Error occurred, aborting. Details: %@", err);
                [self release];
                return nil;
            }
    
            chunkSize = 10;
            encoding = NSUTF8StringEncoding;
            lineDelimiter = [[NSString alloc] initWithString:@"\n"];
            [data retain];
            currentOffset = 0ULL;
            totalFileLength = [data length];
        }
    
        return self;
    }
    
    -(id) initWithFileHandle:(NSFileHandle *) fh
    {
        if (self = [super init])
        {
            if (!fh)
            {
                [self release];
                [NSException raise:@"FH cannot be nil!" format:@"FH cannot be nil!"];
                return nil;
            }
    
            unsigned long long pos = [fh offsetInFile];
    
            data = [[fh readDataToEndOfFile] retain];       
            chunkSize = 10;
            encoding = NSUTF8StringEncoding;
            lineDelimiter = [[NSString alloc] initWithString:@"\n"];
            currentOffset = 0ULL;
            [fh seekToEndOfFile];
            totalFileLength = [fh offsetInFile];
            [fh seekToFileOffset:pos];
        }
    
        return self;
    }
    
    -(id) initWithData:(NSData *)theData
    {
        if (self = [super init])
        {
            data = [theData retain];    
            chunkSize = 10;
            encoding = NSUTF8StringEncoding;
            lineDelimiter = [[NSString alloc] initWithString:@"\n"];
            currentOffset = 0ULL;
            totalFileLength = [data length];        
        }
    
        return self;
    }
    
    -(NSString *) readBlock:(int)blockLen
    {
        if (currentOffset >= totalFileLength) { return nil; }
    
        NSData *tmpdata = [data subdataWithRange:NSMakeRange(currentOffset, blockLen)];
        currentOffset += blockLen;
    
        return [[[NSString alloc] initWithData:tmpdata encoding:encoding] autorelease];
    }
    
    -(NSString *) readLine
    {
        if (currentOffset >= totalFileLength) { return nil; }
    
        NSData * newLineData = [lineDelimiter dataUsingEncoding:NSUTF8StringEncoding];
        //[fileHandle seekToFileOffset:currentOffset];
        NSMutableData * currentData = [[NSMutableData alloc] init];
        BOOL shouldReadMore = YES;
    
        NSAutoreleasePool * readPool = [[NSAutoreleasePool alloc] init];
        while (shouldReadMore) {
            if (currentOffset >= totalFileLength) { break; }
            NSData *chunk = [data subdataWithRange:NSMakeRange(currentOffset, chunkSize)];
            NSRange newLineRange = [chunk rangeOfData_dd:newLineData];
            if (newLineRange.location != NSNotFound) {
    
                //include the length so we can include the delimiter in the string
                chunk = [chunk subdataWithRange:NSMakeRange(0, newLineRange.location)];
                shouldReadMore = NO;
            }
            [currentData appendData:chunk];
            currentOffset += [chunk length];
        }
        [readPool release];
    
        NSString * line = [[NSString alloc] initWithData:currentData encoding:NSUTF8StringEncoding];
        [currentData release];
        return [line autorelease];
    }
    
    -(NSString *) readToEnd
    {
        if (currentOffset >= totalFileLength) { return nil; }
    
        NSData *tmpdata = [data subdataWithRange:NSMakeRange(currentOffset, totalFileLength - currentOffset)];
        currentOffset = totalFileLength;
    
        return [[[NSString alloc] initWithData:tmpdata encoding:encoding] autorelease];
    }
    
    -(void) dealloc
    {
        [data release];
        [lineDelimiter release];
        [super dealloc];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a make generic select control that I can dynamically add elements
Trying to make a MySQL-based application support MS SQL, I ran into the following
I'm trying to make a context menu for a control that is linked to
I am trying to make a div, that when you click it turns into
I am trying to make a JTable that has column spans available. Specifically, I
Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a web service call to an HTTPS endpoint in my Silverlight
I'm trying to make the case for click-once and smart client development but my
I'm trying to make a data bound column invisible after data binding, because it
I am trying to make an Outlook 2003 add-in using Visual Studio 2008 on

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.