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:
http://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