r1327@opsdev009 (orig r70766): mcslee | 2007-11-19 17:39:25 -0800
[amiethrift.git] / lib / cocoa / src / transport / TNSStreamTransport.m
blob42a197e123d84c05c5f76dcd3918dc12aa34a475
1 #import "TNSStreamTransport.h"
2 #import "TTransportException.h"
5 @implementation TNSStreamTransport
7 - (id) initWithInputStream: (NSInputStream *) input
8               outputStream: (NSOutputStream *) output
10   [super init];
11   mInput = [input retain];
12   mOutput = [output retain];
13   return self;
16 - (id) initWithInputStream: (NSInputStream *) input
18   return [self initWithInputStream: input outputStream: nil];
21 - (id) initWithOutputStream: (NSOutputStream *) output
23   return [self initWithInputStream: nil outputStream: output];
27 - (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
29   int got = 0;
30   int ret = 0;
31   while (got < len) {
32     ret = [mInput read: buf+off+got maxLength: len-got];
33     if (ret <= 0) {
34       @throw [TTransportException exceptionWithReason: @"Cannot read. Remote side has closed."];
35     }
36     got += ret;
37   }
38   return got;
42 // FIXME:geech:20071019 - make this write all
43 - (void) write: (uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
45   int result = [mOutput write: data+offset maxLength: length];
46   if (result == -1) {
47     @throw [TTransportException exceptionWithReason: @"Error writing to transport output stream."
48                                               error: [mOutput streamError]];
49   } else if (result == 0) {
50     @throw [TTransportException exceptionWithReason: @"End of output stream."];
51   } else if (result != length) {
52     @throw [TTransportException exceptionWithReason: @"Output stream did not write all of our data."];
53   }
54
56 - (void) flush
58   // no flush for you!
61 @end