X-Git-Url: http://git.ithinksw.org/ITFoundation.git/blobdiff_plain/d0e2a23f6909335baea90af08491a5160026757b..8d770173cee35e1b6139ee6ccb1511ccfa55a3c2:/ITByteStream.m diff --git a/ITByteStream.m b/ITByteStream.m index 4d34eb0..664e1a0 100755 --- a/ITByteStream.m +++ b/ITByteStream.m @@ -17,27 +17,55 @@ { data = [[NSMutableData alloc] init]; lock = [[NSLock alloc] init]; + delegate = nil; } return self; } --(id) initWithStream:(ITByteStream*)stream +-(id) initWithDelegate:(id)d +{ + if (self == [super init]) + { + data = [[NSMutableData alloc] init]; + lock = [[NSLock alloc] init]; + delegate = [d retain]; + } + return self; +} + +-(id) initWithStream:(ITByteStream*)stream delegate:(id)d { if (self == [super init]) { data = [stream->data copy]; lock = [[NSLock alloc] init]; + delegate = [d retain]; } return 0; } --(void) dealloc +-(oneway void) dealloc { + [lock lock]; [data release]; + [lock unlock]; [lock release]; [super dealloc]; } +-setDelegate:(id )d +{ + id old = delegate; + [delegate release]; + delegate = [d retain]; + return old; +} + +-delegate +{ + return delegate; +} + -(int) availableDataLength { int len; @@ -49,17 +77,11 @@ -(NSData*) readDataOfLength:(int)length { - NSData *ret, *tmp; + NSData *ret; NSRange range = {0, length}; [lock lock]; ret = [data subdataWithRange:range]; -#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED [data replaceBytesInRange:range withBytes:nil length:0]; -#else - range = {length, [data length]}; - tmp = [data subdataWithRange:range]; - [data setData:tmp]; -#endif [lock unlock]; return ret; } @@ -74,10 +96,35 @@ return ret; } --(void) writeData:(NSData*)_data +-(void) writeData:(in NSData*)_data { [lock lock]; [data appendData:_data]; [lock unlock]; + [delegate newDataAdded:self]; +} + +-(void) writeBytes:(in char *)b len:(long)length +{ + [lock lock]; + [data appendBytes:b length:length]; + [lock unlock]; + [delegate newDataAdded:self]; +} + +-(void) lockStream +{ + [lock lock]; +} + +-(void) unlockStream +{ + [lock unlock]; +} + +-(void) shortenData:(long)length +{ + NSRange range = {0, length}; + [data replaceBytesInRange:range withBytes:nil length:0]; } @end