Doesn't crash now. Of course, there's still lots of problems.
[ITFoundation.git] / ITByteStream.m
index e868008..67ecf31 100755 (executable)
@@ -8,7 +8,7 @@
 
 #import "ITByteStream.h"
 
-// TODO: Add NSCopying/NSCoding support. Blocking reads (how would this work? I could hack it with socketpair(), i guess)
+// TODO: Add NSCopying/NSCoding support. Blocking reads (how would this work? NSConditionLock?)
 
 @implementation ITByteStream
 -(id) init
           {
           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];
 }
 
+-(void) setDelegate:(id <ITByteStreamDelegate>)d
+{
+    [delegate release];
+    delegate = [d retain];
+}
+
 -(int) availableDataLength
 {
-    return [data length];
+    int len;
+    [lock lock];
+    len = [data length];
+    [lock unlock];
+    return len;
 }
 
 -(NSData*) readDataOfLength:(int)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]; // this should delete off the end. should test.
+    [data replaceBytesInRange:range withBytes:nil length:0];
 #else
     range = {length, [data length]};
     tmp = [data subdataWithRange:range];
-    [data setData:tmp]; // maybe i should add a lock to this? it would be bad if someone was writing when it was reading...
+    [data setData:tmp];
 #endif
     [lock unlock];
     return ret;
 }
 
--(void) writeData:(NSData*)_data
+-(NSData*) readAllData
+{
+    NSData *ret;
+    [lock lock];
+    ret = [data autorelease];
+    data = [[NSMutableData alloc] init];
+    [lock unlock];
+    return ret;
+}
+
+-(void) writeData:(in NSData*)_data
 {
     [lock lock];
     [data appendData:_data];
     [lock unlock];
+    [delegate newDataAdded:self];
+}
+
+-(void) writeBytes:(char *)b len:(long)length
+{
+    [lock lock];
+    [data appendBytes:b length:length];
+    [lock unlock];
+    [delegate newDataAdded:self];
 }
 @end