Index: src/OFConstantString.m ================================================================== --- src/OFConstantString.m +++ src/OFConstantString.m @@ -662,10 +662,11 @@ return [self decomposedStringWithCompatibilityMapping]; } #endif +#ifdef OF_HAVE_FILES - (void)writeToFile: (OFString *)path { [self finishInitialization]; [self writeToFile: path]; @@ -677,10 +678,27 @@ [self finishInitialization]; [self writeToFile: path encoding: encoding]; } +#endif + +- (void)writeToURL: (OFURL *)URL +{ + [self finishInitialization]; + + [self writeToURL: URL]; +} + +- (void)writeToURL: (OFURL *)URL + encoding: (of_string_encoding_t)encoding +{ + [self finishInitialization]; + + [self writeToURL: URL + encoding: encoding]; +} #ifdef OF_HAVE_BLOCKS - (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block { [self finishInitialization]; Index: src/OFData.h ================================================================== --- src/OFData.h +++ src/OFData.h @@ -306,12 +306,19 @@ * * @param path The path of the file to write to */ - (void)writeToFile: (OFString *)path; #endif + +/*! + * @brief Writes the OFData to the specified URL. + * + * @param URL The URL to write to + */ +- (void)writeToURL: (OFURL *)URL; @end OF_ASSUME_NONNULL_END #import "OFMutableData.h" #import "OFData+CryptoHashing.h" #import "OFData+MessagePackValue.h" Index: src/OFData.m ================================================================== --- src/OFData.m +++ src/OFData.m @@ -598,10 +598,26 @@ } @finally { [file release]; } } #endif + +- (void)writeToURL: (OFURL *)URL +{ + void *pool = objc_autoreleasePoolPush(); + OFURLHandler *URLHandler; + OFStream *stream; + + if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil) + @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; + + stream = [URLHandler openItemAtURL: URL + mode: @"w"]; + [stream writeData: self]; + + objc_autoreleasePoolPop(pool); +} - (OFXMLElement *)XMLElementBySerializing { void *pool; OFXMLElement *element; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -1216,10 +1216,26 @@ */ - (void)writeToFile: (OFString *)path encoding: (of_string_encoding_t)encoding; #endif +/*! + * @brief Writes the string to the specified URL using UTF-8 encoding. + * + * @param URL The URL to write to + */ +- (void)writeToURL: (OFURL *)URL; + +/*! + * @brief Writes the string to the specified URL using the specified encoding. + * + * @param URL The URL to write to + * @param encoding The encoding to use to write the string to the URL + */ +- (void)writeToURL: (OFURL *)URL + encoding: (of_string_encoding_t)encoding; + #ifdef OF_HAVE_BLOCKS /*! * Enumerates all lines in the receiver using the specified block. * * @brief block The block to call for each line Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -41,10 +41,11 @@ #ifdef OF_HAVE_FILES # import "OFFile.h" # import "OFFileManager.h" #endif #import "OFURL.h" +#import "OFURLHandler.h" #import "OFXMLElement.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" @@ -2898,10 +2899,34 @@ encoding: encoding]; objc_autoreleasePoolPop(pool); } #endif + +- (void)writeToURL: (OFURL *)URL +{ + [self writeToURL: URL + encoding: OF_STRING_ENCODING_UTF_8]; +} + +- (void)writeToURL: (OFURL *)URL + encoding: (of_string_encoding_t)encoding +{ + void *pool = objc_autoreleasePoolPush(); + OFURLHandler *URLHandler; + OFStream *stream; + + if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil) + @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; + + stream = [URLHandler openItemAtURL: URL + mode: @"w"]; + [stream writeString: self + encoding: encoding]; + + objc_autoreleasePoolPop(pool); +} #ifdef OF_HAVE_BLOCKS - (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block { void *pool = objc_autoreleasePoolPush();