/JMROutputStream.m

http://rtm2cocoa.googlecode.com/ · Objective C · 73 lines · 46 code · 19 blank · 8 comment · 5 complexity · 67180b7c95456312eadcb786d45c7125 MD5 · raw file

  1. //
  2. // JMROutputStream.m
  3. // Excelsior
  4. /*
  5. This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  6. */
  7. #import "JMROutputStream.h"
  8. @implementation JMROutputStream
  9. +(JMROutputStream *)outputStreamToFileAtPath:(NSString *)path {
  10. return [[[JMROutputStream alloc] initToFileAtPath:path] autorelease];
  11. }
  12. -(id)initToFileAtPath:(NSString *)path {
  13. if (self = [super init]) {
  14. _stream = [[NSOutputStream outputStreamToFileAtPath:path append:NO] retain];
  15. [_stream open];
  16. }
  17. return self;
  18. }
  19. +(JMROutputStream *)outputStreamToMemory {
  20. return [[[JMROutputStream alloc] initToMemory] autorelease];
  21. }
  22. -(id)initToMemory {
  23. if (self = [super init]) {
  24. _stream = [[NSOutputStream outputStreamToMemory] retain];
  25. [_stream open];
  26. }
  27. return self;
  28. }
  29. -(void)writeString:(NSString *)string {
  30. NSData *data;
  31. if (!string || [string length]==0)
  32. return;
  33. data = [string dataUsingEncoding:[NSString defaultCStringEncoding]];
  34. [_stream write:[data bytes] maxLength:[data length]];
  35. }
  36. -(void)writeLine:(NSString *)string {
  37. [self writeString:string];
  38. [self writeString:@"\n"];
  39. }
  40. -(NSString *)stringValue {
  41. NSData *data = (NSData *)[_stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  42. NSString *string = nil;
  43. if (data)
  44. string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[NSString defaultCStringEncoding]];
  45. return string;
  46. }
  47. -(void)dealloc {
  48. [_stream close];
  49. [_stream release];
  50. [super dealloc];
  51. }
  52. @end