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];
69 -(int) availableDataLength
78 -(NSData*) readDataOfLength:(int)length
81 NSRange range = {0, length};
83 ret = [data subdataWithRange:range];
84 [data replaceBytesInRange:range withBytes:nil length:0];
89 -(NSData*) readAllData
93 ret = [data autorelease];
94 data = [[NSMutableData alloc] init];
99 -(void) writeData:(in NSData*)_data
102 [data appendData:_data];
104 [delegate newDataAdded:self];
107 -(void) writeBytes:(in char *)b len:(long)length
110 [data appendBytes:b length:length];
112 [delegate newDataAdded:self];
125 -(void) shortenData:(long)length
127 NSRange range = {0, length};
128 [data replaceBytesInRange:range withBytes:nil length:0];