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 -setDelegate:(id <DataReciever>)d
60 delegate = [d retain];
64 -(int) availableDataLength
73 -(NSData*) readDataOfLength:(int)length
76 NSRange range = {0, length};
78 ret = [data subdataWithRange:range];
79 [data replaceBytesInRange:range withBytes:nil length:0];
84 -(NSData*) readAllData
88 ret = [data autorelease];
89 data = [[NSMutableData alloc] init];
94 -(void) writeData:(in NSData*)_data
97 [data appendData:_data];
99 [delegate newDataAdded:self];
102 -(void) writeBytes:(in char *)b len:(long)length
105 [data appendBytes:b length:length];
107 [delegate newDataAdded:self];
120 -(void) shortenData:(long)length
122 NSRange range = {0, length};
123 [data replaceBytesInRange:range withBytes:nil length:0];