Permalink
Browse files

Add NSData, NSObject, and NSOutputStream categories. Add iOS-specific…

… code.
  • Loading branch information...
1 parent 7a5b7cc commit ce9a419aca1523366e42ccdd7c8a3485b3670969 @ldandersen committed Jun 25, 2012
@@ -0,0 +1,33 @@
+//
+// NSData+STAdditions.h
+//
+// Created by Buzz Andersen on 12/29/09.
+// Copyright 2011 System of Touch. All rights reserved.
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSData (STAdditions)
+
+#pragma mark Base 64 Encoding
+- (id)initWithBase64String:(NSString *)string;
+
+#pragma mark HMAC
+- (NSData *)hmacSHA1DataValueWithKey:(NSData *)keyData;
+
+#pragma mark Hex Strings
+- (NSString *)hexString;
+
+#pragma mark UTF8
+- (NSString *)UTF8String;
+
+@end
+
+
+@interface NSMutableData (STAdditions)
+
+- (void)appendUTF8String:(NSString *)inString;
+- (void)appendUTF8StringWithFormat:(NSString *)inString, ...;
+- (void)appendString:(NSString *)inString withEncoding:(NSStringEncoding)inEncoding;
+
+@end
@@ -0,0 +1,59 @@
+//
+// NSData+STAdditions.m
+//
+// Created by Buzz Andersen on 12/29/09.
+// Copyright 2011 System of Touch. All rights reserved.
+//
+
+#import "STUtils-Private.h"
+
+
+@implementation NSData (STAdditions)
+
+#pragma mark UTF8
+
+- (NSString *)UTF8String;
+{
+ return [[[NSString alloc] initWithBytes:[self bytes] length:[self length] encoding:NSUTF8StringEncoding] autorelease];
+}
+
+@end
+
+
+@implementation NSMutableData (STAdditions)
+
+- (void)appendUTF8StringWithFormat:(NSString *)inString, ...;
+{
+ va_list args;
+ va_start(args, inString);
+
+ NSString *formattedString = [[NSString alloc] initWithFormat:inString arguments:args];
+ [self appendUTF8String:formattedString];
+ [formattedString release];
+
+ va_end(args);
+}
+
+- (void)appendUTF8String:(NSString *)inString;
+{
+ [self appendString:inString withEncoding:NSUTF8StringEncoding];
+}
+
+- (void)appendString:(NSString *)inString withEncoding:(NSStringEncoding)inEncoding;
+{
+ NSUInteger byteLength = [inString lengthOfBytesUsingEncoding:inEncoding];
+
+ if (!byteLength) {
+ return;
+ }
+
+ char *buffer = malloc(byteLength);
+
+ if ([inString getBytes:buffer maxLength:byteLength usedLength:NULL encoding:inEncoding options:NSStringEncodingConversionExternalRepresentation range:NSMakeRange(0,byteLength) remainingRange:NULL]) {
+ [self appendBytes:buffer length:byteLength];
+ }
+
+ free(buffer);
+}
+
+@end
@@ -0,0 +1,25 @@
+//
+// NSObject+STAdditions.h
+//
+// Created by Buzz Andersen on 12/29/09.
+// Copyright 2011 System of Touch. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSObject (STAdditions)
+
+// Selectors
+- (id)performSelectorOnMainThread:(SEL)selector withArguments:(NSArray *)arguments waitUntilDone:(BOOL)waitUntilDone;
+
+- (id)performSelectorIfResponds:(SEL)selector;
+- (id)performSelectorIfResponds:(SEL)selector withObject:(id)obj;
+- (id)performSelectorIfResponds:(SEL)selector withObject:(id)obj1 withObject:(id)obj2;
+
+- (void)performSelectorOnRunloopCycle:(SEL)selector;
+
+// URL Parameter Strings
+- (NSString *)URLParameterStringValue;
+
+@end
@@ -0,0 +1,146 @@
+//
+// NSObject+STAdditions.m
+//
+// Created by Buzz Andersen on 12/29/09.
+// Copyright 2011 System of Touch. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#import "STUtils.h"
+
+
+@implementation NSObject (STAdditions)
+
+#pragma mark Selectors
+
+- (id)performSelectorOnMainThread:(SEL)selector withArguments:(NSArray *)arguments waitUntilDone:(BOOL)waitUntilDone;
+{
+ if (![self respondsToSelector:selector]) {
+ return nil;
+ }
+
+ NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
+ [invocation setSelector:selector];
+ [invocation setTarget:self];
+ [invocation retainArguments];
+
+ for (int currentIndex; currentIndex < arguments.count; currentIndex++) {
+ id currentArgument = [arguments objectAtIndex:currentIndex];
+ [invocation setArgument:&currentArgument atIndex:currentIndex];
+ }
+
+ [invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:waitUntilDone];
+
+ if (!waitUntilDone || ![signature methodReturnLength]) {
+ return nil;
+ }
+
+ char *returnType = (char *)[signature methodReturnType];
+
+ // If the return type is an object, return it.
+ if (strchr(returnType, '@')) {
+ id returnValue;
+ [invocation getReturnValue:&returnValue];
+
+ // Add the returned object to the autorelease pool on
+ // this thread, if we're on a secondary thread (since
+ // theoretically it could be autoreleased on the main
+ // thread).
+ if (![NSThread isMainThread]) {
+ [[returnValue retain] autorelease];
+ }
+
+ return returnValue;
+ }
+
+ // If the return type is not an object, box it
+ // up and return it as an NSValue.
+ int length = [signature methodReturnLength];
+ void *returnBuffer = (void *)malloc(length);
+ [invocation getReturnValue:returnBuffer];
+
+ NSValue *returnValue = [NSValue value:returnBuffer withObjCType:returnType];
+
+ free(returnBuffer);
+
+ return returnValue;
+}
+
+- (id)performSelectorIfResponds:(SEL)selector;
+{
+ if ([self respondsToSelector:selector]) {
+ return [self performSelector:selector];
+ }
+
+ return nil;
+}
+
+- (id)performSelectorIfResponds:(SEL)selector withObject:(id)obj;
+{
+ if ([self respondsToSelector:selector]) {
+ return [self performSelector:selector withObject:obj];
+ }
+
+ return nil;
+}
+
+- (id)performSelectorIfResponds:(SEL)selector withObject:(id)obj1 withObject:(id)obj2;
+{
+ if ([self respondsToSelector:selector]) {
+ return [self performSelector:selector withObject:obj1 withObject:obj2];
+ }
+
+ return nil;
+}
+
+- (void)performSelectorOnRunloopCycle:(SEL)selector;
+{
+ [self performSelector:selector withObject:nil afterDelay:0.0];
+}
+
+- (void)performSelectorOnRunloopCycle:(SEL)selector withObject:(id)obj1;
+{
+ [self performSelector:selector withObject:obj1 afterDelay:0.0];
+}
+
+#pragma mark URL Parameter Strings
+
+- (NSString *)URLParameterStringValue;
+{
+ NSString *stringValue = nil;
+
+ if ([self isKindOfClass: [NSString class]]) {
+ stringValue = (NSString *)self;
+ }
+ else if ([self isKindOfClass: [NSNumber class]]) {
+ stringValue = [(NSNumber *)self stringValue];
+ }
+ else if ([self isKindOfClass: [NSDate class]]) {
+ stringValue = [(NSDate *)self HTTPTimeZoneHeaderString];
+ }
+
+ return stringValue;
+}
+
+@end
@@ -0,0 +1,40 @@
+//
+// NSOutputStream+STAdditions.h
+//
+// Created by Buzz Andersen on 4/10/12.
+// Copyright (c) 2012 System of Touch. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+#import <Foundation/Foundation.h>
+
+@interface NSOutputStream (STAdditions)
+
+- (void)writeUTF8StringWithFormat:(NSString *)inString, ...;
+- (void)writeUTF8String:(NSString *)inUTF8String;
+- (void)writeString:(NSString *)inString usingEncoding:(NSStringEncoding)inEncoding;
+- (void)writeData:(NSData *)inData;
+- (void)writeFileDataAtPath:(NSString *)inPath withBufferSize:(NSInteger)inBufferSize;
+
+@end
@@ -0,0 +1,88 @@
+//
+// NSOutputStream+STAdditions.m
+// Hipflask
+//
+// Created by Buzz Andersen on 4/10/12.
+// Copyright (c) 2012 System of Touch. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#import "STUtils.h"
+
+
+@implementation NSOutputStream (STAdditions)
+
+- (void)writeUTF8StringWithFormat:(NSString *)inString, ...;
+{
+ va_list args;
+ va_start(args, inString);
+
+ NSString *formattedString = [[NSString alloc] initWithFormat:inString arguments:args];
+ [self writeUTF8String:formattedString];
+ [formattedString release];
+
+ va_end(args);
+}
+
+- (void)writeUTF8String:(NSString *)inUTF8String;
+{
+ [self writeString:inUTF8String usingEncoding:NSUTF8StringEncoding];
+}
+
+- (void)writeString:(NSString *)inString usingEncoding:(NSStringEncoding)inEncoding;
+{
+ NSUInteger byteLength = [inString lengthOfBytesUsingEncoding:inEncoding];
+
+ if (!byteLength) {
+ return;
+ }
+
+ char *buffer = malloc(byteLength);
+
+ if ([inString getBytes:buffer maxLength:byteLength usedLength:NULL encoding:inEncoding options:NSStringEncodingConversionExternalRepresentation range:NSMakeRange(0,byteLength) remainingRange:NULL]) {
+ [self write:(const uint8_t *)buffer maxLength:byteLength];
+ }
+
+ free(buffer);
+}
+
+- (void)writeData:(NSData *)inData;
+{
+ [self write:[inData bytes] maxLength:inData.length];
+}
+
+- (void)writeFileDataAtPath:(NSString *)inPath withBufferSize:(NSInteger)inBufferSize;
+{
+ if (!inPath.length || ![[NSFileManager defaultManager] fileExistsAtPath:inPath]) {
+ return;
+ }
+
+ NSInputStream *fileInputStream = [NSInputStream inputStreamWithFileAtPath:inPath];
+ while ([fileInputStream hasBytesAvailable]) {
+ uint8_t fileBuffer[inBufferSize];
+ [fileInputStream read:fileBuffer maxLength:inBufferSize];
+ [self write:fileBuffer maxLength:inBufferSize];
+ }
+}
+
+@end
Oops, something went wrong.

0 comments on commit ce9a419

Please sign in to comment.