Index: src/OFDataArray.h ================================================================== --- src/OFDataArray.h +++ src/OFDataArray.h @@ -15,10 +15,11 @@ */ #import "OFObject.h" @class OFString; +@class OFURL; /** * \brief A class for storing arbitrary data in an array. * * If you plan to store large hunks of data, you should consider using @@ -51,10 +52,19 @@ * \param path The path of the file * \return A new autoreleased OFDataArray */ + dataArrayWithContentsOfFile: (OFString*)path; +/** + * Creates a new OFDataArray with an item size of 1, containing the data of the + * specified URL. + * + * \param URL The URL to the contents for the OFDataArray + * \return A new autoreleased OFDataArray + */ ++ dataArrayWithContentsOfURL: (OFURL*)URL; + /** * Creates a new OFDataArray with an item size of 1, containing the data of the * Base64-encoded string. * * \param string The string with the Base64-encoded data @@ -78,10 +88,19 @@ * \param path The path of the file * \return An initialized OFDataArray */ - initWithContentsOfFile: (OFString*)path; +/** + * Initializes an already allocated OFDataArray with an item size of 1, + * containing the data of the specified URL. + * + * \param URL The URL to the contents for the OFDataArray + * \return A new autoreleased OFDataArray + */ +- initWithContentsOfURL: (OFURL*)URL; + /** * Initializes an already allocated OFDataArray with an item size of 1, * containing the data of the Base64-encoded string. * * \param string The string with the Base64-encoded data Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -21,11 +21,15 @@ #include #import "OFDataArray.h" #import "OFString.h" #import "OFFile.h" +#import "OFURL.h" +#import "OFHTTPRequest.h" +#import "OFAutoreleasePool.h" +#import "OFHTTPRequestFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" #import "OFNotImplementedException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" @@ -47,10 +51,15 @@ + dataArrayWithContentsOfFile: (OFString*)path { return [[[self alloc] initWithContentsOfFile: path] autorelease]; } + ++ dataArrayWithContentsOfURL: (OFURL*)URL +{ + return [[[self alloc] initWithContentsOfURL: URL] autorelease]; +} + dataArrayWithBase64EncodedString: (OFString*)string { return [[[self alloc] initWithBase64EncodedString: string] autorelease]; } @@ -114,10 +123,42 @@ @throw e; } return self; } + +- initWithContentsOfURL: (OFURL*)URL +{ + OFAutoreleasePool *pool; + OFHTTPRequest *request; + OFHTTPRequestResult *result; + Class c; + + c = isa; + [self release]; + + pool = [[OFAutoreleasePool alloc] init]; + + if ([[URL scheme] isEqual: @"file"]) { + self = [[c alloc] initWithContentsOfFile: [URL path]]; + [pool release]; + return self; + } + + request = [OFHTTPRequest requestWithURL: URL]; + result = [request perform]; + + if ([result statusCode] != 200) + @throw [OFHTTPRequestFailedException + newWithClass: [request class] + HTTPRequest: request + statusCode: [result statusCode]]; + + self = [[result data] retain]; + [pool release]; + return self; +} - initWithBase64EncodedString: (OFString*)string { self = [super init]; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -707,11 +707,10 @@ OFHTTPRequestResult *result; Class c; c = isa; [self release]; - self = nil; pool = [[OFAutoreleasePool alloc] init]; if ([[URL scheme] isEqual: @"file"]) { self = [[c alloc] initWithContentsOfFile: [URL path]