301d851e54be82fed189d67ecc992c494dec0e4e
[ITFoundation.git] / ITByteStream.m
1 //
2 //  ITByteStream.h
3 //  ITFoundation
4 //
5 //  Created by Alexander Strange on Thu Feb 27 2003.
6 //  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "ITByteStream.h"
10
11 // TODO: Add NSCopying/NSCoding support
12
13 @implementation ITByteStream
14 -(id) init
15 {
16     if (self == [super init])
17            {
18            data = [[NSMutableData alloc] init];
19            }
20     return self;
21 }
22
23 -(id) initWithStream:(ITByteStream*)stream
24 {
25     if (self == [super init])
26            {
27            data = [stream->data copy];
28            }
29     return 0;
30 }
31
32 -(void) dealloc
33 {
34     [data release];
35     [super dealloc];
36 }
37
38 -(int) availableDataLength
39 {
40     return [data length];
41 }
42
43 -(NSData*) readDataOfLength:(int)length
44 {
45     NSData *ret, *tmp;
46     NSRange range = {0, length};
47     ret = [data subdataWithRange:range];
48 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
49     [data replaceBytesInRange:range withBytes:nil length:0]; // this should delete off the end. should test.
50 #else
51     range = {length, [data length]};
52     tmp = [data subdataWithRange:range];
53     [data setData:tmp]; // maybe i should add a lock to this? it would be bad if someone was writing when it was reading...
54 #endif
55     return ret;
56 }
57
58 -(void) writeData:(NSData*)_data
59 {
60     [data appendData:_data];
61 }
62 @end