5 // Created by Alexander Strange on Thu Feb 27 2003.
6 // Copyright (c) 2003 __MyCompanyName__. All rights reserved.
9 #import "ITByteStream.h"
11 // TODO: Add NSCopying/NSCoding support. Blocking reads (how would this work? NSConditionLock?)
13 @implementation ITByteStream
16 if (self == [super init])
18 data = [[NSMutableData alloc] init];
19 lock = [[NSLock alloc] init];
25 -(id) initWithDelegate:(id)d
27 if (self == [super init])
29 data = [[NSMutableData alloc] init];
30 lock = [[NSLock alloc] init];
31 delegate = [d retain];
36 -(id) initWithStream:(ITByteStream*)stream delegate:(id)d
38 if (self == [super init])
40 data = [stream->data copy];
41 lock = [[NSLock alloc] init];
42 delegate = [d retain];
47 -(oneway void) dealloc
56 -(void) setDelegate:(id <ITByteStreamDelegate>)d
59 delegate = [d retain];
62 -(int) availableDataLength
71 -(NSData*) readDataOfLength:(int)length
74 NSRange range = {0, length};
76 ret = [data subdataWithRange:range];
77 [data replaceBytesInRange:range withBytes:nil length:0];
82 -(NSData*) readAllData
86 ret = [data autorelease];
87 data = [[NSMutableData alloc] init];
92 -(void) writeData:(in NSData*)_data
95 [data appendData:_data];
97 [delegate newDataAdded:self];
100 -(void) writeBytes:(in char *)b len:(long)length
103 [data appendBytes:b length:length];
105 [delegate newDataAdded:self];
118 -(void) shortenData:(long)length
120 NSRange range = {0, length};
121 [data replaceBytesInRange:range withBytes:nil length:0];