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 "TTransportError.h"
23
24
25@interface TNSFileHandleTransport ()
26
27@property(strong, nonatomic) NSFileHandle *inputFileHandle;
28@property(strong, nonatomic) NSFileHandle *outputFileHandle;
29
30@end
31
32
33@implementation TNSFileHandleTransport
34
35-(id) initWithFileHandle:(NSFileHandle *)fileHandle
36{
37  return [self initWithInputFileHandle:fileHandle
38                      outputFileHandle:fileHandle];
39}
40
41
42-(id) initWithInputFileHandle:(NSFileHandle *)aInputFileHandle
43             outputFileHandle:(NSFileHandle *)aOutputFileHandle
44{
45  self = [super init];
46  if (self) {
47    _inputFileHandle = aInputFileHandle;
48    _outputFileHandle = aOutputFileHandle;
49  }
50  return self;
51}
52
53
54-(BOOL) readAll:(UInt8 *)buf offset:(UInt32)off length:(UInt32)len error:(NSError *__autoreleasing *)error
55{
56  UInt32 got = 0;
57  while (got < len) {
58
59    NSData *d = [_inputFileHandle readDataOfLength:len-got];
60    if (d.length == 0) {
61      if (error) {
62        *error = [NSError errorWithDomain:TTransportErrorDomain
63                                     code:TTransportErrorEndOfFile
64                                 userInfo:nil];
65      }
66      return NO;
67    }
68
69    [d getBytes:buf+got length:d.length];
70    got += d.length;
71  }
72  return YES;
73}
74
75
76-(UInt32) readAvail:(UInt8 *)buf offset:(UInt32)off maxLength:(UInt32)len error:(NSError *__autoreleasing *)error
77{
78  UInt32 got = 0;
79  while (got < len) {
80
81    NSData *d = [_inputFileHandle readDataOfLength:len-got];
82    if (d.length == 0) {
83      break;
84    }
85
86    [d getBytes:buf+got length:d.length];
87    got += d.length;
88  }
89  return got;
90}
91
92
93-(BOOL) write:(const UInt8 *)data offset:(UInt32)offset length:(UInt32)length error:(NSError *__autoreleasing *)error
94{
95  void *pos = (void *)data + offset;
96
97  @try {
98    [_outputFileHandle writeData:[NSData dataWithBytesNoCopy:pos length:length freeWhenDone:NO]];
99  }
100  @catch (NSException *e) {
101    if (error) {
102      *error = [NSError errorWithDomain:TTransportErrorDomain
103                                   code:TTransportErrorNotOpen
104                               userInfo:@{}];
105    }
106    return NO;
107  }
108
109  return YES;
110}
111
112
113-(BOOL) flush:(NSError *__autoreleasing *)error
114{
115  return YES;
116}
117
118@end
119