Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
 
21
#import "TNSFileHandleTransport.h"
22
#import "TTransportException.h"
23
 
24
 
25
@implementation TNSFileHandleTransport
26
 
27
- (id) initWithFileHandle: (NSFileHandle *) fileHandle
28
{
29
  return [self initWithInputFileHandle: fileHandle
30
                      outputFileHandle: fileHandle];
31
}
32
 
33
 
34
- (id) initWithInputFileHandle: (NSFileHandle *) inputFileHandle
35
              outputFileHandle: (NSFileHandle *) outputFileHandle
36
{
37
  self = [super init];
38
 
39
  mInputFileHandle = [inputFileHandle retain];
40
  mOutputFileHandle = [outputFileHandle retain];
41
 
42
  return self;
43
}
44
 
45
 
46
- (void) dealloc {
47
  [mInputFileHandle release];
48
  [mOutputFileHandle release];
49
  [super dealloc];
50
}
51
 
52
 
53
- (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
54
{
55
  int got = 0;
56
  while (got < len) {
57
    NSData * d = [mInputFileHandle readDataOfLength: len-got];
58
    if ([d length] == 0) {
59
      @throw [TTransportException exceptionWithName: @"TTransportException"
60
                                  reason: @"Cannot read. No more data."];
61
    }
62
    [d getBytes: buf+got];
63
    got += [d length];
64
  }
65
  return got;
66
}
67
 
68
 
69
- (void) write: (uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
70
{
71
  NSData * dataObject = [[NSData alloc] initWithBytesNoCopy: data+offset
72
                                                     length: length
73
                                               freeWhenDone: NO];
74
 
75
  @try {
76
    [mOutputFileHandle writeData: dataObject];
77
  } @catch (NSException * e) {
78
    @throw [TTransportException exceptionWithName: @"TTransportException"
79
                                           reason: [NSString stringWithFormat: @"%s: Unable to write data: %@", __PRETTY_FUNCTION__, e]];
80
  }
81
 
82
  [dataObject release];
83
}
84
 
85
 
86
- (void) flush
87
{
88
 
89
}
90
 
91
@end