1 #import "ITByteStream.h"
3 // TODO: Add NSCopying/NSCoding support. Blocking reads (how would this work? NSConditionLock?)
5 @implementation ITByteStream
8 if (self == [super init]) {
9 data = [[NSMutableData alloc] init];
10 lock = [[NSLock alloc] init];
16 - (id)initWithDelegate:(id <ITDataReceiver>)d {
17 if (self == [super init]) {
18 data = [[NSMutableData alloc] init];
19 lock = [[NSLock alloc] init];
20 delegate = [d retain];
25 - (id)initWithStream:(ITByteStream*)stream delegate:(id <ITDataReceiver>)d {
26 if (self == [super init]) {
27 data = [stream->data copy];
28 lock = [[NSLock alloc] init];
29 delegate = [d retain];
34 - (oneway void)dealloc {
42 - (id <ITDataReceiver>)setDelegate:(id <ITDataReceiver>)d {
45 delegate = [d retain];
49 - (id <ITDataReceiver>)delegate {
53 - (int)availableDataLength {
61 - (NSData*)readDataOfLength:(int)length {
63 NSRange range = {0, length};
65 ret = [data subdataWithRange:range];
66 [data replaceBytesInRange:range withBytes:nil length:0];
71 - (NSData*)readAllData {
74 ret = [data autorelease];
75 data = [[NSMutableData alloc] init];
80 - (void)writeData:(in NSData*)_data {
82 [data appendData:_data];
84 [delegate newDataAdded:self];
87 - (void)writeBytes:(in char *)b len:(long)length {
89 [data appendBytes:b length:length];
91 [delegate newDataAdded:self];
98 - (void)unlockStream {
102 - (void)shortenData:(long)length {
103 NSRange range = {0, length};
104 [data replaceBytesInRange:range withBytes:nil length:0];