Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame^] | 1 | |
| 2 | #import "TNSFileHandleTransport.h" |
| 3 | |
| 4 | |
| 5 | @implementation TNSFileHandleTransport |
| 6 | |
| 7 | - (id) initWithFileHandle: (NSFileHandle *) fileHandle |
| 8 | { |
| 9 | return [self initWithInputFileHandle: fileHandle |
| 10 | outputFileHandle: fileHandle]; |
| 11 | } |
| 12 | |
| 13 | |
| 14 | - (id) initWithInputFileHandle: (NSFileHandle *) inputFileHandle |
| 15 | outputFileHandle: (NSFileHandle *) outputFileHandle |
| 16 | { |
| 17 | self = [super init]; |
| 18 | |
| 19 | mInputFileHandle = [inputFileHandle retain]; |
| 20 | mOutputFileHandle = [outputFileHandle retain]; |
| 21 | |
| 22 | return self; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | - (void) dealloc { |
| 27 | [mInputFileHandle release]; |
| 28 | [mOutputFileHandle release]; |
| 29 | [super dealloc]; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | - (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len |
| 34 | { |
| 35 | int got = 0; |
| 36 | while (got < len) { |
| 37 | NSData * d = [mInputFileHandle readDataOfLength: len-got]; |
| 38 | if ([d length] == 0) { |
| 39 | @throw [NSException exceptionWithName: @"TTransportException" |
| 40 | reason: @"Cannot read. No more data." |
| 41 | userInfo: nil]; |
| 42 | } |
| 43 | [d getBytes: buf+got]; |
| 44 | got += [d length]; |
| 45 | } |
| 46 | return got; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | - (void) write: (uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length |
| 51 | { |
| 52 | NSData * dataObject = [[NSData alloc] initWithBytesNoCopy: data+offset |
| 53 | length: length |
| 54 | freeWhenDone: NO]; |
| 55 | |
| 56 | [mOutputFileHandle writeData: dataObject]; |
| 57 | |
| 58 | |
| 59 | [dataObject release]; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | - (void) flush |
| 64 | { |
| 65 | [mOutputFileHandle synchronizeFile]; // ? |
| 66 | } |
| 67 | |
| 68 | @end |