-
Notifications
You must be signed in to change notification settings - Fork 0
/
HFFileHandle.m
115 lines (94 loc) · 3.03 KB
/
HFFileHandle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// HFFileHandle.m
// HFConnetionDemo
//
// Created by LiQiu Yu on 15-9-28.
// Copyright (c) 2015年 LiQiu Yu. All rights reserved.
//
#import "HFFileHandle.h"
@interface HFFileHandle ()
@property (nonatomic,LW_STRONG) NSFileManager *manager;
@property (nonatomic,LW_STRONG) NSFileHandle *writeFileHandle;
@property (nonatomic,LW_STRONG) NSFileHandle *readFileHandle;
@property (nonatomic,LW_STRONG) HFRequest *request;
@end
@implementation HFFileHandle
@synthesize manager;
@synthesize writeFileHandle;
@synthesize readFileHandle;
@synthesize request;
@synthesize doneFilePath;
@synthesize writingFilePath;
- (id)initWithRequest:(HFRequest*)re{
self = [super init];
if (self) {
self.request = re;
[self setFilePathWithRequestURL:[request.request.URL absoluteString]];
self.manager = [NSFileManager defaultManager];
}
return self;
}
- (void)dealloc{
#if !__has_feature(objc_arc)
[manager release];
[request release];
[writeFileHandle release];
[readFileHandle release];
[doneFilePath release];
[writingFilePath release];
[super dealloc];
#endif
}
- (void)writeData:(NSData*)data{
if (writeFileHandle) {
[writeFileHandle writeData:data];
}
}
- (void)closeFile{
[readFileHandle closeFile];
[writeFileHandle closeFile];
[self fileDone:writingFilePath];
}
- (void) fileDone:(NSString *)path{
self.doneFilePath = [path stringByAppendingPathExtension:@"done"];
NSError *error;
//file exist or not
if ([manager fileExistsAtPath:doneFilePath]) {
[manager removeItemAtPath:doneFilePath error:&error];
}else{
NSLog(@"file not exist at path %@",doneFilePath);
}
if ([manager moveItemAtPath:writingFilePath toPath:doneFilePath error:&error] ){
}else{
NSLog(@"file manger move error %@",error);
}
}
- (NSString *)documentPath {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];;
}
- (void) setFilePathWithRequestURL:(NSString *)url {
NSArray *urlcomponent = [url componentsSeparatedByString:@"/"];
NSString *fileName = [urlcomponent lastObject];
self.writingFilePath = [[self documentPath] stringByAppendingPathComponent:fileName];
self.writeFileHandle = [NSFileHandle fileHandleForWritingAtPath:writingFilePath];
[writeFileHandle seekToFileOffset:[self getStartOffSetWithPath]];
}
- (void)createFile{
if (![[NSFileManager defaultManager] fileExistsAtPath:writingFilePath]) {
[manager createFileAtPath:writingFilePath contents:nil attributes:nil];
}
}
- (unsigned long long)getStartOffSetWithPath{
if(writingFilePath)
{
if ([[NSFileManager defaultManager] fileExistsAtPath:writingFilePath]) {
self.readFileHandle = [NSFileHandle fileHandleForReadingAtPath:writingFilePath];
return [[readFileHandle readDataToEndOfFile] length];
}
else{
//[manager createFileAtPath:_writingFilePath contents:nil attributes:nil];
}
}
return 0;
}
@end