Index: src/OFData.h ================================================================== --- src/OFData.h +++ src/OFData.h @@ -241,20 +241,18 @@ * @return An initialized OFData */ - (instancetype)initWithContentsOfFile: (OFString *)path; #endif -#if defined(OF_HAVE_FILES) || defined(OF_HAVE_SOCKETS) /*! * @brief Initializes an already allocated OFData with an item size of 1, * containing the data of the specified URL. * * @param URL The URL to the contents for the OFData * @return A new autoreleased OFData */ - (instancetype)initWithContentsOfURL: (OFURL *)URL; -#endif /*! * @brief Initializes an already allocated OFData with an item size of 1, * containing the data of the string representation. * Index: src/OFData.m ================================================================== --- src/OFData.m +++ src/OFData.m @@ -20,19 +20,21 @@ #include #include #import "OFData.h" #import "OFData+Private.h" -#import "OFString.h" +#import "OFDictionary.h" #ifdef OF_HAVE_FILES # import "OFFile.h" # import "OFFileManager.h" #endif -#import "OFURL.h" -#import "OFDictionary.h" -#import "OFXMLElement.h" +#import "OFStream.h" +#import "OFString.h" #import "OFSystemInfo.h" +#import "OFURL.h" +#import "OFURLHandler.h" +#import "OFXMLElement.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFInvalidServerReplyException.h" #import "OFOutOfMemoryException.h" @@ -231,33 +233,52 @@ return self; } #endif -#if defined(OF_HAVE_FILES) || defined(OF_HAVE_SOCKETS) - (instancetype)initWithContentsOfURL: (OFURL *)URL { - void *pool; - OFString *scheme; - - pool = objc_autoreleasePoolPush(); - - scheme = [URL scheme]; - -# ifdef OF_HAVE_FILES - if ([scheme isEqual: @"file"]) - self = [self initWithContentsOfFile: - [URL fileSystemRepresentation]]; - else -# endif - @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; - - objc_autoreleasePoolPop(pool); + self = [super init]; + + @try { + void *pool = objc_autoreleasePoolPush(); + OFURLHandler *URLHandler; + OFStream *stream; + size_t pageSize; + unsigned char *buffer; + + if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil) + @throw [OFUnsupportedProtocolException + exceptionWithURL: URL]; + + _itemSize = 1; + _count = 0; + + pageSize = [OFSystemInfo pageSize]; + buffer = [self allocMemoryWithSize: pageSize]; + + while (![stream isAtEndOfStream]) { + size_t length = [stream readIntoBuffer: buffer + length: pageSize]; + + if (SIZE_MAX - _count < length) + @throw [OFOutOfRangeException exception]; + + _items = [self resizeMemory: _items + size: _count + length]; + memcpy(_items + _count, buffer, length); + _count += length; + } + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + [self release]; + @throw e; + } return self; } -#endif - (instancetype)initWithStringRepresentation: (OFString *)string { self = [super init]; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -825,11 +825,10 @@ */ - (instancetype)initWithContentsOfFile: (OFString *)path encoding: (of_string_encoding_t)encoding; #endif -#if defined(OF_HAVE_FILES) || defined(OF_HAVE_SOCKETS) /*! * @brief Initializes an already allocated OFString with the contents of the * specified URL. * * If the URL's scheme is file, it tries UTF-8 encoding. @@ -851,11 +850,10 @@ * @param encoding The encoding to assume * @return An initialized OFString */ - (instancetype)initWithContentsOfURL: (OFURL *)URL encoding: (of_string_encoding_t)encoding; -#endif /*! * @brief Writes the OFString into the specified C string with the specified * encoding. * Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -1062,40 +1062,35 @@ return self; } #endif -#if defined(OF_HAVE_FILES) || defined(OF_HAVE_SOCKETS) - (instancetype)initWithContentsOfURL: (OFURL *)URL { return [self initWithContentsOfURL: URL encoding: OF_STRING_ENCODING_AUTODETECT]; } - (instancetype)initWithContentsOfURL: (OFURL *)URL encoding: (of_string_encoding_t)encoding { - void *pool = objc_autoreleasePoolPush(); - OFString *scheme = [URL scheme]; - -# ifdef OF_HAVE_FILES - if ([scheme isEqual: @"file"]) { - if (encoding == OF_STRING_ENCODING_AUTODETECT) - encoding = OF_STRING_ENCODING_UTF_8; - - self = [self - initWithContentsOfFile: [URL fileSystemRepresentation] - encoding: encoding]; - } else -# endif - @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; - - objc_autoreleasePoolPop(pool); + @try { + void *pool = objc_autoreleasePoolPush(); + OFData *data = [OFData dataWithContentsOfURL: URL]; + + self = [self initWithCString: [data items] + encoding: encoding + length: [data count]]; + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + [self release]; + @throw e; + } return self; } -#endif - (instancetype)initWithSerialization: (OFXMLElement *)element { @try { void *pool = objc_autoreleasePoolPush();