PageRenderTime 26ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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/*
 6 
 7 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.
 8 
 9 */
10
11#import "JMROutputStream.h"
12
13
14@implementation JMROutputStream
15
16+(JMROutputStream *)outputStreamToFileAtPath:(NSString *)path {
17    return [[[JMROutputStream alloc] initToFileAtPath:path] autorelease];
18}
19
20-(id)initToFileAtPath:(NSString *)path {
21    if (self = [super init]) {
22        _stream = [[NSOutputStream outputStreamToFileAtPath:path append:NO] retain];
23        [_stream open];
24    }
25    
26    return self;
27}
28
29+(JMROutputStream *)outputStreamToMemory {
30    return [[[JMROutputStream alloc] initToMemory] autorelease];
31}
32-(id)initToMemory {
33    if (self = [super init]) {
34        _stream = [[NSOutputStream outputStreamToMemory] retain];
35        [_stream open];
36    }
37    
38    return self;
39}
40
41-(void)writeString:(NSString *)string {
42    NSData *data;
43    
44    if (!string || [string length]==0)
45        return;
46    
47    data = [string dataUsingEncoding:[NSString defaultCStringEncoding]];
48    [_stream write:[data bytes] maxLength:[data length]];
49}
50
51-(void)writeLine:(NSString *)string {
52    [self writeString:string];
53    [self writeString:@"\n"];
54}
55
56-(NSString *)stringValue {
57    NSData *data = (NSData *)[_stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
58    NSString *string = nil;
59    
60    if (data)
61        string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[NSString defaultCStringEncoding]];
62
63    return string;
64}
65
66-(void)dealloc {
67    [_stream close];
68    [_stream release];
69    
70    [super dealloc];
71}
72
73@end